function Matlab_video_tutorial_examples
% Worked examples from MATLAB video tutorial
% You can find lots more examples in the Matlab tutorial,
% as well as old homework problems.

close all  % This closes any open figure windows

% Matlab will execute all the statements in this function
% in turn, starting at the top and working down.
% You have to code things in a logical order, so if you use
% a variable in a calculation, it has to be assigned a value
% before you use it, eg with a=1, or a=some function.
%
% Unlike EN30, everything we do in Matlab functions in EN40 is working
% with numbers.   All the variables simply store a value, they are
% no longer algebraic symbols (we don't define them as syms)
% 'Live Scripts' work better for symbolic stuff.

%Create a row vector
row_vec = [1,2,3,4]
% what is row_vec(3)? (The answer will appear on the command window,
% but you will have to scroll up to see it)
row_vec(3) % This is called 'indexing' - it shows the 3rd number in the vector
row_vec(3) = 10  %This is a way to change just one number in a vector 
%Create a column_vector (note the semicolons)
col_vec = [4;5;6;7]
% You can use 'transpose' to flip a row vector to a column vector and
% vice-versa.   
another_col_vec = transpose(row_vec)
another_row_vec = transpose(col_vec)
% Create a matrix with 4 rows and 4 columns 
matrix = [ 1,2,3,4; ...   % The ... continues the line. First row
           5,6,7,8;...    % Second row
           9,10,22,12;... % You get the idea
           13,15,16,16]
% Let's check some bits of the matrix (see if you can work out what
% each one will be, then check the answer in the command window)
% The syntax for matrices is matrix(row,column)
matrix(1,1)
matrix(3,4)
% Multiply a matrix by a vector.  If you don't know what this means
% yet don't worry - we will cover it in class.  If you've done linear
% algebra this is just a standard matrix/vector multiplication
matrix*col_vec 
%
% Basic images
%
animage = ones(100);  % Create a 100x100 matrix of ones
figure
imshow(animage,'InitialMagnification',200) % display the matrix as an image
animage(50,75) = 0; % Make the pixel at the 50th row and 75th column black
imshow(animage,'InitialMagnification',200)
figure
animage(50,25:75) = 0 % Make the pixels between 25th and 75th columns black (draws a line)
figure
imshow(animage,'InitialMagnification',200)
%
% matrix*row_vec % - this line will bomb, because you cant multiply 
%                  a matrix by a row vector.   Uncomment the line to 
%                 (remove the % at the start of the line) to see
%                 what happens.

%% Example 1 
% Create a vector with a loop
for i=1:401
% YOu can use x(i) = start_x + (i-1)*(end_x - start_x) /(number of points-1)
% to create a vector that starts at start_x ends at end_x, and
% has number of points elements.  Here we start at -10 and end at +10
% The semicolon at the end of the line stops the result of the
% calculation from being displayed in the MATLAB command window
   x(i) = -10 + (i-1)*20/400;   
end
x  % This prints the vector - it's long
%% Example 2 
% We want to plot y = sin(5x) + sin(5.5x)
% We already have a vector of x points
% We can create a vector of y points by using Matlab magic
% Note that y is a vector - for each x(i) there is a corresponding
% y(i) with y(i) = sin(5*x(i)) + sin(5.5*x(i)), with i=1,2,...401
y = sin(5*x) + sin(5.5*x)
% To just do a basic plot use plot(x,y)
% To make the plot fancy, click the arrow in the figure window
% then double click the plot.  You can edit the plot by hand,
% then go to File> Generate Code to generate the Matlab code
% You can cut the matlab code into your file and it will recreate
% the matlab figure (you will  have to edit the line that says
% plot(X1,Y1) to read plot(x,y), because X1, Y1 are not defined in
% this script, they are called x,y instead.

% Create figure
figure1 = figure;
% Create axes
axes1 = axes('Parent',figure1);
hold(axes1,'on');
% Create plot
plot(x,y);
% Create xlabel
xlabel('x axis');
% Create ylabel
ylabel('y axis');
box(axes1,'on');
% Set the remaining axes properties
set(axes1,'FontSize',16);
%%
% Example 3  - plot the function
% $ w = \cases{ cos(v) & cos(v)> sin(v) \cr
%             sin(v) & cos(v)<sin(v) \cr $
% where $ 0<v<4 \pi $
% We will do this by creating a vector v, and another vector w,
% where each number in w is calculated using the formula above.
for k = 1:501
%  We use the same trick from above to create equally spaced points
%  Note we can use k instead of i, or any variable name that makes
%  the code readable.   
   v(k) = (k-1)*4*pi/500;
%  This calculates the sin of the kth entry in v, and stores it in a 
%  variable called s.  You could code this in many other ways too
%  This just makes things readable.
%  Note we cant do s = sin(v), because that will make s a big vector.
%  We just want a single number
   s = sin(v(k));
   c = cos(v(k)); % And c is cos(v(k))
%  Now we create the kth point in the vector w
   if (c>s)
       w(k) = c;
   else
       w(k) = s;
   end
end
% We dont bother making this plot fancy.
figure
plot(v,w)

% Example 4
% Write a function to compute the number of digits in a vector
% that exceed a threshold.  Test it with the first 17 digits of pi
% This creates a vector containing the first 17 digits of pi
% (You don't need to know how this code works, but the 
% 'floor(x)' function computes the nearest integer below x
% so with i=1 we compute the lowest integer below 3.141 (giving 3)
%    with i=2 we compute the lowest integer below (10*pi-30) = 1
%    with i=3 we compute the lowest integer below (100*pi-310) = 4
%    and so on.  This is a total hack and there are better ways to do it
for i=1:17
pivec(i) = floor(pi*10^(i-1))-10*floor(pi*10^(i-2));
end
% This passes the vector pivec into the function, and sets
% the 'threshold' argument to 7
numbergtseven = countvec(pivec,7) % The answer is printed to the command window

%%
% Example 5: Solve the differential equation dw/dt = w(t), with w(t)=0.1 at t=0
% Plot the function for 0<t<10
 t0 = 0;  % The start time
 t1 = 10; % The end time
 tspan = [t0,t1];
 init_w = 0.1;  % The value of w at time t=t0
 % Call the MATLAB ode solver 
 [times,sols] = ode45(@(t,w) diffeq(t,w),tspan,init_w);
 times   % 'times' is a vector of numbers
 sols    % 'sols' is a vector of numbers, giving w at each time
 
 figure
 plot(times,sols)  % This plots the solution
%%
% Example 6
% solve dC/dt = (1/t0)*(1/C - 1) 
%(approximate description of battery charging)
 init_C = 0.1;  % Initial value of C
 trange = [0;15];  % Time range
 t0 = 8;  % Value of the constant t0
 [tvals,Cvals] = ode45(@(t,C) battery(t,C,t0),trange,init_C);
 figure
 plot(tvals,Cvals)
%%
% Example 7
% Solve dm/dt = m(a-b*c)    dc/dt = -c*(g-d*m)   (predator-prey equations)
t0 = 0; t1 = 15;   % Start at t=0 and end at t=15
a=4; b = 3; g = 1; d = 0.1; % Given values for a,b,c,d
m0 = 20; c0 = 1;  % Value of m and c at time t=0
init_w = [m0;c0]; % The initial value for the vector of solutions w
[times,sols] = ode45(@(t,w) catnmouse(t,w,a,b,g,d),[t0,t1],init_w);
figure
plot(times,sols)  % Plot graphs of m and c on the same plot
% sols is now a matrix, the first column is values of m, the second is
% values of c
mice = sols(:,1);  % Extract the first column (the : means take all the rows)
cats = sols(:,2);  % This extracts the cat population

figure
plot(times,mice)  % Plot a graph of just mouse population -v- time
figure
plot(times,cats)  % Plot a graph of just cat population -v- time


end
function dwdt = diffeq(t,w)
% This is the function that tells matlab dw/dt = w for the ODE problem
  dwdt = w;
end
%%
function dCdt = battery(t,C,t0)
% Differential equation for battery problem
dCdt = (1/t0)*(1/C - 1);
end
%%

function dwdt = catnmouse(t,w,a,b,g,d)
% This is the function that tells MATLAB the differential equatino
% for the predator-prey problem
% w is now a column vector containing [m;c]
% w(1) is the first value (i.e. m) and w(2) is the second value (i.e. c)
   m = w(1);
   c = w(2);
   dmdt = m*(a-b*c);  %These functions are specified in the problem
   dcdt = -c*(g-d*m);
   dwdt = [dmdt;dcdt];  % dwdt is also a column vector
end

