## Recipe 11.2. Applying the Runge-Kutta Method to Second-Order Initial Value Problems## ProblemYou need to numerically solve a second-order differential equation of the form: ## SolutionThis is a standard initial value problem, and you can implement any of a number of standard numerical integration techniques to solve it using Excel and VBA. ## DiscussionConsider this equation and initial conditions: Physically, this equation represents the equation of motion for an object moving under some applied thrust, T.
To solve this equation, it helps to rewrite it in the form of two first-order equations. Let v = ds/dt represent the velocity of the object. Now we have the following: These equations represent two coupled first-order equations, and you can apply any of a number of numerical integration schemes to solve them both.
I decided to use the Runge-Kutta method
for this example. This method is based on taking more terms in the Taylor series expansion of a function, as I explained in Recipe 11.1. The caveat here is that you need to apply further Taylor series expansions to estimate the higher-order derivatives. The Runge-Kutta approach reduces the truncation error to something on the order of (dt) The Runge-Kutta integration formulas are a little more complicated than the simple formula we used with Euler's basic method. The general equations are as follows: y' represents dy/dx in these equations. You can see that every step requires four intermediate computations prior to predicting the next y-value, that is, y(x + dx). Example 11-3 shows the VBA subroutine I wrote implementing these formulas. ## Example 11-3. Implementation of Runge-Kutta method
This is a relatively long subroutine compared to the one shown in Example 11-2 implementing Euler's method, so I commented this one to help you navigate through it. As usual, several local variables are declared at the beginning of the subroutine. There are a bunch of them here, so I labeled each one in Example 11-3 to indicate their respective purposes. After the variables are declared, several of them are populated with data retrieved from the active spreadsheet. The spreadsheet is shown in Figure 11-4. ## Figure 11-4. Solution using the Runge-Kutta methodValues retrieved from the spreadsheet include the time-step size, thrust, mass, drag coefficient, number of iterations, and number of output rows. The block of code in Example 11-3 containing the
The next little section of code simply initializes a few variables. This is where the initial values for displacement and velocity specified in the problem are set. Next, the I find it easier to think in terms of physical values like force and acceleration whenever possible. Knowing that acceleration is equal to dv/dt, you can readily see that acceleration corresponds to y' in the Runge-Kutta formulas for this particular problem. This is because we broke the original equation up and are dealing with the first one involving velocity. After all of the k-values are obtained, v Finally, the results are sent to the active spreadsheet in the same manner I showed you in Recipe 11.1. Figure 11-4 shows the results, including a plot of velocity and displacement versus time, using a time step of 0.005. You should play around with this time step to see how the accuracy of the results is affected. (If this were a class, I'd assign that exercise for homework.) I should also mention that I used a button control, as you can see in Figure 11-4, to call the subroutine when the button is clicked. Refer back to Recipe 9.3 to learn how to add this sort of button control and assign its functionality. |

# Recipe11.2.Applying the Runge-Kutta Method to Second-Order Initial Value Problems

Similar products