function section2_examples
%
%  Test a simple function to multiply two numbers

   twicetwo = multiplyab(2,2)  % The answer will be displayed on the command window

% Example 1
% 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 2: 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
 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),[t0,t1],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 3
% 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 number = countvec(vec,thresh)
% This function counts the number of entries in 'vec' that exceed
% 'thresh'
% vec is a vector of numbers (vec(1) is the first; vec(2) the second etc)
% thresh is a number
% 'number' is the answer computed by the function
number = 0;   % We have to initialize the variable number to zero
for i = 1:length(vec)
  if (vec(i)>thresh) % check if the ith number in vec is bigger than thresh
      number = number + 1;   % add 1 if true, otherwise do nothing
  end
end

% For the function to work properly, the output argument
% 'number' must be assigned a value inside the function

end


function c = multiplyab(a,b)
% Function to multiply two numbers
   c=a*b;
end

function dwdt = diffeq(t,w)
% This is the function that tells matlab dw/dt = w for the ODE problem
  dwdt = w;
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