function homework1_solutions

% MAX TOTAL GRADE 25 POINTS + 5 POINTS EXTRA CREDIT

close all   % This closes any open fig windows

% Solution to Problem #1   [2 POINTS]
for i=1:201
   x(i) = -3+6*(i-1)/200;
end

% Solution to problem #2   [2 POINTS]
figure1 = figure;
for i=1:length(x)
  y(i) = exp(-x(i)^2);
end
% The axis labels etc are optional for all problems...
axes('Parent',figure1,'FontSize',12);
box('on');
hold('all');
plot(x,y);
title({'Solution to problem #2'},'FontSize',14);
xlabel({'{\it x}'},'FontSize',12,'FontName','Times New Roman');
ylabel({'y'},'FontSize',12,'FontName','Times New Roman',...
    'FontAngle','italic');


% Solution to problem #3  [3 POINTS]
for i = 1:101
  v(i) = (i-1)*2*pi/100;
  if (sin(v(i))<0.707) 
      w(i) = sin(v(i));
  else
      w(i) = 0.707;
  end
end
figure2 = figure;
axes('Parent',figure2,'FontSize',12);
box('on');
hold('all');
plot(v,w);
title({'Solution to problem #3'},'FontSize',14);
xlabel({'{\it v}'},'FontSize',14,'FontName','Times New Roman');
ylabel({'w'},'FontSize',14,'FontName','Times New Roman',...
    'FontAngle','italic');

% Solution to problem #4 (see below for the function) [5 POINTS]
avg = compute_average(w)

%Solution to Problem #5 [5 POINTS]
t0 = 8;   % Value of time constant
C0=0.1;   % Initial battery charge

[tvals,cvals] = ode45(@charge,[0,15.],C0);
figure3 = figure;
axes('Parent',figure3,'FontSize',12);
box('on');
hold('all');
plot(tvals,cvals);
title({'Solution to problem #5'},'FontSize',14);
xlabel({'{Time (sec)}'},'FontSize',14,'FontName','Times New Roman');
ylabel({'% Charge'},'FontSize',14,'FontName','Times New Roman');

% Solution to problem #6 [8 POINTS]

% Parameters for 6.1
m0 = 20;  % Initial population of mice
c0 = 1.; % Initial population of cats
alpha = 4;  % Rate of mouse reproduction
beta = 3; % Rate of cat eating mouse
gamma = 1; % Rate of cat perishing
delta = 0.1; % Rate of cat increase

y0 = [m0,c0];
[timevals,solvals] = ode45(@predator_prey,[0,20],y0);
figure4 = figure;
axes('Parent',figure4,'FontSize',12);
box('on');
hold('all');
plot(timevals,solvals);
title({'Solution to problem #6.1'},'FontSize',14);
xlabel({'{Time (sec)}'},'FontSize',14,'FontName','Times New Roman');
ylabel({'Population'},'FontSize',14,'FontName','Times New Roman');

% Problem 6.2
c0=2;
y0 = [m0,c0];
[timevals,solvals] = ode45(@predator_prey,[0,20],y0);
figure5 = figure;
axes('Parent',figure5,'FontSize',12);
box('on');
hold('all');
plot(timevals,solvals);
title({'Solution to problem #6.2'},'FontSize',14);
xlabel({'{Time (sec)}'},'FontSize',14,'FontName','Times New Roman');
ylabel({'Population'},'FontSize',14,'FontName','Times New Roman');


% Extra credit problem (5 points)
% For a steady state solution dm/dt = 0 and dc/dt = 0
% Solving for c and m gives c=alpha/beta and m=gamma/delta
% Here's a solution with these initial conditions, but with
% a very small change in the mouse population to test whether
% the solution is stable.  The graph shows that an oscillation
% develops in both the cat and mouse populations, so the 
% steady solution is not stable.
c0=alpha/beta;
m0 = gamma/delta+0.00001;
y0 = [m0,c0];
[timevals,solvals] = ode45(@predator_prey,[0,200],y0);
figure6 = figure;
axes('Parent',figure6,'FontSize',12);
box('on');
hold('all');
plot(timevals,solvals);
title({'Solution to problem #6.3'},'FontSize',14);
xlabel({'{Time (sec)}'},'FontSize',14,'FontName','Times New Roman');
ylabel({'Population'},'FontSize',14,'FontName','Times New Roman');


    function dCdt = charge(t,C)
        dCdt = (1/t0)*(1/C-1);
    end

    function dydt = predator_prey(t,y)
        % The array y stores [m,c] at time t
        m = y(1);
        c = y(2);
        dmdt = m*(alpha-beta*c);
        dcdt = -c*(gamma-delta*m);
        dydt = [dmdt;dcdt];
    end



end
function avg = compute_average(vector)
% Function to compute the average of a vector
  avg = 0;
  for i =1:length(vector)
      avg = avg + vector(i);
  end
  avg = avg/length(vector);
end