%  *********************************************************************
%     Use myEuler.m function to solve Y'=-Y^2 with differnt step size                                                               *
%   Math 430 * Math 635 * Lecture 7 * Sep 24, 2015 * Victor Matveev  
% **********************************************************************

f = @(y) -y.^2;
y0     = 1;
tTotal = 1;

for N = [2 4 6 10]  % Try the folowing partitions: N=2, N=4, N=6 and N=10
    
    [T, Y] = myEuler(f, y0, tTotal, N);  % Get the solution computed in myEuler.m
    plot(T, Y, 'o-'); % Plot results as a solid line connecting data point circles
    hold on;
    
end;
 
t = 0 : 0.01 : 1;                             % Make a denser set of t-values 
plot(t, 1 ./ (1 + t), 'k-');                  % The exact solution is Y(t) = 1/(1+t)
xlabel('t'); ylabel('Y(t)');                  % Without labels who knows what is plotted...
title('Euler method to solve Y''=-Y^2');      % Titles are nice
legend('N=2', 'N=4', 'N=6', 'N=10', 'Exact'); % Always include a legend for multiple curves!


