You are on page 1of 1

function EulerMethod(f,a,b,y0,n)

%solve the initial value problem y'=f(t,x),y(a)=y0


%using Euler's method
fprintf ('\n')
disp('
Euler Method')
disp('______________________________________________________________')
disp('ti
f(ti,yi)
yi
Exact
error')
disp('______________________________________________________________')
fprintf('\n')
h=(b-a)/n;
y=y0;
fprintf('%6.2f
-----%12.6f
%12.6f
%4.2f\n',a,y,y,0)
for i=1:n
t=a+(i-1)*h;
m=feval(f,t,y);
y=y+h*m;
%write the exact solution g if known as g=g(t)
%otherwise set g='n'
t=t+h;
g=exp(-t)+2*2-2;
if(g~='n')
err=abs(g-y);
fprintf('%6.2f %12.6f %12.6f %12.6f
%8.2e\n',t,m,y,g,err)
else
fprintf('%6.2f
%12.6f
%12.6f\n',t,m,y)
end
end
function f=feu(t,y)
f=2*t-y;

You might also like