12.07.2015 Views

COPYRIGHT 2008, PRINCETON UNIVERSITY PRESS

COPYRIGHT 2008, PRINCETON UNIVERSITY PRESS

COPYRIGHT 2008, PRINCETON UNIVERSITY PRESS

SHOW MORE
SHOW LESS
  • No tags were found...

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

206 chapter 9C Dhigh-precision work, and unless you get every fraction and method call just right,your rk4 may appear to work well but still not give all the precision that you shouldhave. Regardless, we recommend that you write your own rk2, as doing so willmake it clearer as to how the Runge–Kutta methods work, but without all the pain.We give rk2, rk4, and rk45 codes on the CD and list rk4.java in Listing 9.1.1. Write your own rk2 method. Design your method for a general ODE; thismeans making the derivative function f(t, x) a separate method.2. Use your rk2 solver in a program that solves the equation of motion (9.6) or(9.24). Use double precision to help control subtractive cancellation and plotboth the position x(t) and velocity dx/dt as functions of time.3. Once your ODE solver compiles and executes, do a number of things to checkthat it is working well and that you know what h values to use.a. Adjust the parameters in your potential so that it corresponds to a pureharmonic oscillator (set p =2or α =0). For this case we have an analyticresult with which to compare:x(t)=A sin(ω 0 t + φ), v(t)=ω 0 A cos(ω 0 t + φ), ω 0 = √ k/m.b. Pick values of k and m such that the period T =2π/ω is a nice numberwith which to work (something like T =1).c. Start with a step size h ≃ T/5 and make h smaller until the solution lookssmooth, has a period that remains constant for a large number of cycles,and agrees with the analytic result. As a general rule of thumb, we suggestthat you start with h ≃ T/100, where T is a characteristic time for theproblem at hand. Here we want you to start with a large h so that you cansee a bad solution turn good.d. Make sure that you have exactly the same initial conditions for the analyticand numerical solutions (zero displacement, nonzero velocity) and thenplot the two together. It is good if you cannot tell them apart, yet that onlyensures that there are approximately two places of agreement.e. Try different initial velocities and verify that a harmonic oscillator isisochronous, that is, that its period does not change as the amplitude varies.4. Now that you know you can get a good solution of an ODE with rk2, comparethe solutions obtained with the rk2, rk4, and rk45 solvers.TABLE 9.1Comparison of ODE Solvers for Different EquationsEquation No. Method Initial h No. of Flops Time (ms) Relative Error(9.40) rk4 0.01 1000 5.2 2.2 × 10 −8rk45 1.00 72 1.5 1.8 × 10 −8(9.41) rk4 0.01 227 8.9 1.8 × 10 −8rk45 0.1 3143 36.7 5.7 × 10 −11−101<strong>COPYRIGHT</strong> <strong>2008</strong>, PRINCET O N UNIVE R S I T Y P R E S SEVALUATION COPY ONLY. NOT FOR USE IN COURSES.ALLpup_06.04 — <strong>2008</strong>/2/15 — Page 206

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!