%MATLAB as a calculator
%MATLAB can do the fundamental operations +,-,*,/,^
2+2
x=2*5 %stores the result into the variable x
%MATLAB also knows the common functions
sqrt(169)
exp(2) %MATLAB treats 'e' as a variable
log(exp(3)) %default is natural log
log10(100) %use log10 for base 10
%trig functions are standard, inverse trig is asin(),acos(),etc.
%by default trig is done in radians; use sind() and the like if you want
%degrees
sin(pi/2)
format long %shows more decimal places
sin(1.6)
sind(1.6)
%order of operations with trig
sin^2(40)+cos^2(40) %this produces an error
sin(40)^2+cos(40)^2 %now that's better
%to suppress an output, add a semicolon
y=2^2+sin(pi); %this will output nothing, but assign this value to y
%it's good practice to suppress outputs if you don't need them, I'm showing
%most outputs to show you how MATLAB works

%to clear variables, use the 'clear' command
%clear all clears all variables the workspace
%clc clears the command window
clear all
clc


%--------------------------------------------------------------
%--------------------------------------------------------------
%Power of MATLAB comes from matrices and vectors
x=[1 2 3] %x is now a row vector
y=[2; 1; -5] %use semicolons to make a new row, y is 3 by 1
%computation with vectors
%addition/subtraction only works with same dimensions
%matrix multiplication works if dimensions are compatible
x*y %1 by 3 vector times 3 by 1 vector results in 1 by 1 (dot/inner product)
y*x %3 by 1 times 1 by 3 results in 3 by 3 (outer product)
x.*y %this swaps the order, so result is 3 by 3
%what if we want to multiply/divide componentwise?
z=[5 5 5]; %dimensions match those for x
x+z %same result as "x+5"
x.*z %the dot will do componentwise multiplication,division,exponentiation
%to see these as fractions, use format rat
format rat
x./z 
x.^z 
%often you will want a vector with equally spaced entries
%this can be done in two ways
x=linspace(0,10,5) %left endpt, right endpt, 5 equally spaced points (so
%size determined)
y=0:2.5:10 %left endpt, space size, right endpt (size not determined 
%by spacing)
%use == for componentwise logic check
x==y %entry of 1 where arrays agree, 0 if not
z=sqrt(x).*sin(y) %can do function operations on vectors too, don't forget the dot
%use ' to get the transpose 
%you can check matrix dimensions using size
size(x')
[m,n]=size(x') %if you want to assign to variables

%--------------------------------------------------------------
%--------------------------------------------------------------
%MATRICES AND ARRAYS-Now we are cooking with gas!
A=[1 2 3; 4 5 6; 7 8 8] %matrices are built similar to vectors, use a semicolon to start a new row
A(2,3) %this will output the entry in row 2, col 3 (so 6)
A(3,3)=9 %can edit matrix entries
B=A(2:3,2:3) %get a submatrix by specifying row and column range
%what if you want some rows but all columns (or vice-versa)
B=A(2:3,:) %the colon keeps all rows/columns depending on where you put it (this will keep all the columns)
%you can also delete rows or columns by setting that part to an empty vector
B(:,2)=[] %deletes column 2 from B
%transpose works the same way as vectors
x=A(1,:)' %select first row of A, transpose, and assign to x
%operations with matrices
A*x %multiply matrix and vector as you would expect
A^2 %multiply the matrix by itself
A.^2 %squares each entry (remember that dot indicates a component operation)
%common useful matrices
Z=zeros(2,4) %creates a matrix full of 0's of given dimensions, one argument will make square matrix
I=eye(7) %creates the identity matrix of given size, two dimensions will make desired size with ones on diagonal
T=ones(2) %creates a matrix full of 1's
R=rand(4,3) %creates a matrix of given dimensions with random values between 0 and 1
R=randi([-3,5],4,3) %randi will give random integers in specified range
help randi %help command can be used to get function documentation/description
%We'll do more fun stuff with linear algebra in the future

%--------------------------------------------------------------
%--------------------------------------------------------------
%round-off functions
%these work with non-array variables too, but it made more sense to put
%them after the vectors
t=[-2.33 2.66];
round(t) %round to nearest integer
fix(t) %rounds toward 0
floor(t) %rounds toward -infinity
ceil(t) %rounds toward +infinity
s=[4 9 -9 6];
mod(s,4) %modular division, second component is modulus
mod(s,[4;2]) %can also do array for modulus, but dimensions must agree like matrix multiplication


%--------------------------------------------------------------
%--------------------------------------------------------------
%for and while loops
%one of the most fundamental concepts in coding is a for loop, which is
%useful for doing a process over and over again
%Example 1: adding up integers 1 to n
sum=0; %initialize the sum at 0 before we start the loop
for i=1:100 %this will cycle through every integer between 1 and 100
    sum=sum+i; %this adds the current index to the sum
end %make sure to end loops in MATLAB!  some coding languages end loops differently
sum %this will display the final sum

%Example 2: store partial sums of the "power of two reciprocals" series in an array
A=[]; %initialize with empty array
sum=0; %initialize sum
for i=1:20
    sum=sum+1/(2^i); %compute sum
    A(i)=sum; %store current sum in the i-th position of the array
end
A

%you may instead want to do computations until a certain condition is met
%Example: countdown
count = 10 %initialize counter
while count > 0 %repeat operation until count hits 0
    count=count-1 %updates counts down by 1, assigns new value to count and prints
end

%what if we have a "bad" while loop that won't exit?
count = 10 %initialize counter
while count >= 0 %repeat operation until count hits 0
    count=count+1; %SUPPRESS OUTPUT - it won't terminate!
end
%if a computation is taking too long (possibly due to bug), use ctrl+C to kill the computation

%CONDITIONAL STATEMENTS
%you can write conditional if-elseif-else statements, useful for piecewise
%functions or logic
count=10
while count>=0
    if count==0 %do something fun when the count hits 0
        'Happy New Year!'
        %use apostrophes to declare a string of characters instead of a
        %variable
        break %this gets us out of the while loop
    else
        count=count-1
    end %need to end both if statment and while loop
end 