0% found this document useful (0 votes)
53 views1 page

% Solve The Initial-Value Problem Y' F (T, Y), X (A) Y0 % Using Euler's Method

This document contains code to solve an initial value problem using Euler's method. It defines an 'euler' function that takes inputs of a function f, initial value a, endpoint b, initial condition y0, and number of steps n. The function prints out a table with the time step ti, function value f(ti,yi), solution yi, and error compared to the exact solution at each step.

Uploaded by

afnan_lion94
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
53 views1 page

% Solve The Initial-Value Problem Y' F (T, Y), X (A) Y0 % Using Euler's Method

This document contains code to solve an initial value problem using Euler's method. It defines an 'euler' function that takes inputs of a function f, initial value a, endpoint b, initial condition y0, and number of steps n. The function prints out a table with the time step ti, function value f(ti,yi), solution yi, and error compared to the exact solution at each step.

Uploaded by

afnan_lion94
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 1

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

% solve the initial-value problem y'=f(t,y), x(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 = 0.5 + 0.4*exp(-0.05*t) - 0.9*exp(-0.1*t);
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 Al2.6f %12.6f\n',t,m,y)
end
end

feu.m:
function f =feu(t,y)
f = -0.02*exp(-0.05*t) + 0.09*exp(-0.1*t);

Command window:
euler('feu',0,60,0,60)

You might also like