%% MATH 151A (Fall 2023)
%% Week 0: Introduction to MATLAB
%
%% Arrays
%
%% Construction

a = 1
b = [1 2 3]  % or [1, 2, 3]
c = [a; a+2; a-2; 2*a; a/2]
d = [1 2 3; 4 5 6]
e = [c c+2 2*c; b]

%
%% Indexing
%

c
c(1)
c(3)
c(end)
c(2:4)
c(1:2:end)
e
e(1, 2)
e(2:4, end)
e(2:2:end, :)
c
c(2:3) = [1; 0];
c

%
%% Control flow
%
%% Conditionals

a = 5
if a < 1
    b = 1
elseif a > 5
    b = 5
else
    b = 3
end

%
%% Logical operations

a = 6
if a < 0 || a > 3
    b = 2
else
    b = 3
end
if a >= 0 && a < 5
    c = 2
else
    c = 3
end
if a > 0 || ~(a < -5)
    d = 2
else
    d = 3
end

%
%% Loops

for i = 2:3:10
    i
end
for j = 1:2:10
    j
    if j > 5
        break
    end
end
k = 1
while k <= 5
    k = 2*k;
end
k

%
%% Functions

square = @(x) x^2;
square(3)
square(square(3) + 1)
f = @(x, y) x + 2*y;
f(2, 3)
f([1; 2], [3; 4])
square_vec = @(x) x.^2;
square_vec(3)
square_vec([1; 2; 3])
sin([0 pi/6 pi/4 pi/3 pi/2])
g = @(f, x) f(x/3);
g(@cos, pi)

%
%% Plots
% Plotting \sin(2 \pi x) on [0, 1].

x = linspace(0, 1, 21)
y = sin(2*pi*x)
plot(x, y)

%
%% Exporting and publishing
% For MATLAB live scripts:
% export('week_00_intro.mlx', 'week_00_intro.pdf')
% For MATLAB code and output:
% publish('week_00_intro_code.m', 'pdf')
%