%% Lab 0 (optional) -- Intro to MATLAB LiveScripts
%
%% The LiveScript Format
% The labs in this course will be presented in the format of MATLAB LiveScripts. You can run these scripts either online (using MATLAB Online at https://matlab.mathworks.com ) or through the LiveEditor interface of MATLAB  on a local computer (either in a university computer lab or on your laptop).
% Of course, if you are reading this you already have it set-up. So Go You!
% The neat thing about the LIveScript is that all the MATLAB Code you see in the file is executable; results will pop up to the right of here. Each time you click Run, all the code in the file is executed and the results will update.
% To make things more manageable, the file can be divided into sections: each time you click Run Section (you can do this also by hitting Ctrl-Enter on a PC, or Cmd-Enter on a Mac), the code within the currect section will be executed. Let's start by giving an example:

f = @(x) x.^3 - 2 * x.^2 - 3*x - 5
fplot(f, [-3,3])

% If you click the Run Section button above, you should see a graph show up. Feel free to change the definition of the function f to get a different graph!
%
%% Arithmetic
% In a pinch, you can use MATLAB as a calculator

% addition
1 + 1
% subtraction
4.5 - 3/2
% multiplication
15 .* 20
% division
3 ./ pi
% exponentiation
4.76.^3.2

% If you move your mouse cursor to a line in the input and click on it, the corresponding output will be highlighted.
% The percent sign. Notice that some of the lines in the code above start with the percent sign (%). The percent sign is used to introduce Comments in to your code. The lines starting with the percent sign are ignored by the MATLAB interpreter, and you can (and we will) use these lines to add explanatory comments to the code shown.
% The period symbol. The sharp-eyed student will notice that there is a period symbol . before the asterisk for multiplication *, the slash for division /, and the caret for exponentiation ^. This has to do with how MATLAB works with linear algebra. For the purposes of this course, the rule of thumb is that you should always prefix multiplication, division, and exponentiation symbols with the period symbol. Otherwise your code may behave unexpectedly!
%% Reference
% arithmetic and the period symbol
% https://www.mathworks.com/help/matlab/matlab_prog/matlab-operators-and-special-characters.html#bvg3oy_-2?s_tid=doc_ta
%
%% Variables
% We are now ready to go beyond the arithmetic manipulation. In MATLAB, it is allowed to store values in "variables", as you will see in the following code.

x=3
y=x+2
x=x.*10

% In this code,
% the variable x is assigned to be 3
% the variable y is assigned to be x+2, which is 5
% the variable x is multiplied by 10.
% Note that the third line means that you do the right hand side first (multiply x by 10), and then assgin the result, which is 30, to x. After this process, x is updated to be 30.
% If you have multiple values to store at a time, you could use an array to do the job.

x=[1 2 3]
y=[4 5 6]

% This is the simplest way to build arrays. The following are some basic manipulations for arrays.

x=x.*10     % every entry in the matrix x is multiplied by 10
y(2)=100    % we assign 100 to the second entry of y

% Note that the x,y here are the arrays continued from the previous code box.
% Another useful function is the summation command, which calculates the sum of numbers in an array.

sum(x)      % sum of entries of x
cumsum(y)   % partial sum of entries of y

% In this code,
% sum(x) is a number, which is equal to 10+20+30=60
% cumsum(y) is an array, whose entries give partial sum of y, namely 4 (the first term), 4+100 (the sum of the first and second terms), and 4+100+6 (the sum of the first three terms).
%% Reference
% sum and cumsum
% https://www.mathworks.com/help/matlab/ref/sum.html
% https://www.mathworks.com/help/matlab/ref/cumsum.html
%% Food for Thought
% The command sum gives the summation of a specific range of numbers, while the command sumsum gives the partial sum for varying ranges. This should remind you of two similar operations about integration you learned in Calculus I: taking the definite integral versus taking the indefinite integral (or anti-derivative). Think about how these pair up and how the pairs are alike.
%
%% Quick construction
% Imagine that you have to construct an array with 100 entries, which are 1, 2, ..., 100. You will not want to achieve this by typing in all the 100 numbers! Therefore, we are going to introduce some efficient ways to build an array.

x=1:3:10
y=linspace(1,10,19)
z=zeros(1,10)
w=ones(1,10)

% In this code,
% the entry of x runs from 1 to 10, increased by 3 each time
% there are 19 equally spaced values in y running from 1 to 10
% z is a 1*10 matrix (or an array with 10 entries) with all the entries being 0
% w is a 1*10 matrix (or an array with 10 entries) with all the entries being 1.
% Note that another frequently appearing command x=1:10 is an abbreviation of x=1:1:10, which means that the entry of x runs from 1 to 10, increased by 1 each time.

x=1:10

% For the specific uses appearing later, we introduce here how to assign a random value to a variable.

x=randi(10)  % the number may be 1, 2, ..., 10
y=rand       % the random number is between 0 and 1

%
%% Reference
% i:j:k notation
% https://www.mathworks.com/help/matlab/ref/colon.html
% linspace
% https://www.mathworks.com/help/matlab/ref/linspace.html
% randi
% https://www.mathworks.com/help/matlab/ref/randi.html
%
%% Control
% MATLAB includes convenient tools to generate graphs. The simplest command to draw a graph is plot. You have to assign x-coordinate, y-coordinate, and graph setting by the way plot(x, y, setting).

plot(5, 1, 'ro')          % draw (5,1) with a red circle
hold on
plot(1:10, 11:20)         % draw (1,11),...,(10,20) with default
hold off

% In the first line, 'r' means red and 'o' means circle. More correspondences between commands and graph settings are listed in the cheat-sheet. hold on tells MATLAB to continue all subsequent plotting commands by showing the results on the same graph, without it MATLAB generates a new graph for each plot coommand. The final  hold off means we do not need hold on anymore.
% You can also plug arrays into the command plot.

x=1:10
y=x.^2
plot(x,y)

% Remember that you can click on the commands above to identify the corresponding figure on the right hand side.
% Another frequently used command is the command for. This is used when you want to repeat doing similar jobs. For example, it offers another way to achieve the command plot(1:10, 11:20, 'bo').

for i=1:10
    plot(i,i+10,'bo')
    hold on
end
hold off

% In this code,
% the variable i runs from 1 to 10, i.e. there are 10 rounds for the second and the third line of commands.
% when i is 1, add the point (i,i+10)=(1,11) in the graph
% when i is 2, add the point (i,i+10)=(2,12) in the graph
% when i is 3, add the point (i,i+10)=(3,13) in the graph
% ......
% when i is 10, add the point (i,i+10)=(10,20) in the graph
% You can also use mutiple for to generate a nested-loop.

for i=1:10
    for j=1:3
        plot(i,i+j,'ro')
        hold on
    end
end
hold off

% In this code,
% the variable i runs from 1 to 10, which means that the code from the second line to the fifth line will be executed 10 times
% when i is 1, j runs from 1 to 3, which means that the points (i,i+j)=(1,1+1), (1,1+2), (1,1+3) will be added to the graph
% when i is 2, j runs from 1 to 3, which means that the points (i,i+j)=(2,2+1), (2,2+2), (2,2+3) will be added to the graph
% ......
% when i is 10, j runs from 1 to 3, which means that the points (i,i+j)=(10,10+1), (10,10+2), (10,10+3) will be added to the graph.
% Sometimes we want to add conditions to decide whether we run the specific commands. The command if is designed to achieve this goal.

x=0
threshold=5
for i=1:10
    if i>threshold
        x=x+i   % replace x by the value of x+i
    end
end
ending=x

% In this code, i runs from 1 to 10 as before. However, we add a condition this time. Only when i>threshold, the command x=x+i will be executed. It means that the variable x is calculating the summation of all such i. Therefore, the final answer would be 6+7+8+9+10=40.
% Sometimes, you may not want MATLAB to show every result on the right hand side. In this case, you could add a semi colon (;) at the end of the command to make the output suppressed. For instance, if you add a semi colon at the end of x=x+i

x=0
threshold=5
for i=1:10
    if i>threshold
        x=x+i;
    end
end
ending=x

% you will get the same result but the middle steps are not shown on the window.
%
%% Reference
% plot
% https://www.mathworks.com/help/matlab/ref/plot.html
% for
% https://www.mathworks.com/help/matlab/ref/for.html
% hold on
% https://www.mathworks.com/help/matlab/ref/hold.html
% if
% https://www.mathworks.com/help/matlab/ref/if.html
%
%% Functions as formulas (symbolic functions)
% MATLAB has a somewhat quirky syntax for writing functions.
% To define a function like f(x), you need to specify what is the independent variable. In this case, it is x. The command to create a function looks like

f = @(x) 2 .* sin(pi .* x) - 3

% In this expression,
% f is the name of the function
% = tells MATLAB you are trying to define f to be something
% the most important part is @(x), this tells MATLAB that you are trying to define a function whose independent variable is called x.
% The remaining part consists of the formula for f.
% Notice that the period symbol . is used in front of the multiplication asterisk .*
% Now that we have defined a function, we can try to evaluate it.

f(0)
f(1/2)
f(1)

% You can use it to define new functions

g = @(x) (f(x)).^2
g(0)
g(1/2)

% You can also define functions with more than one variable (though we won't really need this very much in Calculus II)

h = @(x,y) f(x) + g(y)
h(0,0)
h(0,1/2)
h(1/2,0)

%% Plotting symbolic functions
% For a function defined by a formula, you can plot it using the fplot command. The command takes two arguments, the first argument is the name of the function you are plotting, and the second argument describes the interval on which to do the plotting.
% As an example, we can plot the functions f and g defined in the last section between -3 and 4.

fplot(f,[-3,4])
hold on
fplot(g,[-3 4])
hold off

%% Reference
% fplot
% https://www.mathworks.com/help/matlab/ref/fplot.html
% function creation
% https://www.mathworks.com/help/matlab/matlab_prog/anonymous-functions.html