HW Mat4
HW Mat4
HW Mat4
Q25.2 Solve the following problem over the interval from x = 0 to 1 using a step size of 0.25 where y(0) =1. Display all your results on the same graph.
dy
=(1+2 x) √ y
dx
(a) Analytically.
(b) Euler’s method.
(c) Heun’s method without the corrector.
(d) Ralston’s method.
(e) Fourth-order RK method.
Solution ……
(a) Analytically.
1
dy
=( 1+2 x ) . dx ≫≫( y ) 2 dy =( 1+ 2 x ) . dx
√y
Integrating x=0 , y = 1
y 1 x
∫ ( y )2 dy=∫ ( 1+2 x ) . dx
1 0
1 1 1
2 y y =x + x 2 x ≫≫ 2 y 2 −2 ( 1 ) 2 =x + x 2−( 0 )
1
2
| 0 |
1
2 x+ x 2 +2 x+ x2 +2 2
y = ≫ ≫ y =( )
2 2
at x = 0 , 0.25 , 0.5 , 0.75 , 1
2 2 2 2
x + x 2 +2 2 0+(0) +2 x + x 2 +2 2 0.25+(0.25) + 2
y ( 0 )=( ) =( ) =1 y ( 0.25 )=( ) =( ) =1.337
2 2 2 2
2 2 2 2
x + x 2 +2 2 0.5+(0.5) +2 x + x 2 +2 2 0.75+(0.75) + 2
y ( 0.5 )=( ) =( ) =1.891 y ( 0.75 )=( ) =( ) =2.743
2 2 2 2
2 2
x + x 2+2 2 1+(1) + 2
y ( 1 )=( ) =( ) =4
2 2
ـــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــ
when x = 0 , y(0) =1
1 1
y '1=( 1+2 x )( y ) 2 =( 1+2( 0) ) ( 1 ) 2 =1
1 1
y '1=( 1+2 x )( y ) 2 =( 1+2( 0.25) ) ( 1.25 ) 2 =1.677
1 1
y '1=( 1+2 x )( y ) 2 =( 1+2( 0.5) ) ( 1.669 ) 2 =2.584
1 1
y '1=( 1+2 x )( y ) 2 =( 1+2(0.75) ) ( 2.473 ) 2 =3.931
ـــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــ
when x = 0 , y(0) =1
1 1
' 2 2
y =( 1+2 x )( y ) =( 1+2(0) ) ( 1 ) =1
y 0' = yi + y '∗h=1+(1∗.25)=1.25
1 1
' 0 2
y =( 1+2 x ) ( y ' ) =( 1+2(0.25) ) ( 1.25 ) 2 =1.677
1
1 1
' 2 2
y =( 1+2 x )( y ) =( 1+2(0.25)) ( 1.334 ) =1.733
1 1
' 2 2
y =( 1+2 x )( y ) =( 1+2(0.5) ) ( 1.888 ) =2.748
y 0' = yi + y '∗h=1.888+(2.748∗0.25)=2.575
1 1
' 0 2
y =( 1+2 x ) ( y ' ) =( 1+2(0.75) ) ( 2.575 ) 2 =4.012
1
1 1
' 2 2
y =( 1+2 x )( y ) =( 1+2(1) ) ( 2.711 ) =4.112
y 0' = yi + y '∗h=2.711+(4.112∗0.25)=3.740
1 1
' 0 2
y =( 1+2 x ) ( y ' ) =( 1+2(1) ) (3.740 )2 =5.802
1
when x = 0 , y(0) =1
1 1
k 1=f ( x , y ) =( 1+ 2 x ) ( y )2 = ( 1+ 2(0) ) ( 1 ) 2 =1
3 3
k 2=f ( xi + h , y i + k 1 h)=¿
4 4
1 1
k 1=f ( x , y ) =( 1+ 2 x ) ( y )2 = ( 1+ 2(0.25) ) ( 1.333 ) 2 =1.732
3 3
k 2=f ( xi + h , y i + k 1 h)=¿
4 4
Numerical Methods Course Home work
1 1
k 1=f ( x , y ) =( 1+ 2 x ) ( y )2 = ( 1+ 2(0.5) ) ( 1.879 ) 2 =2.741
3 3
k 2=f ( xi + h , y i + k 1 h)=¿
4 4
1 1
2 2
k 1=f ( x , y ) =( 1+ 2 x ) ( y ) = ( 1+ 2(0.75) ) ( 2.719 ) =4.122
3 3
k 2=f ( xi + h , y i + k 1 h)=¿
4 4
ـــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــ
when x = 0 , y(0) =1
1 1
2 2
k 1=f ( x , y ) =( 1+ 2 x ) ( y ) = ( 1+ 2(0) ) ( 1 ) =1
1 1
k 2=f ( xi + h , y i + k 1 h)=¿
2 2
1 1
k 3=f (xi + h , y i + k 2 h)=¿
2 2
1 1
k 4=f ( x i +h , y i+ k 3 h)=( 1+2 ( x +h ) ) ( y+ k 3 h ) 2 =( 1+2 ( 0+0.25 ) ) ( 1+1.350∗0.25 ) 2 =1.735
Numerical Methods Course Home work
h 0.25
y i+1 = y i+ ( k 1 +2∗k 2 +2∗k 3 +k 4 )=1+ ( 1+2∗1.325+2∗1.350+1.735 )=1.337
6 6
when x = 0.25 , y(0) =1.337
1 1
k 1=f ( x , y ) =( 1+ 2 x ) ( y )2 = ( 1+ 2(0.25) ) ( 1.337 )2 =1.734
1 1
k 2=f ( xi + h , y i + k 1 h)=¿
2 2
1 1
k 3=f (xi + h , y i + k 2 h)=¿
2 2
1 1
2 2
k 4=f ( x i +h , y i+ k 3 h)=( 1+2 ( x +h ) ) ( y+ k 3 h ) =( 1+2 ( 0.25+0.25 ) ) ( 1.337+2.220∗0.25 ) =2.751
h 0.25
y i+1 = y i+ ( k 1 +2 k 2 +2 k 3 +k 4 ) =1.337+ ( 1.734+2∗2.181+2∗2.220+2.751 ) =1.891
6 6
1 1
2 2
k 1=f ( x , y ) =( 1+ 2 x ) ( y ) = ( 1+ 2(0.5) ) ( 1.891 ) =2.750
1 1
k 2=f ( xi + h , y i + k 1 h)=¿
2 2
1 1
k 3=f (xi + h , y i + k 2 h)=¿
2 2
1 1
k 4=f ( x i +h , y i+ k 3 h)=( 1+2 ( x +h ) ) ( y+ k 3 h ) 2 =( 1+2 ( 0.5+0.25 )) ( 1.891+3.42∗0.25 ) 2 =6.864
h 0.25
y i+1 = y i+ ( k 1 +2 k 2 +2 k 3 +k 4 ) =1.891+ ( 2.75+ 2∗3.364+ 2∗3.42+6.864 )=2.857
6 6
when x = 0.75 , y(0) =2.857
1 1
k 1=f ( x , y ) =( 1+ 2 x ) ( y )2 = ( 1+ 2(0.75) ) ( 2.857 ) 2 =4.226
1 1
k 2=f ( xi + h , y i + k 1 h)=¿
2 2
1 1
k 3=f (xi + h , y i + k 2 h)=¿
2 2
Numerical Methods Course Home work
1 1
2 2
k 4=f ( x i +h , y i+ k 3 h)=( 1+2 ( x +h ) ) ( y+ k 3 h ) =( 1+2 ( 0.75+0.25 ) ) ( 2.857+5.137∗0.25 ) =6.105
h 0.25
y i+1 = y i+ ( k 1 +2 k 2 +2 k 3 +k 4 ) =2.857+ ( 4.226+ 2∗5.06+2∗5.137+6.105 ) =4.140
6 6
4.5
3.5
3
alytically
2.5
Euler’s method
Heun’s method
2
Ralston’s method
Fourth-order RK method
1.5
0.5
0
0 0.25 0.5 0.75 1 1.25
ـــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــ
d2 y dy
2
+0.5 +7 y
dx dx
where y(0) = 4 and y(0) = 0. Solve from x = 0 to 5 with h = 0.5 Plot your results.
Solution ……
Numerical Methods Course Home work
dy
Let = y'
dx
d y' '
=−0.5 y −7 y
dx
x y
y; k 11 k 12 y mid y ,mid ❑ k 21 k 22 y mid y ,mid ❑ k 31 k 32 y end y , end k 41 k 42
0 4 0 0 -28 4 -7 -7 -31.5 2.25 -7.875 -7.875 -11.813 0.0625 -5.906 -5.906 2.516
0.5 1.028 -9.34 -9.34 -2.526 -1.305 -9.97 -9.97 14.12 -1.463 -5.81 -5.81 13.14 -1.875 -2.77 -2.77 14.51
1 -2.147 -3.91 -3.91 16.984 -3.125 0.336 0.336 21.7 -1.389 1.515 1.515 13.68 -1.389 2.93 2.93 8.258
1.5 -2.497 4.09 4.09 15.434 -2.47 7.95 7.95 13.315 -0.51 7.42 7.42 -0.144 1.213 4.02 4.02 -10.50
2 0.7405 6.7 6.7 -8.534 2.24 4.67 4.67 -17.735 1.908 2.27 2.27 -14.5 -0.546 -0.546 -0.546 57.162
2.5 -0.92 5.38 5.38 3.75 0.425 6.32 6.32 -6.135 0.66 3.85 3.85 -6.545 1.005 2.11 2.11 -8.09
3 1.4 2.905 2.905 -11.25 2.13 0.092 0.092 -14.96 1.423 -0.834 -0.834 -9.54 0.983 -1.867 -1.867 -5.95
3.5 1.36 -2.61 -2.61 -8.215 0.7075 -4.66 -4.66 -2.62 0.195 -3.27 -3.27 0.27 -0.275 -2.48 -2.48 3.165
4 -0.386 -3.23 -3.23 4.317 -1.19 -2.15 -2.15 9.43 -0.923 -0.87 -0.87 6.9 -0.821 0.219 0.219 5.64
4.5 -1.14 0.32 0.32 7.82 -1.06 2.28 2.28 6.28 -0.57 1.89 1.89 3.045 -0.195 1.843 1.843 0.444
5 -0.195 0.444 0.444 1.143 -0.084 0.729 0.729 0.223 -0.013 0.5 0.5 -0.15 0.055 0.365 0.365 -0.567
0
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 5.5
y
-2
y2
-4
-6
-8
-10
-12
ـــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــ
Q 25.7 Use (a) Euler’s and (b) the fourth-order RK method to solve
2
dy
=−2+4 e−x dz =− y z
dx dx 3
Numerical Methods Course Home work
over the range x = 0 to 1 using a step size of 0.2 with y(0) = 2 and z(0) = 4.
Solution ……
dy
=−2 y+ 4 e−x
dx
when x = 0 , y(0) =2
ـــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــ
dz − y z 2 dz −4 x e−x ∗z2
= ≫≫ ≫ =
dx 3 dx 9
when x = 0 , z(0) = 4
2
−4 x e−x∗z 2 −4 (0)e ∗(4)
−0
z i=f ( x , z ) = = =0
9 9
−4 x e−x∗z 2 −4 ( 0.2 ) e ∗( 4 )2
−0.2
z i=f ( x , z ) = = =−1.164
9 9
x y z
0 2 4
0.2 2 4
0.4 1.855 3.77
0.6 1.65 3.43
0.8 1.429 3.08
1 1.22 2.78
ـــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــــ
1 1 0 0.2
( 2 2 )
k 2=f xi + h , y i + k 1 h =−2∗2+ ∗0.2+ 4 e−0+
2 2
=−0.1
1 1 −0.1 0.2
( 2 2 )
k 3=f xi + h , y i + k 2 h =−2∗2+
2
∗0.2+ 4 e−0+
2
=0.09
h 0.2
y i+1 = y i+ ( k 1 +2∗k 2 +2∗k 3 +k 4 )=2+ ( 0−2∗0.1+ 2∗0.09+ 0.218 )=2.006
6 6
when x = 0.2 , y(0) =2.006
1 1 −0.737 0.2
( 2 2 )
k 2=f xi + h , y i + k 1 h =−2∗2.006+
2
∗0.2+ 4 e−0.2 +
2
=−0.711
1 1 −0.711 0.2
( 2 2 )
k 3=f xi + h , y i + k 2 h =−2∗2.006+
2
∗0.2+ 4 e−0.2 +
2
=−0.708
h 0.2
y i+1 = y i+ ( k 1 +2∗k 2 +2∗k 3 +k 4 )=2.006+ (−0.737−2∗0.711−2∗0.708−1.25 )=1.859
6 6
when x = 0.4 , y(0) =1.845
1 1 −1.009 0.2
( 2 2 )
k 2=f xi + h , y i + k 1 h =−2∗1.845+
2
∗0.2+ 4 e−0.4 + =−1.01
2
1 1 −1.01 0.2
( 2 2 )
k 3=f xi + h , y i + k 2 h =−2∗1.845+
2
∗0.2+ 4 e−0.4 +
2
=−1.01
h 0.2
y i+1 = y i+ ( k 1 +2∗k 2 +2∗k 3 +k 4 )=1.845+ (−1.009−4∗1.01−1.011 )=1.643
6 6
when x = 0.6 , y(0) =1.643
1 1 −1.091 0.2
( 2 2 )
k 2=f xi + h , y i + k 1 h =−2∗1.643+
2
∗0.2+ 4 e−0.6 +
2
=−1.100
1 1 −1.1 0.2
( 2 2 )
k 3=f xi + h , y i + k 2 h =−2∗1.643+
2
∗0.2+ 4 e−0.6 +
2
=−1.101
h 0.2
y i+1 = y i+ ( k 1 +2∗k 2 +2∗k 3 +k 4 )=1.643+ (−1.091−2∗1.1−2∗1.101−1.111 )=1.423
6 6
when x = 0.8 , y(0) =1.423
1 1 −1.049 0.2
( 2 2 )
k 2=f xi + h , y i + k 1 h =−2∗1.423+
2
∗0.2+ 4 e−0.8 n+
2
=−1.054
1 1 −1.054 0.2
( 2 2 )
k 3=f xi + h , y i + k 2 h =−2∗1.423+
2
∗0.2+ 4 e−0.8 n +
2
=−1.054
h 0.2
y i+1 = y i+ ( k 1 +2∗k 2 +2∗k 3 +k 4 )=1.423+ (−1.049−4∗1.054−1.06 )=1.212
6 6
dz − y z 2 dz −4 x e−x ∗z2
= ≫≫ ≫ =
dx 3 dx 9
when x = 0 , z(0) = 4
2
−4 x e−x∗z 2 −4 (0) e ∗( 4)
−0
l 1=f ( x , z ) = = =0
9 9
0.2 0.2 ( ) 2
h h (
− 4 ( 0 ) e−0+
2 )(
∗ 4+
2
0 )
(
l 2=f x + , z+ l 1 =
2 2 ) 9
=−0.1778
2
0.2 0.2 (
h h (
− 4 ( 0 ) e−0+
2 )(
∗ 4+
2
−1.778 ) )
(
l 3=f x + , z+ l 2 =
2 2 ) 9
=−0.1762
2
−4 x e−x∗z 2 −4 ( 0 ) e + 0.2∗( 4−0.1762 )
−0
l 4 =f ( x +h , z+l 3 ) = = =−0.325
9 9
h 0.2
z i+1=z i + ( l 1 +2∗l 2 +2∗l 3+ l 4 )=4+ (−0−2∗0.1778−2∗0.1762−0.325 )=3.966
6 6
when x = 0.2 , z(0) = 3.966
2
−4 x e−x∗z 2 −4 ( 0.2 ) e ∗( 3.966 )
−0.2
l 1=f ( x , z ) = = =−1.145
9 9
2
0.2 0.2 (
h h (
− 4 ( 0.2 ) e−0.2 +
2 )(
∗ 3.966+
2
−1.145 ) )
(
l 2=f x + , z+ l 1 =
2 2 ) 9
=−1.244
2
0.2 0.2 (
h h (
− 4 ( 0.2 ) e−0.2 +
2 )(
∗ 3.966+
2
−1.244 ) )
(
l 3=f x + , z+ l 2 =
2 2 ) 9
=−1.238
h 0.2
z i+1=z i + ( l 1 +2∗l 2 +2∗l 3+ l 4 )=3.966+ (−1.145−2∗1.244−2∗1238−0.707 )=3.739
6 6
Numerical Methods Course Home work
when x = 0.4 , z(0) = 3.739
2
−4 x e−x∗z 2 −4 ( 0.4 ) e ∗ (3.739 )
−0.4
l 1=f ( x , z ) = = =−1.666
9 9
2
0.2 0.2 (
h h (
− 4 ( 0.4 ) e−0.4 +
2 )(
∗ 3.739+
2
−1.666 ) )
(
l 2=f x + , z+ l 1 =
2 2 ) 9
=−1.663
2
0.2 0.2 (
h h (
− 4 ( 0.4 ) e−0.4 +
2 )(
∗ 3.739+
2
−1.663 ) )
(
l 3=f x + , z+ l 2 =
2 2 ) 9
=−1.664
−0.4 2
−4 x e−x∗z 2 −( 4 ( 0.4 ) e + 0.2 )∗( 3.739−1.664 )
l 4 =f ( x +h , z+l 3 ) = = =−0.609
9 9
h 0.2
z i+1=z i + ( l 1 +2∗l 2 +2∗l 3+ l 4 )=3.739+ (−1.666−2∗1.663−2∗1.664−0.609 )=3.441
6 6
when x = 0.6 , z(0) = 3.441
2
−4 x e−x∗z 2 −4 ( 0.6 ) e ∗( 3.441 )
−0.6
l 1=f ( x , z ) = = =−1.733
9 9
2
0.2 0.2 (
h h (
− 4 ( 0.6 ) e−0.6 +
2 )(
∗ 3.441+
2
−1.733 ) )
(
l 2=f x + , z+ l 1 =
2 2 ) 9
=−1.681
2
0.2 0.2 (
h h (
− 4 ( 0.6 ) e−0.6 +
2 )(
∗ 3.441+
2
−1.681 ) )
(
l 3=f x + , z+ l 2 =
2 2 ) 9
=−1.687
−0.6 2
−4 x e−x∗z 2 −( 4 ( 0.6 ) e +0.2 )∗( 3.441−1.681 )
l 4 =f ( x +h , z+l 3 ) = = =−0.522
9 9
h 0.2
z i+1=z i + ( l 1 +2∗l 2 +2∗l 3+ l 4 )=3.441+ (−1.733−2∗1.681−2∗1.687−0.522 )=3.141
6 6
when x = 0.8 , z(0) = 3.141
2
−4 x e−x∗z 2 −4 ( 0.8 ) e ∗( 3.141 )
−0.8
l 1=f ( x , z ) = = =−1.576
9 9
Numerical Methods Course Home work
2
0.2 0.2 (
h h (
− 4 ( 0.8 ) e−0.8 +
2 )(
∗ 3.141+
2
−1.576 ) )
2(
l 2=f x + , z+ l 1 =
2 ) 9
=−1.521
2
0.2 0.2 (
h h (
− 4 ( 0.8 ) e−0.8+
2 )(
∗ 3.141+
2
−1.521 ) )
2(
l 3=f x + , z+ l 2 =
2 ) 9
=−1.527
−0.8 2
−4 x e−x∗z 2 −( 4 ( 0.8 ) e +0.2 )∗( 3.141−1.527 )
l 4 =f ( x +h , z+l 3 ) = = =−0.474
9 9
h 0.2
z i+1=z i + ( l 1 +2∗l 2 +2∗l 3+ l 4 )=3.141+ (−1.576−2∗1.521−2∗1.527−0.474 )=2.87
6 6
x y z
0 2 4
0.2 2 4
0.4 1.855 3.77
0.6 1.65 3.43
0.8 1.429 3.08
1 1.22 2.78
4.5
3.5
2.5 y1
Z1
2 y2
Z2
1.5
0.5
0
0 0.25 0.5 0.75 1 1.25