close all; clear all; clc

x = 3 %Use percent to comment,
x = 3; %This is preferred
y1 = 5;

%Addition/Subtraction
z = x+y1;
z = x-y1;

%Multiplication/Division
b = z*2;
b = z/2;

%Exponents
a = x^2;

%% Conditionals
close all; clear all; clc;

a = true; %non zero number is true
b = false; %zero is false


x = 5;
if x<5
    disp('less')
elseif x>5
    disp('greater')
else
    disp('equal')
end

% "and" &&
% "or" ||
% "not ~

%% Vectors
close all; clear all; clc;

%Useful for storing several values into one variable

x = [1,2,3,4,5]; %x is row vector
x = [1 2 3 4 5];
x = x.'; %x is changed to a column vector
x = [1;2;3;4;5]; %x is a column vector

a = x(2);
x(2) = 3;

%% Loops (for and while)
close all; clear all; clc;

%For loops

%for i = start:step:end
%   [code]
%end

%for i = start:end %default step size is 1 (end => start)
%   [code]
%end

x = [5 4 3 2 1];
y1 = zeros(1,length(x)); %[0 0 0 0 0]
for i = 1:length(x)
    y1(i) = x(i);
end

%Multiply by 2 every element in vector x

for i = 1:length(x)
    x(i) = x(i)*2;
end

%Multipy by 2 every element in an odd index in vector x
for i = 1:2:length(x)
    x(i) = x(i)*2;
end

%While Loops

%while [Boolean Expression]
%   [code]
%end

i = 1;
while i<=length(x)
    x(i) = x(i)/2;
    i = i+1;
end

%% Matrices
close all; clear all; clc;

x = [1 2 3;4 5 6];
x(1,2) = 4;
a = x(2,1);

x = x.';

[nr nc] = size(x);
for i = 1:nr
    for j = 1:nc
        x(i,j) = x(i,j)*2;
    end
end

%built in functions

%x = zeros(nr,nc,np,...)
%x = ones(nr,nc,np,...)
%x = rand(nr,nc,np,...)
x = rand; %x gets a random number from 0 to 1
x = rand(2,3);

%% Vectorized code
close all; clear all; clc;
x = [5 4 3 2 1];
%Non vectorized for multiplication
for i = 1:length(x)
    x(i) = x(i)*2;
end

%Vectorized code
x = x*2;
y1 = 2:6;
z = 2:2:8;
a = x(2:4);
x(2:4) = a/3;
x = linspace(1,10,20);

%Addition/Subtract
x = 1:3;
y1 = 4:6;
z = x+y1;
z = x-6;

%Multiplication/Division
z = 3*x;
z = x.*y1; %z = [x(1)*y(1) x(2)*y(2),...]
z = x/3;
z = x./y1;

x = 3:-1:1;
for i = x
    disp(i)
end

%% Functions
close all; clear all; clc;
a = myFun(1,2);
b = myFun2(2,1);
c = myFun3(2,3);

%% Anonymous Functions
f1 = @(t) 3*sin(t);
a = f1(pi);

f2 = @(c) myFun2(c,5);
x = f2(6);


%% Plotting
close all; clear all; clc;

x = linspace(0,2*pi,11); %X-coordinates
y1 = sin(x); 
y2 = cos(x);

%Basic
figure
plot(x,y1)

%Using formatting options
figure
hold on
plot(x,y1,'k--','LineWidth',2)
plot(x,y2,'g*-','MarkerSize',10)
axis equal
grid on
xlabel('X Position (m)')
ylabel('Y Position (m)')
title('Trajectory')
legend('Sine curve','Cosine curve')

%Subplots
%Basic
figure
subplot(1,2,1)
plot(x,y1)

subplot(1,2,2)
hold on
plot(x,y1,'k--','LineWidth',2)
plot(x,y2,'g*-','MarkerSize',10)
axis equal
grid on
xlabel('X Position (m)')
ylabel('Y Position (m)')
title('Trajectory')
legend('Sine curve','Cosine curve')

function out = myFun2(a,b)
out = a-b;
end

function out = myFun3(a,b)
out = a*b;
end

function out = myFun4(t)
out = 3*sin(t);
end