This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
1
CHAPTER 25
25.1 The analytical solution can be derived by separation of variables
Substituting the initial conditions yields C = 0. Taking the exponential gives the final result
The result can be plotted as
0
1
2
0 1 2
25.2 Euler’s method with h = 0.5
x y dy/dx0 1 -1.10.5 0.45 -0.38251 0.25875 -0.025881.5 0.245813 0.2826842 0.387155 1.122749
Euler’s method with h = 0.25 gives
x y dy/dx0 1 -1.10.25 0.725 -0.752190.5 0.536953 -0.456410.75 0.422851 -0.227281 0.36603 -0.03661.25 0.356879 0.1650571.5 0.398143 0.457865
The results can be plotted along with the analytical solution as
0
1
2
0 1 2
25.3 For Heun’s 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
The slope at the end of the interval can be computed as
which can be averaged with the initial slope to predict
Both methods are plotted on the same graph below. Notice how Euler’s method (particularly for z) is not very accurate for this step size. The 4th-order RK is much closer to the exact solution.
These can then be used to compute the 4th-order prediction
along with a fifth-order formula:
The error estimate is obtained by subtracting these two equations to give
25.15Option Explicit
Sub EulerTest()Dim i As Integer, m As IntegerDim xi As Double, yi As Double, xf As Double, dx As Double, xout As DoubleDim xp(200) As Double, yp(200) As Double'Assign valuesyi = 1xi = 0xf = 4dx = 0.5xout = 0.5'Perform numerical Integration of ODECall ODESolver(xi, yi, xf, dx, xout, xp, yp, m)'Display resultsSheets("Sheet1").SelectRange("a5:b205").ClearContentsRange("a5").SelectFor i = 0 To m ActiveCell.Value = xp(i) ActiveCell.Offset(0, 1).Select ActiveCell.Value = yp(i) ActiveCell.Offset(1, -1).SelectNext iRange("a5").SelectEnd Sub
Sub ODESolver(xi, yi, xf, dx, xout, xp, yp, m)'Generate an array that holds the solutionDim x As Double, y As Double, xend As DoubleDim h As Doublem = 0xp(m) = xiyp(m) = yix = xiy = yiDo '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 DoLoopEnd Sub
Sub Integrator(x, y, h, xend)Dim ynew As DoubleDo 'Calculation loop If (xend - x < h) Then h = xend - x 'Trim step if increment exceeds end Call Euler(x, y, h, ynew) y = ynew If (x >= xend) Then Exit DoLoopEnd Sub
Sub Euler(x, y, h, ynew)Dim dydx As Double'Implement Euler's methodCall Derivs(x, y, dydx)ynew = y + dydx * hx = x + hEnd Sub
Sub Derivs(x, y, dydx)'Define ODEdydx = -2 * x ^ 3 + 12 * x ^ 2 - 20 * x + 8.5End Sub
25.16 Example 25.1:
Example 25.4 (nonlinear model). Change time steps and initial conditions to
Sub Derivs(t, v, dvdt)'Define ODEdvdt = 9.8 - 12.5 / 68.1 * (v + 8.3 * (v / 46) ^ 2.2)End Sub
25.17Option Explicit
Sub Heun()Dim maxit As Integer, es As DoubleDim n As Integer, m As Integer, i As Integer, iter As IntegerDim xi As Double, xf As Double, yi As Double, h As DoubleDim x As Double, y As Double, y2 As Double, y2old As DoubleDim k1 As Double, k2 As Double, slope As DoubleDim xp(1000) As Double, yp(1000) As Double, itr(1000) As IntegerDim ea As Doublemaxit = 15: es = 0.0000001xi = 0: xf = 4: yi = 2n = 4h = (xf - xi) / nx = xiy = yim = 0xp(m) = xyp(m) = yFor 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 = y2Next iSheets("Heun").SelectRange("a5:b1005").ClearContentsRange("a5").SelectFor 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).SelectNext iRange("a5").SelectEnd Sub
Sub Derivs(x, y, dydx)'Define ODEdydx = 4 * Exp(0.8 * x) - 0.5 * yEnd Sub
25.18Option Explicit
Sub RK4Test()Dim i As Integer, m As IntegerDim xi As Double, yi As Double, xf As Double, dx As Double, xout As DoubleDim xp(200) As Double, yp(200) As Double'Assign valuesyi = 1xi = 0xf = 4dx = 0.5xout = 0.5'Perform numerical Integration of ODECall ODESolver(xi, yi, xf, dx, xout, xp, yp, m)'Display resultsSheets("Sheet1").SelectRange("a5:b205").ClearContentsRange("a5").SelectFor i = 0 To m
Sub ODESolver(xi, yi, xf, dx, xout, xp, yp, m)'Generate an array that holds the solutionDim x As Double, y As Double, xend As DoubleDim h As Doublem = 0xp(m) = xiyp(m) = yix = xiy = yiDo '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 DoLoopEnd Sub
Sub Integrator(x, y, h, xend)Dim ynew As DoubleDo 'Calculation loop If (xend - x < h) Then h = xend - x 'Trim step if increment exceeds end Call RK4(x, y, h, ynew) y = ynew If (x >= xend) Then Exit DoLoopEnd Sub
Sub RK4(x, y, h, ynew)'Implement RK4 methodDim k1 As Double, k2 As Double, k3 As Double, k4 As DoubleDim ym As Double, ye As Double, slope As DoubleCall Derivs(x, y, k1)ym = y + k1 * h / 2Call Derivs(x + h / 2, ym, k2)ym = y + k2 * h / 2Call Derivs(x + h / 2, ym, k3)ye = y + k3 * hCall Derivs(x + h, ye, k4)slope = (k1 + 2 * (k2 + k3) + k4) / 6ynew = y + slope * hx = x + hEnd Sub
Sub Derivs(x, y, dydx)'Define ODEdydx = -2 * x ^ 3 + 12 * x ^ 2 - 20 * x + 8.5End Sub
Sub Derivs(x, y, dydx)'Define ODEdydx = 4 * Exp(0.8 * x) - 0.5 * yEnd Sub
25.19Option Explicit
Sub RK4SysTest()Dim i As Integer, m As Integer, n As Integer, j As IntegerDim xi As Double, yi(10) As Double, xf As DoubleDim dx As Double, xout As DoubleDim xp(200) As Double, yp(200, 10) As Double'Assign valuesn = 2xi = 0xf = 2yi(1) = 4yi(2) = 6dx = 0.5xout = 0.5'Perform numerical Integration of ODECall ODESolver(xi, yi, xf, dx, xout, xp, yp, m, n)
'Display resultsSheets("Sheet1").SelectRange("a5:n205").ClearContentsRange("a5").SelectFor 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).SelectNext iRange("a5").SelectEnd Sub
Sub ODESolver(xi, yi, xf, dx, xout, xp, yp, m, n)'Generate an array that holds the solutionDim i As IntegerDim x As Double, y(10) As Double, xend As DoubleDim h As Doublem = 0x = xi'set initial conditionsFor i = 1 To n y(i) = yi(i)Next i'save output valuesxp(m) = xFor i = 1 To n yp(m, i) = y(i)Next iDo '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 DoLoopEnd Sub
Sub Integrator(x, y, h, n, xend)Dim j As IntegerDim ynew(10) As DoubleDo 'Calculation loop If (xend - x < h) Then h = xend - x 'Trim step if increment exceeds end Call RK4Sys(x, y, h, n, ynew) For j = 1 To n y(j) = ynew(j) Next j If (x >= xend) Then Exit DoLoopEnd Sub
Dim ym(10) As Double, ye(10) As DoubleDim k1(10) As Double, k2(10) As Double, k3(10) As Double, k4(10) As DoubleDim slope(10)'Implement RK4 method for systems of ODEsCall Derivs(x, y, k1)For j = 1 To n ym(j) = y(j) + k1(j) * h / 2Next jCall Derivs(x + h / 2, ym, k2)For j = 1 To n ym(j) = y(j) + k2(j) * h / 2Next jCall Derivs(x + h / 2, ym, k3)For j = 1 To n ye(j) = y(j) + k3(j) * hNext jCall Derivs(x + h, ye, k4)For j = 1 To n slope(j) = (k1(j) + 2 * (k2(j) + k3(j)) + k4(j)) / 6Next jFor j = 1 To n ynew(j) = y(j) + slope(j) * hNext jx = x + h
End Sub
Sub Derivs(x, y, dydx)'Define ODEdydx(1) = -0.5 * y(1)dydx(2) = 4 - 0.3 * y(2) - 0.1 * y(1)End Sub
Application to Example 25.10:
25.20 Main Program:
%Damped spring mass system%mass: m=10 kg%damping: c=5,40,200 N/(m/s)%spring: k=40 N/m% MATLAB 5 version%Independent Variable t, tspan=[tstart tstop]%initial conditions [x(1)=velocity, x(2)=displacement];
tspan=[0,15]';x0=[6,1.5]';[t,x]=ode45('dxdt',tspan,x0);plot(t,x(:,1),t,x(:,2),'--')gridtitle('Displacement and Velocity Versus Time')xlabel('Time, t')ylabel('Displacement and Velocity')
function dx=dxdt(t,x)dx=[x(2);-5*x(1)*x(2)+(x(1)+7)*sin(1*t)];
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 4th-order RK method indicates that the solution reaches the ground between t = 20.2 and 20.4 s.
-500
0
500
1000
1500
0 5 10 15 20 25
25.24 The volume of the tank can be computed as
(1)
This equation cannot be solved because it has 2 unknowns: V and H. The volume is related to the depth of liquid by
(2)
Equation (2) can be differentiated to give
(3)
This result can be substituted into Eq. (1) to give and equation with 1 unknown,
(4)
The area of the orifice can be computed as
Substituting this value along with the other parameters (C = 0.55, g = 9.81, r = 1.5) into Eq. (4) gives
(5)
We can solve this equation with an initial condition of H = 2.75 m using the 4th-order RK method with a step size of 6 s. If this is done, the result can be plotted as shown,
t v x dv/dt dx/dt0 1400 0 -9.81 14001 1390.19 1400 -9.80569 1390.192 1380.384 2790.19 -9.80141 1380.3843 1370.583 4170.574 -9.79717 1370.5834 1360.786 5541.157 -9.79296 1360.7865 1350.993 6901.943 -9.78878 1350.993
The entire solution for height can be plotted as
0
40000
80000
120000
0 50 100 150 200 250
The maximum height occurs at about 146 s.
25.27 First, we must recognize that the evaluation of the definite integral
is equivalent to solving the differential equation
for y(b) given the initial condition y(a) = 0. Thus, we must solve the following initial-value problem:
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;