%% Euler's method for y'=t+y, y(0)=1 on [0,4]
%  eulerprogr.m -- Change the manual iteration to a program.
% 
%% Function handles:
%
clear; close all;clc
% Function defining the diff. equ: [y' = f(t,y)]
f=@(t,y)t+y
%
% Exact solution:
yanal=@(t) 2*exp(t) - t -1
%% One trial call.
YL=[]   % Cumulate approximations at enpoint
n=3;
[T,Y]=eulerS(f,[0 4],1,n); [T Y],plot(T,Y,T,Y,'.r');shg;YL=[YL Y(end)]
grid on; hold on
%% Now in loop:
while 1     % Infinite loop (1 is always true, elama on laiffii)
   n=2*n
   [T,Y]=eulerS(f,[0 4],1,n); 
   plot(T,Y,T,Y,'.r');shg;
   YL=[YL Y(end)]
   reply=input('Continue? Y/N [Y]:','s');
   if isempty(reply)
       reply='Y'
   end    
   if upper(reply)=='N'
       break
   end
   if n > 50   % A safety check in case user gets out of mind.
       break
   end    
end  
%%
t=linspace(0,4);
plot(t,yanal(t),'k','LineWidth',2)
['Final t  Exact sol.  Euler-approksimation']
[T(end) yanal(T(end)) YL(end)]

%%
endpointerrors=yanal(T(end))-YL

