• No results found

A daptive R unge-K utta Function

Here is how such an adaptive iteration can be implemented for our fourth-order Runge-Kutta scheme: Call A the local truncation error; we know th a t A o c r5 for fourth-order Runge-Kutta. Suppose th at the current time step r 0id gave an error of Ac = |xf> — xs |; this is our estimate for the truncation error. Given that we want the error to be less than or equal to the user-specified ideal error, call it A*; then, the estimate for the new time step is

Test — T Ai Ac

I / o

(3.30) Since this is only an estimate, the new time step is rnew = SiTest> where S± < 1.

This makes us overestimate the change when we lower r and underestimate the change when we raise it. We waste computer effort every time we reject an answer and need to reduce the time step, so it is better to set. r new < r est·

We should also put in a second safety factor, 52 > 1. to be sure that the program is not too enthusiastic about precipitously raising or lowering the time step. W ith both safeguards, the new time step is

{ *$2Told if Si Test ^ $2Told

Told/S2 if SlTest ^ ^oid/*?2 (3.31)

*Sirest otherwise

I able 3.5: Outline of function rka, which evaluates a single step using the lourth-order adaptive Runge-Kutta method.

• Inputs: x(tf), t , r. Aj, f(x. £;A). and A.

• Outputs:

x(f')>

and r n€W.

• Set. initial variables.

• Loop over maximum number of attem pts to satisfy error bound.

- Take the two small time steps.

-- Take the single big time step.

- Compute th e estimated truncation error.

- Estimate new r value (including safety factors).

- If error is acceptable, return computed values.

• Issue error message if error bound never satisfied.

See pages 93 and 100 for program listings.

These constraints ensure th at our new estimate for r never increases or de­

creases by more than a factor of 52. Of course, this new r may be found to be insufficiently small, and we may have to continue reducing the time step: but at least we know it will not happen in an uncontrolled way.

This procedure is not bulletproof—round-off error becomes significant at very small time steps. For this reason the adaptive iteration may fail to find a time step th at gives the desired accuracy. Keep this limitation in mind when you specify the ideal error.

An adaptive Runge-Kutta function, called rka, is outlined in Table 3.5.

Notice th at the inputs in the calling sequence are the same as for rk4, except for the addition of A*, the specified ideal error. The outputs from rk a are the new state of the system, x(i'); the new time, t f; and the new time step, r newj which should be used the next time rk a is called.

Using the adaptive Runge-Kutta method, the o r b i t program gives the re­

sults shown in Figure 3.9 for a highly elliptical orbit. Notice that the program takes many more steps at perihelion (closest approach) than at aphelion. Com­

pare with the results using nonadaptive Runge-Kutta (Figure 3.7) in which the steps at perihelion are widely spaced. A plot of time step versus radial dis­

tance (Figure 3.10) shows th at r varies by nearly three orders of magnitude.

Interestingly, this graph reveals an approximate power law relation of the form r oc r 3/ 2. Of course this dependence reminds us of Kepler’s third law. Equation (3.10). We expect some scatter in the points since our adaptive routine only

1500- 1000-

V 500

12 I -500

--- Kiretic ---Total

0.2 0.3

Time (yr)

J

Figure 3.9: Graphs of trajectory and energy from the o r b i t program using adaptive Runge-Kutta. The initial radial distance is 1 AU, and the initial tangential velocity is 7r/2 AU/yr. The initial time step is r = 0.1 yr; 40 time steps are computed.

10“1

IQ'3

10"4 2 10 ' 10 1 10°0

Distance (AU)

Figure 3.10: Time step, r , as a function of radial distance from the o r b it pro- gram, using adaptive Runge-Kutta. Parameters arc the same as for Figure 3.9.

Figure 3.11: Double pendulum,

estimates the optimum time step.

EXERCISES

14. Consider the central force

v GMm /„ a \

where a is a constant. Modify the o rb it program to compute the motion of an object under this force law. Using adaptive Runge-Kutta, show th at the orbit precesses 360(1 — a)/a degrees per revolution, where a = y /l + GM m2a / L'2 and L is the angular momentum. [Computer]

15. Modify o rb it to add a drag force on the cornet, F<j = C |v|v (see Section 2.1).

Take the drag force to be small relative to the gravitational force F g by fixing the constant C such that |F g(ri)| = 100|Fd(vi)|, where ri and v i are the initial position and velocity, respectively. Show that the average kinetic energy (averaged over an orbit) increases with time. [Computer]

16. In Rutherford scattering, an alpha particle is deflected as it passes near the nucleus of a heavy atom. Write a program using adaptive Runge-Kutta to simulate Rutherford scattering. Find the scattering angle for a 5-MeV alpha particle striking a gold nucleus at an impact parameter of 10 femtometers. [Computer]

17. The adaptive Runge-Kutta routine, rka, uses a generic method for estimating the error. Write a modified version of rk a that accepts a user-specified function that computes Ac. For the comet problem, write a function that evaluates the absolute fractional error in the total energy. Test your routines and compare with the original version of rk a for the case considered in Figures 3.9 and 3.10. [Computer]

18. Write an adaptive Runge-Kutta program to simulate a pendulum system con­

sisting of a bob of mass m and a massless rod with rest length L. The rod acts like a stiff spring with spring constant, k. Assume that the motion is 111 the xy plane. Obtain plots of the motion for the values m = 0.1 kg, L = 1.0 in, and spring constants in the range k — 102 N /m (rubber) to k = 106 N/m (metal wire). How does the average time step adopted by the algorithm vary with the spring constant? [Computer]

19. Consider a double pendulum, as shown in Figure 3.11. Its Lagrangian is

m i + 1712 T 2n2 . W 2 r 2 ¿2 . j a ---—---L \ U \ ^ ---— ¿ 2 ^ 2 4 W'‘2QL/2 COS v 2

4- 7n2L iL2ex62 cos(0i — #2) + {m,} + 1712)9 Li cos0i

where 0 = dG/dt. (a) Use the Lagrangian to find the equations of motion. [Pencil] (b) Write a program that uses adaptive Runge-Kuttato simulate the motion of the double pendulum. Take g — 9.81 m /s2, m1 = m2, and Li = L<2 = 0.1 m; compute examples of the motion for various initial conditions. Show that in some cases, the lower mass spins completely around with an aperiodic motion. [Computer]