Numerical Integration of Ordinary Differential
6.4 Modified Euler Method with Predictor-Corrector Algorithm
The modified Euler method is a more accurate way for calculating the value of yi+1 than the simple linear approximation of the Euler method in the previous sec-tion. Again, we consider a differential equation of standard form y = f x y( , ) with initial condition y(0)=YI , and again we subdivide the x domain into N sub- divisions and march in the x direction. We saw that in the Euler method, tak-ing yi+1= +y hy y hf x yi i= +i ( , )i i overshoots the true value of yi+1 (as shown in Figure 6.2). Now, suppose we were able to determine y x( i+1), which would be the slope to the curve at xi+1. If we were to predict yi+1 by using y x( i+1) in Equation 6.3, that is,
yi+1= +y h yi i+1= +y h f xi ( i+1,yi+1) (6.5) then we would undershoot the true value of yi+1. This is shown in Figure 6.3. Here, we have constructed a tangent to the curve at yi+1 and drawn a parallel line passing
yi
xi xi+1
yi+1 y
x Slope = y´i
Slope = y´i+1
yi + hy´i+1 yi + hy´i
h
Figure 6.3 In the modified Euler method, we use the average of two straight-line approximations to predict yi+1.
through point ( , )x yi i to obtain the predicted value for yi+1. Since using yi in Equation (6.3) overshoots the true value of yi+1 and using yi+1 in Equation (6.3) undershoots the true value of yi+1, we see that a better estimate for yi+1 would be obtained by using an average of the two derivatives in Equation (6.3), that is,
= + +
+ +
1 2 1
y y h y y
i i i i
(6.6)
Unfortunately, Equation (6.6) is no longer explicit because we do not know the value of yi+1. The use of Equation (6.6) in solving the differential Equation (6.1) is an example of an implicit method. However, we can approximate a value for yi+1 by using the predictor-corrector method. To apply this method, we rewrite Equation (6.6) as follows:
= + +
( )
+ +
1 2 1
y y h y y
iC
i i i P
(6.7)
where the P superscript indicates the predicted value, and the C superscript indicates the corrected value. Equation (6.7) is called the corrector equation and can be used to iteratively calculate the value for yi+1. Substituting Equation (6.1) into (6.7) gives
yiC+1=yi+2h
(
f x y( , )i i + f x( i+1,yiP+1))
(6.8)The predictor-corrector technique proceeds as follows:
1. Use the Euler method to determine a first predicted value for yi+1, called yiP11,
+ that is,
yiP+11= +y h f x y( , )
i i i
2. Calculate the first corrected value yiC+11
by using yiP11
+ in Equation (6.8).
3. Use yiC+11
as the new predicted value yiP21
+ in Equation (6.8).
4. Calculate a new corrected value yiC+12
5. Repeat steps 3 and 4 until yiC+1+1−y.+1 < ε
iC
n n , where ε is an error tolerance that depends on the desired accuracy and is typically a fraction of a percent of the last corrected value, for example, ε =0.01%× yiC+n1.
Example 6.1
In this example, we find the exact solution of a first-order differential equa-tion and then compare with the corresponding soluequa-tions found by the Euler and modified Euler methods.
A simple RC circuit is “driven” by a voltage source VD as shown in Figure 6.4. Writing Kirchhoff’s voltage law around the loop gives
− − =
VD vR vC 0 (6.9)
Applying Ohm’s law (vR=iR) and the constituent relation for capacitor current i C= dv
dt
C , we can rewrite Equation (6.9) as
− − =
V RCdv dt v
D C
C 0
Rearranging gives
+τ = τ dv
dtC v V
C D
1 (6.10)
where τ = RC is the time constant for the RC circuit. Equation (6.10) is a first-order differential equation of vC with respect to time. We assume that at t = 0, the initial capacitor voltage is zero, and that the driving voltage is a ramp function, that is V tD( )=V to. We first solve Equation (6.10) analytically so that we can have a basis for comparison with numerical solutions. We begin by guessing a particular solution vC,P having the same form as the driving function:
= α + β
vC P, t (6.11)
where α and β are constants to be determined. Substituting vC,P into Equation (6.10), we obtain
(
α + β +)
τ(
α + β =)
τd
dt t 1 t V to
–+
VD vC
+
− C
R + vR −
Figure 6.4 RC circuit.
which gives
Matching terms between the left and right sides of Equation (6.12), we see that α = Vo and β = − τVo . Thus,
= − τ
vC P, V t Vo o
Next, we find the complementary solution, vC,H, to the homogeneous equation, which is Equation (6.10) when the driving function is zero.
+τ = dv
dtC H 1vC H , 0
, (6.13)
Equation (6.13) is separable. Thus,
= − τ → = −
where γ is a constant to be determined. We now take the complete solution as the sum of the particular and homogeneous solutions:
= +
Applying the initial condition vC(0) 0= to Equation (6.15), we find that γ =Voτ. Thus, the complete solution for the ramp response of an RC circuit
We now solve Equation (6.10) numerically for the following parameter values: R = 1 kΩ, C = 1 μF, and Vo=10 V. First, we rearrange the governing differential equation (6.10) into the form of Equation (6.1):
( ) ( )
= τ − =
τ − =
dv
dtC 1VD vC 1V t vo C f t v( , )C (6.17) Next, we choose a time interval and time step. For the RC circuit, we know that the interesting transient behavior will occur within a few mul-tiples of the time constant, and thus we arbitrarily choose an interval of t = [ 0, 10τ ]. We also divide the interval into 20 steps, and thus h=10τ
20 .
A MATLAB script that carries out the Euler method and the modified Euler method follows:
% Example_6_1.m
fprintf('Warning at step ');
The resulting solutions for Example 6.1 are graphed in Figure 6.5. Note that for N = 20 subdivisions in the time domain, both the Euler and modified Euler solutions are almost indistinguishable from the exact solution.
Figure 6.6 shows the same example where we have increased the time step by reducing N to 6 (accomplished by changing steps = 20 to steps = 6 in the program). Note that in this case, the Euler solution deviates substantially from the exact solution and also oscillates above and below the exact solu-tion. On the other hand, the solution for the modified Euler method remains accurate and stable even for small N. Ideally, we would like to reduce N to as small as possible (to reduce computation time), but at the same time we want to avoid numerical instability. Thus, the modified Euler method is a compro-mise that has some additional computational operations (due to the iterative predictor-corrector algorithm) but yet stays stable for large time steps.
0 0.005 0.01
Vc (volts)
Euler solution compared to exact
20 steps
0 0.005 0.01
Vc (volts)
Modified Euler solution compared to exact
20 steps Euler
Exact
Modified Euler Exact
Figure 6.5 Plot of VC versus t for RC circuit for 20 steps over the interval 0 ≤ t ≤ 10τ: (a) Euler and exact solutions; (b) modified Euler and exact solutions.