0% found this document useful (0 votes)
44 views8 pages

Euler Method Via M-Files: Matlab Code in Eulerc1.m For Y' C (Y - Y) and y (0) 200

The document describes Matlab code for numerically solving ordinary differential equations using the Euler method. It provides code for two examples: 1) solving y' = c(ysur - y) with initial condition y(0) = 200, and 2) solving y' = ty^1/2 with initial condition y(0) = 4. The code calculates the numerical solution for varying values of KK (the number of time steps) and plots the numerical solution alongside the exact solution to calculate the error.

Uploaded by

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

Euler Method Via M-Files: Matlab Code in Eulerc1.m For Y' C (Y - Y) and y (0) 200

The document describes Matlab code for numerically solving ordinary differential equations using the Euler method. It provides code for two examples: 1) solving y' = c(ysur - y) with initial condition y(0) = 200, and 2) solving y' = ty^1/2 with initial condition y(0) = 4. The code calculates the numerical solution for varying values of KK (the number of time steps) and plots the numerical solution alongside the exact solution to calculate the error.

Uploaded by

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

Euler Method via m-files

Matlab Code in eulerc1.m for


y’ = c(ysur – y) and y(0) = 200

clear;
y(1) = 200.;
yexact(1) = 200;
T = 50;
KK = 4
h = T/KK;
t(1)= 0.;
for k = 1:KK
t(k+1) = t(k) + h;
yexact(k+1) = 70 + 130*
exp(-.05*t(k+1));
y(k+1) = y(k) + h*
(.05*(70 - y(k)));
end
error = abs(yexact(KK+1) - y(KK+1))
plot(t,y,'b',t,yexact,'r')
EDU» eulerc1

KK =

error =

8.1002

200

180

160

140

120

100

80

60
0 5 10 15 20 25 30 35 40 45 50
EDU» eulerc1

KK =

error =

4.1828

200

180

160

140

120

100

80

60
0 5 10 15 20 25 30 35 40 45 50
EDU» eulerc1

KK =

16

error =

2.0935

200

180

160

140

120

100

80

60
0 5 10 15 20 25 30 35 40 45 50
EDU» eulerc1

KK =

32

error =

1.0450

200

180

160

140

120

100

80

60
0 5 10 15 20 25 30 35 40 45 50
Matlab code in eulerc2.m for
y’ = t y1/2 and y(0) = 4

clear;
y(1) = 4.;
yexact(1) = 4;
T = 2;
KK = 32
h = T/KK;
t(1)= 0.;
for k = 1:KK
t(k+1) = t(k) + h;
yexact(k+1) = (.25*t(k+1)^2
+ 2)^2.;
y(k+1) = y(k) + h*(t(k)*y(k)^.5);
end
error = abs(yexact(KK+1) - y(KK+1))
plot(t,y,'b',t,yexact,'r')
eulerc2

KK =

error =

1.6708

8.5

7.5

6.5

5.5

4.5

4
0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 2
EDU» eulerc2

KK =

32

error =

0.2323

8.5

7.5

6.5

5.5

4.5

4
0 0.2 0.4 0.6 0.8 1 1.2 1.4 1.6 1.8 2

You might also like