Beruflich Dokumente
Kultur Dokumente
CHAPTER 25
25.1 The analytical solution can be derived by separation of variables
dy
ln y
1.1 dx
x3
1.1x C
3
Substituting the initial conditions yields C = 0. Taking the exponential gives the final result
ye
x3
1.1 x
3
25.3 For Heuns method, the value of the slope at x = 0 can be computed as 1.1 which can be
used to compute the value of y at the end of the interval as
y (0.5) 1 (0 1.1(1))0.5 0.45
1.1 0.3825
0.5 0.629375
2
yij
0.45
0.629375
0.5912578
a
28.5%
6.45%
2
3
4
0.5993577
0.5976365
1.35%
0.288%
yi
1.0000000
0.5976365
0.4590875
0.6269127
2.8784358
The results along with the analytical solution are displayed below:
y
1
0.60157
0.464524
0.59138
1.584452
k1
-1.1
-0.51133
-0.04645
0.680087
4.594911
ym
0.725
0.473737
0.452911
0.761402
2.73318
k2
-0.75219
-0.25463
0.209471
1.494252
10.83023
ym
0.811953
0.537912
0.516892
0.964943
4.292008
k3
-0.8424
-0.28913
0.239062
1.893701
17.00708
ye
0.578799
0.457006
0.584055
1.538231
10.08799
k4
-0.49198
-0.0457
0.671663
4.460869
51.95317
-0.79686
-0.27409
0.253713
1.986144
18.70378
dy
z
dt
dz
0 .5 x y
dt
y (0)=2
z (0)=0
dy/dx
dz/dx
0
0.1
0.2
0.3
0.4
0.5
2
2
1.98
1.9405
1.8822
1.805995
0
-0.2
-0.395
-0.583
-0.76205
-0.93027
0
-0.2
-0.395
-0.583
-0.76205
-0.93027
-2
-1.95
-1.88
-1.7905
-1.6822
-1.556
(b) For Heun (without iterating the corrector) the first few steps are
x
y
0
2
0.1
1.99
0.2 1.96055
0.3 1.912446
0.4 1.846671
0.5 1.764385
z
0
-0.1975
-0.38801
-0.56963
-0.74052
-0.89899
dy/dx
0
-0.1975
-0.38801
-0.56963
-0.74052
-0.89899
dz/dx
-2
-1.94
-1.86055
-1.76245
-1.64667
-1.51439
yend
2
1.97025
1.921749
1.855483
1.772619
1.674486
zend
-0.2
-0.3915
-0.57407
-0.74587
-0.90519
-1.05043
dy/dx
-0.2
-0.3915
-0.57407
-0.74587
-0.90519
-1.05043
dz/dx
ave slope
-1.95
-0.1
-1.87025 -0.2945
-1.77175 -0.48104
-1.65548 -0.65775
-1.52262 -0.82286
-1.37449 -0.97471
Heun
y
0
0
Euler
-2
25.9 (a) The Heun method without iteration can be implemented as in the following table:
t
0
0.1
0.2
0.3
0.4
0.5
y
1
1.00005
1.000492
1.002176
1.006436
1.014986
k1
0
0.000995
0.007845
0.025865
0.059434
0.111847
yend
1
1.000149
1.001276
1.004762
1.012379
1.02617
k2
0.000995
0.007843
0.025841
0.059335
0.11156
0.184731
0.000498
0.004419
0.016843
0.0426
0.085497
0.148289
4
2.9 3.784421 0.051826 3.789604 0.01065 0.031238
3 3.787545 0.010644 3.78861 0.000272 0.005458
(b) The Ralston 2nd order RK method can be implemented as in the following table:
0.00028
0.003851
0.015977
0.041508
0.084254
0.146959
t
0
0.1
0.2
0.3
0.4
0.5
2.9
3
y
1
1.000028
1.000413
1.002011
1.006162
1.014587
k1
0
0.000995
0.007845
0.02586
0.059418
0.111803
yint
1
1.000103
1.001001
1.00395
1.010618
1.022972
k2
0.000421
0.005278
0.020043
0.049332
0.096672
0.164537
Both methods are displayed on the following plot along with the exact solution,
ye
cos t
cos 3 t 2
3
3
Note that both methods agree closely with the exact solution.
Exact
Ralston
Heun
2
0
0
y
2.0000
2.0000
1.8550
1.6492
1.4286
1.2166
z
4.0000
1.8667
1.4021
1.1590
1.0113
0.9139
dy/dx dz/dx
0.00 -10.67
-0.73 -2.32
-1.03 -1.22
-1.10 -0.74
-1.06 -0.49
-0.96 -0.34
(b) 4th-order RK
x
y
0 2.000
0.2 1.934
z
k11
k12
k21
k22
k31
k32
k41
k42
1
2
4.000 0.000 -10.667 -0.381 -5.736 -0.305 -7.678 -0.603 -3.926 -0.329 -6.903
2.619 -0.594 -4.423 -0.786 -2.962 -0.748 -3.338 -0.888 -2.266 -0.758 -3.215
5
0.4
0.6
0.8
1
1.783
1.593
1.393
1.201
1.976
1.617
1.392
1.243
-0.884
-0.990
-0.990
-0.930
-2.321
-1.387
-0.901
-0.618
-0.962
-1.001
-0.963
-0.884
-1.718
-1.087
-0.732
-0.515
-0.947
-0.999
-0.968
-0.893
-1.830
-1.131
-0.753
-0.526
-0.991
-0.989
-0.928
-0.840
-1.377
-0.897
-0.617
-0.441
-0.949
-0.997
-0.963
-0.887
-1.799
-1.120
-0.748
-0.524
Both methods are plotted on the same graph below. Notice how Eulers method (particularly
for z) is not very accurate for this step size. The 4th-order RK is much closer to the exact
solution.
5
Euler
3
2
4th-order RK
0
0
0.2
0.4
0.6
0.8
yscale = 2 + (2(3)) = 8
new = 0.001(8) = 0.008
Because present > new, decrease step.
25.15
Option Explicit
Sub EulerTest()
Dim i As Integer, m As Integer
Dim xi As Double, yi As Double, xf As Double, dx As Double, xout As Double
Dim xp(200) As Double, yp(200) As Double
'Assign values
yi = 1
xi = 0
xf = 4
dx = 0.5
xout = 0.5
'Perform numerical Integration of ODE
Call ODESolver(xi, yi, xf, dx, xout, xp, yp, m)
'Display results
Sheets("Sheet1").Select
Range("a5:b205").ClearContents
Range("a5").Select
For i = 0 To m
ActiveCell.Value = xp(i)
ActiveCell.Offset(0, 1).Select
6
ActiveCell.Value = yp(i)
ActiveCell.Offset(1, -1).Select
Next i
Range("a5").Select
End Sub
Sub ODESolver(xi, yi, xf, dx, xout, xp, yp, m)
'Generate an array that holds the solution
Dim x As Double, y As Double, xend As Double
Dim h As Double
m = 0
xp(m) = xi
yp(m) = yi
x = xi
y = yi
Do
'Print loop
xend = x + xout
If (xend > xf) Then xend = xf 'Trim step if increment exceeds end
h = dx
Call Integrator(x, y, h, xend)
m = m + 1
xp(m) = x
yp(m) = y
If (x >= xf) Then Exit Do
Loop
End Sub
Sub Integrator(x, y, h, xend)
Dim ynew As Double
Do
'Calculation loop
If (xend - x < h) Then h = xend - x
Call Euler(x, y, h, ynew)
y = ynew
If (x >= xend) Then Exit Do
Loop
End Sub
25.17
Option Explicit
Sub Heun()
Dim maxit As Integer, es As Double
Dim n As Integer, m As Integer, i As Integer, iter As Integer
Dim xi As Double, xf As Double, yi As Double, h As Double
Dim x As Double, y As Double, y2 As Double, y2old As Double
Dim k1 As Double, k2 As Double, slope As Double
Dim xp(1000) As Double, yp(1000) As Double, itr(1000) As Integer
Dim ea As Double
maxit = 15: es = 0.0000001
xi = 0: xf = 4: yi = 2
7
n = 4
h = (xf - xi) / n
x = xi
y = yi
m = 0
xp(m) = x
yp(m) = y
For i = 1 To n
Call Derivs(x, y, k1)
y2 = y + k1 * h
iter = 0
Do
y2old = y2
Call Derivs(x + h, y2, k2)
slope = (k1 + k2) / 2
y2 = y + slope * h
iter = iter + 1
ea = Abs((y2 - y2old) / y2) * 100
If ea < es Or iter >= maxit Then Exit Do
Loop
m = m + 1
x = x + h
xp(m) = x
yp(m) = y2
itr(m) = iter
y = y2
Next i
Sheets("Heun").Select
Range("a5:b1005").ClearContents
Range("a5").Select
For i = 0 To m
ActiveCell.Value = xp(i)
ActiveCell.Offset(0, 1).Select
ActiveCell.Value = yp(i)
ActiveCell.Offset(0, 1).Select
ActiveCell.Value = itr(i)
ActiveCell.Offset(1, -2).Select
Next i
Range("a5").Select
End Sub
Sub Derivs(x, y, dydx)
'Define ODE
dydx = 4 * Exp(0.8 * x) - 0.5 * y
End Sub
25.19
Option Explicit
Sub RK4SysTest()
Dim i As Integer, m As Integer, n As Integer, j As Integer
Dim xi As Double, yi(10) As Double, xf As Double
Dim dx As Double, xout As Double
Dim xp(200) As Double, yp(200, 10) As Double
'Assign values
n = 2
xi = 0
xf = 2
yi(1) = 4
yi(2) = 6
dx = 0.5
xout = 0.5
'Perform numerical Integration of ODE
Call ODESolver(xi, yi, xf, dx, xout, xp, yp, m, n)
'Display results
Sheets("Sheet1").Select
Range("a5:n205").ClearContents
Range("a5").Select
For i = 0 To m
ActiveCell.Value = xp(i)
For j = 1 To n
ActiveCell.Offset(0, 1).Select
ActiveCell.Value = yp(i, j)
Next j
ActiveCell.Offset(1, -n).Select
Next i
Range("a5").Select
End Sub
Sub ODESolver(xi, yi, xf, dx, xout, xp, yp, m, n)
'Generate an array that holds the solution
Dim i As Integer
Dim x As Double, y(10) As Double, xend As Double
Dim h As Double
m = 0
x = xi
'set initial conditions
For i = 1 To n
y(i) = yi(i)
Next i
'save output values
xp(m) = x
For i = 1 To n
yp(m, i) = y(i)
Next i
Do
'Print loop
xend = x + xout
If (xend > xf) Then xend = xf 'Trim step if increment exceeds end
h = dx
Call Integrator(x, y, h, n, xend)
m = m + 1
'save output values
xp(m) = x
For i = 1 To n
yp(m, i) = y(i)
Next i
If (x >= xf) Then Exit Do
Loop
End Sub
Sub Integrator(x, y, h, n, xend)
9
Dim j As Integer
Dim ynew(10) As Double
Do
'Calculation loop
If (xend - x < h) Then h = xend - x
Call RK4Sys(x, y, h, n, ynew)
For j = 1 To n
y(j) = ynew(j)
Next j
If (x >= xend) Then Exit Do
Loop
End Sub
10
25.21 The Matlab program shown below performs the Euler Method and displays the time just
before the cylindrical tank empties (y < 0).
%prob2521.m
dt=0.5;
t=0;
y=3;
i=1;
while(1)
y=y+dydt(t,y)*dt;
if y<0, break, end
t=t+dt;
i=i+1;
end
function dy=dydt(t,y);
dy=-0.06*sqrt(y);
c
dv
g d v2
dt
m
dx
v
dt
(a) Here are the first few steps of Eulers method with a step size of h = 0.2.
t
0
0.2
0.4
0.6
0.8
1
x
1000
1000
999.6076
998.8232
997.6479
996.0837
dx/dt
0
1.962
3.922075
5.876384
7.821118
9.752533
0
-1.962
-3.92208
-5.87638
-7.82112
-9.75253
dv/dt
9.81
9.800376
9.771543
9.72367
9.657075
9.57222
(b) Here are the results of the first few steps of the 4 th-order RK method with a step size of h
= 0.2.
t
0
0.2
0.4
0.6
0.8
1
x
1000
999.8038
999.2157
998.2368
996.869
995.1149
v
0
1.961359
3.918875
5.868738
7.807195
9.730582
11
The results for x of both methods are displayed graphically on the following plots. Because
the step size is sufficiently small the results are in close agreement. Both indicate that the
parachutist would hit the ground at a little after 20 s. The more accurate 4 th-order RK method
indicates that the solution reaches the ground between t = 20.2 and 20.4 s.
25.25 The solution can be obtained with the 4th-order RK method with a step size of 1. Here are
the results of the first few steps.
x
1950
1951
1952
1953
1954
1955
y
2555
2607.676
2661.132
2715.368
2770.38
2826.168
25.27 First, we must recognize that the evaluation of the definite integral
I
b
a
f ( x ) dx
12
dy
f (x )
dx
for y(b) given the initial condition y(a) = 0. Thus, we must solve the following initial-value
problem:
dy
1
1
6
2
dx ( x 0.3) 0.01 ( x 0.9) 2 0.04
where y(0) = 0. We can do this in a number of ways. One convenient approach is to use the
MATLAB function ode45 which implements an adaptive RK method. To do this, we must
first set up an M-file to evaluate the right-hand side of the differential equation,
function dy = humpsODE(x,y)
dy = 1./((x-0.3).^2 + 0.01) + 1./((x-0.9).^2 + 0.04) - 6;