0% found this document useful (0 votes)
19 views

Function Euler Method

This function uses Euler's method to solve initial value problems of the form y'=f(t,y), y(a)=y0 over the interval [a,b] with n steps. It prints the time steps, function evaluations, numerical solutions, exact solutions if known, and errors at each step to demonstrate the application of the method.

Uploaded by

Anis Farhanah
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

Function Euler Method

This function uses Euler's method to solve initial value problems of the form y'=f(t,y), y(a)=y0 over the interval [a,b] with n steps. It prints the time steps, function evaluations, numerical solutions, exact solutions if known, and errors at each step to demonstrate the application of the method.

Uploaded by

Anis Farhanah
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 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