DYNAMIC MODELS
6.3 The Euler Method
One of the reasons we simulate dynamic models is to obtain accurate quanti-tative information about system behavior. For some applications the simple simulation techniques of the previous section are too imprecise. More sophisti-cated numerical analysis techniques are available, however, that can be used to provide accurate solutions to initial value problems for almost any differential equation model. In this section we present the simplest generally useful method for solving systems of differential equations to any desired degree of accuracy.
Example 6.3. Reconsider the RLC circuit problem of Example 5.4 in the previous chapter. Describe the behavior of this circuit.
Our analysis in Section 5.3 was successful only in determining the local behavior of the dynamical system
x′1= x1− x31− x2
x′2= x1
(6.8)
in the neighborhood of (0, 0), which is the only equilibrium of this system. The equilibrium is unstable, with nearby solution curves spiraling counterclockwise and outward. A sketch of the vector field (see Fig. 5.11) reveals little new information. There is a general counterclockwise rotation to the flow, but it is hard to tell whether solution curves spiral inward, outward, or neither, in the absence of additional information.
We will use the Euler method to simulate the dynamical system in Eq. (6.8).
Figure 6.19 gives an algorithm for the Euler method. Consider a continuous–
time dynamical system model
x′ = F (x)
with x = (x1, . . . , xn) and F = (f1, . . . , fn), along with the initial condition x(t0) = x0.
Starting from this initial condition, at each iteration the Euler method pro-duces an estimate of x(t + h) based on the current estimate of x(t), using the fact that
x(t + h)− x(t) ≈ h F (x(t)).
The accuracy of the Euler method increases as the step size h becomes smaller;
i.e., as the number of steps N becomes larger. For small h the error in the estimate x(N ) of the final value of the state variable x is roughly proportional to h. In other words, using twice as many steps (i.e., reducing h by half) produces results twice as accurate.
Figures 6.20 and 6.21 illustrate the results obtained by applying a computer implementation of the Euler method to Eq. (6.8). Each graph in Figs. 6.20 and 6.21 is the result of several simulation runs. For each set of initial conditions, we need to perform a sensitivity analysis on the input parameters T and N .
First, we enlarged T until any further enlargements produced essentially the same picture (the solution just cycled around a few more times). Then
Algorithm: THE EULER METHOD Variables: t(n) = time after n steps
x1(n) = first state variable at time t(n) x2(n) = second state variable at time t(n) N = number of steps
T = time to end simulation Input: t(0), x1(0), x2(0), N, T Process: Begin
h← (T − t(0))/N for n = 0 to N− 1 do Begin
x1(n + 1)← x1(n) + hf1(x1(n), x2(n)) x2(n + 1)← x2(n) + hf2(x1(n), x2(n)) t(n + 1)← t(n) + h
End End
Output: t(1), . . . , t(n) x1(1), . . . , x1(N ) x2(1), . . . , x2(N )
Figure 6.19: Pseudocode for the Euler method.
we enlarged N (i.e., decreased the step size) to check accuracy. If doubling N produced a graph that was indistinguishable from the one before, we judged that N was large enough for our purposes.
In Fig. 6.20 we started at x1(0) =−1, x2(0) =−1.5. The resulting solution curve spirals in toward the origin, with a counterclockwise rotation. However, before it gets too close to the origin, the solution settles into a more–or–less periodic behavior, cycling around the origin. When we start nearer the origin in Fig. 6.21, the same behavior occurs, except now the solution curve spirals outward. In both cases the solution approaches the same closed loop around the origin. This closed loop is called a limit cycle.
Figure 6.22 shows the complete phase portrait for this dynamical system.
For any initial condition except (x1, x2) = (0, 0), the solution curve tends to the same limit cycle. If we begin inside the loop, the curve spirals outward; if we begin outside the loop, the curve moves inward. The kind of behavior we see in Fig. 6.22 is a phenomenon that cannot occur in a linear dynamical system.
If a solution to a linear dynamical system spirals in toward the origin, it must spiral all the way into the origin. If it spirals outward, then it spirals all the way out to infinity. This observation has modeling implications, of course. Any dynamical system exhibiting the kind of behavior shown in Fig. 6.22 cannot be
-2 -1.5 -1 -0.5 0 0.5 1 1.5 2
x2
-1.5 -1 -0.5 0 0.5 1 1.5
x1
Figure 6.20: Graph of voltage x2versus current x1for the nonlinear RLC circuit problem: case x1(0) =−1.0, x2(0) =−1.5.
-2 -1.5 -1 -0.5 0 0.5 1 1.5 2
x2
-1.5 -1 -0.5 0 0.5 1 1.5
x1
Figure 6.21: Graph of voltage x2versus current x1for the nonlinear RLC circuit problem: case x1(0) = 0.1, x2(0) = 0.3.
-3 -2 -1 0 1 2 3
x2
-2 -1 0 1 2
x1
Figure 6.22: Graph of voltage x2 versus current x1 showing the complete phase portrait for the nonlinear RLC circuit problem of Example 6.3.
modeled adequately using linear differential equations.
The graphs in Figures 6.20–6.22 were produced using a spreadsheet imple-mentation of the Euler method. The advantage of a spreadsheet impleimple-mentation is that the computations and graphics are both performed on the same plat-form, and the results of changing initial conditions can be observed instantly.
A simple computer program to implement this algorithm is effective, but the output is harder to interpret without graphics. Many graphing calculators and computer algebra systems also have built–in differential equation solvers, most of which are based on some variation of the Euler method. The Runge–Kutta method is one variation that uses a more sophisticated interpolation between x(t) and x(t + h); see Exercise 21 at the end of this chapter. No matter what kind of numerical method you use to solve differential equations, be sure to check your results by performing a sensitivity analysis on the parameters that control accuracy. Even the most sophisticated algorithms can produce serious errors unless they are used with care.
Next, we will perform a sensitivity analysis to determine the effect of small changes in our assumptions on our general conclusions. Here we will discuss the sensitivity to the capacitance C. Some additional questions of sensitivity and robustness are relegated to the exercises at the end of this chapter. In our example we assumed that C = 1. In the more general case we obtain the
dynamical system
x′1= x1− x31− x2
x′2=x1
C. (6.9)
For any value of C > 0, the vector field is essentially the same as in Fig. 5.11.
The velocity vectors are vertical on the curve x2 = x1− x31 and horizontal on the x2 axis. The only equilibrium is the origin, (0, 0).
The matrix of partial derivatives is A =
which approximates the behavior of our nonlinear system near the origin. To obtain the eigenvalues, we must solve
λ− 1 1
As long as 0 < C < 4, the quantity under the radical is negative, so we have two complex conjugate eigenvalues with positive real parts, making the origin an unstable equilibrium.
Next, we need to consider the phase portrait for the linear system. It is possible to solve the system in Eq. (6.10) in general by using the method of eigenvalues and eigenvectors, although it would be rather messy. Fortunately, in the present case it is not really necessary to determine a formula for the exact analytical solution to Eq. (6.10) in order to draw the phase portrait. We already know that the eigenvalues of this system are of the form λ = a± ib, where a is positive. As we mentioned previously (in Section 5.1, during the discussion of step 2 for Example 5.1), this implies that the coordinates of any solution curve must be linear combinations of the two terms eatcos(bt) and eatsin(bt). In other words, every solution curve spirals outward. A cursory examination of the vector field for Eq. (6.10) tells us that the spirals must rotate counterclockwise. We thus see that for any 0 < C < 4, the phase portrait of the linear system in Eq.
(6.10) looks much like the one in Fig. 5.10.
Our examination of the linear system in Eq. (6.10) shows that the behavior of the nonlinear system in the neighborhood of the origin must be essentially
-2 -1 0 1 2
x2
-1.5 -1 -0.5 0 0.5 1 1.5
x1
Figure 6.23: Graph of voltage x2versus current x1for the nonlinear RLC circuit problem: case x1(0) = 0.1, x2(0) = 0.3, C = 0.5.
the same as in Fig. 6.22 for any value of C near the baseline case C = 1. To see what happens farther away from the origin, we need to simulate. Figures 6.23 through 6.26 show the results of simulating the dynamical system in Eq.
(6.9) using the Euler method for several different values of C near 1. In each simulation run we started at the same initial condition as in Fig. 6.21.
In each case the solution curve spirals outward and is gradually attracted to a limit cycle. The limit cycle gets smaller as C increases. Several different initial conditions were used for each value of C tested (additional simulation runs are not shown). In each case, apparently, a single limit cycle attracts every solution curve away from the origin. We conclude that the RLC circuit of Example 6.3 has the behavior shown in Fig. 6.22 regardless of the exact value of the capacitance C, assuming that C is close to 1.