STIFFNESS Applied Numerical Methods with MATLAB fo

components. Although the transient phenomena exist for only a short part of the integration interval, they can dictate the time step for the entire solution. Both individual and systems of ODEs can be stiff. An example of a single stiff ODE is d y dt = −1000y + 3000 − 2000e −t 23.17 If y0 = 0, the analytical solution can be developed as y = 3 − 0.998e −1000t − 2.002e −t 23.18 As in Fig. 23.8, the solution is initially dominated by the fast exponential term e –1000t . After a short period t 0.005, this transient dies out and the solution becomes governed by the slow exponential e –t . Insight into the step size required for stability of such a solution can be gained by ex- amining the homogeneous part of Eq. 23.17: d y dt = −ay 23.19 If y0 = y , calculus can be used to determine the solution as y = y e −at Thus, the solution starts at y and asymptotically approaches zero. Euler’s method can be used to solve the same problem numerically: y i +1 = y i + d y i dt h Substituting Eq. 23.19 gives y i +1 = y i − ay i h 3 y 2 1 4 2 t 1 0.02 0.01 FIGURE 23.8 Plot of a stiff solution of a single ODE. Although the solution appears to start at 1 , there is actually a fast transient from y = to 1 that occurs in less than the . 005 time unit. This transient is perceptible only when the response is viewed on the finer timescale in the inset. 23.3 STIFFNESS 603 or y i +1 = y i 1 − ah 23.20 The stability of this formula clearly depends on the step size h. That is, |1 − ah| must be less than 1. Thus, if h 2a, |y i | → ∞ as i → ∞. For the fast transient part of Eq. 23.18, this criterion can be used to show that the step size to maintain stability must be 21000 = 0.002. In addition, we should note that, whereas this criterion maintains stability i.e., a bounded solution, an even smaller step size would be required to obtain an accurate solution. Thus, although the transient occurs for only a small fraction of the integration interval, it controls the maximum allowable step size. Rather than using explicit approaches, implicit methods offer an alternative remedy. Such representations are called implicit because the unknown appears on both sides of the equation. An implicit form of Euler’s method can be developed by evaluating the deriva- tive at the future time: y i + 1 = y i + d y i + 1 dt h This is called the backward, or implicit, Euler’s method. Substituting Eq. 23.19 yields y i + 1 = y i − ay i + 1 h which can be solved for y i + 1 = y i 1 + ah 23.21 For this case, regardless of the size of the step, |y i | → 0 as i → ∞. Hence, the approach is called unconditionally stable. EXAMPLE 23.5 Explicit and Implicit Euler Problem Statement. Use both the explicit and implicit Euler methods to solve Eq. 23.17, where y0 = 0. a Use the explicit Euler with step sizes of 0.0005 and 0.0015 to solve for y between t = 0 and 0.006. b Use the implicit Euler with a step size of 0.05 to solve for y between 0 and 0.4. Solution. a For this problem, the explicit Euler’s method is y i + 1 = y i + − 1000y i + 3000 − 2000e − t i h The result for h = 0.0005 is displayed in Fig. 23.9a along with the analytical solution. Although it exhibits some truncation error, the result captures the general shape of the ana- lytical solution. In contrast, when the step size is increased to a value just below the stabil- ity limit h = 0.0015, the solution manifests oscillations. Using h 0.002 would result in a totally unstable solution—that is, it would go infinite as the solution progressed. b The implicit Euler’s method is y i + 1 = y i + − 1000y i + 1 + 3000 − 2000e − t i + 1 h Now because the ODE is linear, we can rearrange this equation so that y i +1 is isolated on the left-hand side: y i +1 = y i + 3000h − 2000he −t i +1 1 + 1000h The result for h = 0.05 is displayed in Fig. 23.9b along with the analytical solution. Notice that even though we have used a much bigger step size than the one that induced instabil- ity for the explicit Euler, the numerical result tracks nicely on the analytical solution. Systems of ODEs can also be stiff. An example is d y 1 dt = −5y 1 + 3y 2 23.22a d y 2 dt = 100y 1 − 301y 2 23.22b For the initial conditions y 1 = 52.29 and y 2 = 83.82, the exact solution is y 1 = 52.96e −3.9899t − 0.67e −302.0101t 23.23a y 2 = 17.83e −3.9899t + 65.99e −302.0101t 23.23b 1.5 y 1 0.5 0.006 0.004 h ⫽ 0.0015 h ⫽ 0.0005 Exact a t 0.002 2 y 1 0.4 0.3 Exact h ⫽ 0.05 b t 0.2 0.1 FIGURE 23.9 Solution of a stiff ODE with a the explicit and b implicit Euler methods. 23.3 STIFFNESS 605 Note that the exponents are negative and differ by about two orders of magnitude. As with the single equation, it is the large exponents that respond rapidly and are at the heart of the system’s stiffness. An implicit Euler’s method for systems can be formulated for the present example as y 1,i +1 = y 1,i + −5y 1,i +1 + 3y 2,i +1 h 23.24a y 2,i +1 = y 2,i + 100y 1,i +1 − 301y 2,i +1 h 23.24b Collecting terms gives 1 + 5hy 1,i +1 − 3y 2,i +1 = y 1,i 23.25a −100y 1,i +1 + 1 + 301hy 2,i +1 = y 2,i 23.25b Thus, we can see that the problem consists of solving a set of simultaneous equations for each time step. For nonlinear ODEs, the solution becomes even more difficult since it involves solving a system of nonlinear simultaneous equations recall Sec. 12.2. Thus, although stability is gained through implicit approaches, a price is paid in the form of added solution complexity.

23.3.1 MATLAB Functions for Stiff Systems

MATLAB has a number of built-in functions for solving stiff systems of ODEs. These are ode15s . This function is a variable-order solver based on numerical differentiation formulas. It is a multistep solver that optionally uses the Gear backward differentiation formulas. This is used for stiff problems of low to medium accuracy. ode23s . This function is based on a modified Rosenbrock formula of order 2. Because it is a one-step solver, it may be more efficient than ode15s at crude tolerances. It can solve some kinds of stiff problems better than ode15s . ode23t . This function is an implementation of the trapezoidal rule with a “free” inter- polant. This is used for moderately stiff problems with low accuracy where you need a solution without numerical damping. ode23tb . This is an implementation of an implicit Runge-Kutta formula with a first stage that is a trapezoidal rule and a second stage that is a backward differentiation for- mula of order 2. This solver may also be more efficient than ode15s at crude tolerances. EXAMPLE 23.6 MATLAB for Stiff ODEs Problem Statement. The van der Pol equation is a model of an electronic circuit that arose back in the days of vacuum tubes, d 2 y 1 dt 2 − μ 1 − y 2 1 d y 1 dt + y 1 = 0 E23.6.1 The solution to this equation becomes progressively stiffer as μ gets large. Given the ini- tial conditions, y 1 = dy 1 dt = 1, use MATLAB to solve the following two cases: a for μ = 1, use ode45 to solve from t = 0 to 20; and b for μ = 1000, use ode23s to solve from t = 0 to 6000. Solution. a The first step is to convert the second-order ODE into a pair of first-order ODEs by defining d y 1 dt = y 2 Using this equation, Eq. E23.6.1 can be written as d y 2 dt = μ 1 − y 2 1 y 2 − y 1 = 0 An M-file can now be created to hold this pair of differential equations: function yp = vanderpolt,y,mu yp = [y2;mu1-y12y2-y1]; Notice how the value of μ is passed as a parameter. As in Example 23.1, ode45 can be in- voked and the results plotted: [t,y] = ode45vanderpol,[0 20],[1 1],[],1; plott,y:,1,-,t,y:,2,-- legendy1,y2; Observe that because we are not specifying any options, we must use open brackets [] as a place holder. The smooth nature of the plot Fig. 23.10a suggests that the van der Pol equation with μ = 1 is not a stiff system. b If a standard solver like ode45 is used for the stiff case μ = 1000, it will fail miser- ably try it, if you like. However, ode23s does an efficient job: [t,y] = ode23svanderpol,[0 6000],[1 1],[],1000; plott,y:,1 3 2 1 ⫺1 ⫺2 ⫺3 3 2 1 ⫺1 ⫺2 ⫺3 5 10 15 20 2000 4000 6000 y1 y2 b m ⫽ 1000 a m ⫽ 1 FIGURE 23.10 Solutions for van der Pol’s equation. a Nonstiff form solved with ode45 and b stiff form solved with ode23s. 23.4 MATLAB APPLICATION: BUNGEE JUMPER WITH CORD 607 We have only displayed the y 1 component because the result for y 2 has a much larger scale. Notice how this solution Fig. 23.10b has much sharper edges than is the case in Fig. 23.10a. This is a visual manifestation of the “stiffness” of the solution.

23.4 MATLAB APPLICATION: BUNGEE JUMPER WITH CORD

In this section, we will use MATLAB to solve for the vertical dynamics of a jumper con- nected to a stationary platform with a bungee cord. As developed at the beginning of Chap. 22, the problem consisted of solving two coupled ODEs for vertical position and velocity. The differential equation for position is d x dt = v 23.26 The differential equation for velocity is different depending on whether the jumper has fallen to a distance where the cord is fully extended and begins to stretch. Thus, if the distance fallen is less than the cord length, the jumper is only subject to gravitational and drag forces, dv dt = g − signv c d m v 2 23.27a Once the cord begins to stretch, the spring and dampening forces of the cord must also be included: dv dt = g − signv c d m v 2 − k m x − L − γ m v 23.27b The following example shows how MATLAB can be used to solve this problem. EXAMPLE 23.7 Bungee Jumper with Cord Problem Statement. Determine the position and velocity of a bungee jumper with the following parameters: L = 30 m, g = 9.81 ms 2 , m = 68.1 kg, c d = 0.25 kgm, k = 40 Nm, and γ = 8 N · sm. Perform the computation from t = 0 to 50 s and assume that the initial conditions are x0 = v0 = 0. Solution. The following M-file can be set up to compute the right-hand sides of the ODEs: function dydt = bungeet,y,L,cd,m,k,gamma g = 9.81; cord = 0; if y1 L determine if the cord exerts a force cord = kmy1-L+gammamy2; end dydt = [y2; g - signy2cdmy22 - cord]; Notice that the derivatives are returned as a column vector because this is the format required by the MATLAB solvers. Because these equations are not stiff, we can use ode45 to obtain the solutions and dis- play them on a plot: [t,y] = ode45bungee,[0 50],[0 0],[],30,0.25,68.1,40,8; plott,-y:,1,-,t,y:,2,: legendx m,v ms As in Fig. 23.11, we have reversed the sign of distance for the plot so that negative distance is in the downward direction. Notice how the simulation captures the jumper’s bouncing motion. 40 20 ⫺20 ⫺40 ⫺60 ⫺80 10 20 30 40 50 x m v ms FIGURE 23.11 Plot of distance and velocity of a bungee jumper.

23.5 CASE STUDY PLINY’S INTERMITTENT FOUNTAIN

Background. The Roman natural philosopher, Pliny the Elder, purportedly had an in- termittent fountain in his garden. As in Fig. 23.12, water enters a cylindrical tank at a con- stant flow rate Q in and fills until the water reaches y high . At this point, water siphons out of the tank through a circular discharge pipe, producing a fountain at the pipe’s exit. The foun- tain runs until the water level decreases to y low , whereupon the siphon fills with air and the fountain stops. The cycle then repeats as the tank fills until the water reaches y high , and the fountain flows again. When the siphon is running, the outflow Q out can be computed with the following formula based on Torricelli’s law: Q out = C 2gyπr 2 23.28