Sunteți pe pagina 1din 4

CHE634/692/MATLAB/Euler & RK4/NA00

Example of Euler & RK programming using MATLAB


Ruzitah Mohd Salieh

Solve the following problem;


dy 2t - y,
at
y(0)= -1

Pensyarah
Fakulti Kejuruteraan Kimia
Un iverziti Teknologi MARA
40450 Shah Alam

With n =10 to get the value of y at t = 1. Compare with the exact solution of;
y(t)= e -1 +21-2
The following MATLAB function euler.m finds the solution of the-problem using Euler's
method. INPUTS are
a function f(t,y)
the intial and final values time, a & b
/-46 the initial condition yo
tt the number of steps, n
The input function f(t,y) should be defined -as an M-file.
EULER
euler.m

NORNIZAR ANUAR
'taloa Pregnn

4.40 Seriefta Mud& Kiefurvfereaft (Kep,) Klmla


f awiti Kapurwlersan Kion4a
Uwvrsiti Telmoiogi MARA
404 50 S4,01 Mtn+

function euler(f,a,b,y0,n)
% solve the initial-value problem y'=f(t,y), y(a)=y0
% using Euler's method.
fprintf('\n')
Euler method')
disp('
disp('
)
error ')
Exact
yi
f(ti,yi)
disp('
ti
)
disp('
fprintf('\n')
h=(b-a) /n;
y=y0;
%12.6f %12.6f %4.2f\n',a,Y,Y,0)
fprintf( 1966.2f
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*t-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

CHE634/692/MATLAB/Euler & RK4/NA00

fprintf('%6.2f Al2.6f %12.6f\n',t,m,y)


end
end

feu.m
function f =feu(t,y)
f = 2*t-y;

Solution:
In MATLAB command window, type the following
euler('feu1,0,1,-1,10)
This is the result;
Euler method
ti

yi

f(ti,yi)

Exact

error

cyf

0.00
0.10
0.20
0.30
0.40
0.50
0.60
0.70
0.80
0.90
1.00

,
1. 000 0 OC'n41700 0 0 0
1.100000:6.790000
-0.671000
1_190000
-0.543900
1.271000
-0.409510
1.343900
-0.268559
1.409510
-0.121703
1.468559
0.030467
1.521703
0.187420
1.569533
0_348678
1.612580

-1.000000
-0.895163
-0.781269
-0.659182
-0.529680
-0;393469
-0.251188
-0.103415
0.049329
0.206570
0.367879

0.00
4.84e-003
8.73e-003
1.18e-002
1.42e-002
1.60e-002
1.74e-002
1.83e-002
1.89e-002
1.91e-002
1.92e-002

CHE634/692/MATLAB/Euler & RK4/NA00

RUNGE-KUTTA
rk2 4.m
function rk24k(f,a,b,y0,n,order)
% solve the initial-value problem y' = f(t,y), y(a)=y0
% using Runge-Kutta methods.
fprintf('\n')
disp(('
Runge-Kutta method of order = ',num2str(order)]) .
h=(b-a)/n;
y=y0;
if (order==2)
disp('
t
disp('
error ')
disp ('
,)

kl

k2

Exact

fprintf('\n')
%12.6f %12.6f
fprintf('%6.2f
%4.2f\n',a,y,y,0)
for i=1:n
t=a+(i-1)*h;
kl=feval(f,t,y);k2=feval(f,t+h,y+h*kl);
Y=Y+h*(kr+k2)/2;
t=t+h;
% Enter the exact solution if known as g=g(t)
% otherwise set g='n'.
g=exp(-t)+2*t-2;
if (g-='n')
err=abs(g-y);
fprintf('%6.2f %12.6f %12.6f %12.6f %12.6f
%8.2e\n',t,k1,k2,y,g,err)
else
%12.6f %12.6f\n',t,kl,k2,y)
fprintf('%6.2f
end
end
end
if (order==4)
disp('
disp('


Exact


kl
error ')

disp('
fprintf('\n')
fprintf('%6.2f
---%12.6f %12.6f %4.2f\n',a,y,y,0)
for i=1:n
t=a+(i-1)*h;

k2

k3

k4

CHE634/692JMATLAB/Euler & RK4/NAOO

kl=feval(f,t,y);
k2=feval(f,t+h/2,y+h*k1/2);
k3=feval(f,t+h/2,y+h*k2/2);
k4=feval(f,t+h,y+h*k3);
Y=Y+h*(k1+2*k2+2*k3+k4)/6;
t=t+h;
% Enter the exact solution if known as g=g(t)
% otherwise set g='n'.
g=exp(-t)+2*t-2;
if (g-='n')
err=abs(g-y);
fprintf('%6.2f %12.6f %12.6f %12.6f %12:6f %12.6f %12.6f
%8 . 2e \n' , t, kl, k2, k3, k4, y, g, err)
else
fprintf ( ' %6.2f %12.6f %12.6f %12 6f %12 . 6f
%12 . 6f \n' , t, kl, k2, k3, k4, y)
end
end
end

Solution;
Ln MATLAB_command window, type the following
rk2 4 (' - feu', 0, 1,-1, 10, 4)

This is the result;


fxv -4 k,

2,

k-

Lk I. ( K. -1

N 4-

4-v-4 -2-2,61)
)

k -4 2 L'2

I-3

Runge-Kutta method of order 4


ti

k 1

k2

k3

k4

yi

Exact

Error

0.0
0.1
0.2
0.3
0.4
0.5
Q.6
0.7
0.8
0.9
1.0

--
1.000000
1.095163
1.181269
1.259182
1.329680
1.393469
1.451188
1.503414
1.550671
1.593430

-
1.050000
1.140404
1.222206
1.296222
1.363196
1.423796
1.478629
1.528244
1.573137
1.613759

--
1.047500
1.138142
1.220159
1.29437d
1.361520
1.422279
1.477257
1.527002
1.572014
1.612742

----
1.095250
1.181348
1.259253
1.329745
1.393528
I .451241
1.503462
1.550714
1.593469
1.632156

-1
-0.895162
-0.781269
-0.659182
-0.52968
-0.393469
-0.251188
-0.103414
0.049329
0.20657
0.36788

-I
-0.895163
-0.781269
-0.659182
-0.52968
-0.393469
-0.251188
-0.103415
0.049329
0.20657
0.367879

0
8.20e-008
I.48e-007
2.01 e-007
2.43e-007
2.75c-007
2.98e-007
3.15e-007
3.26e-007
3.31c-007
3.33e-007

S-ar putea să vă placă și