How do I specify derivatives in C++ for use in the 4th order Runge-Kutta method?

  • Thread starter Thread starter brokenlynx
  • Start date Start date
  • Tags Tags
    Derivatives
brokenlynx
Messages
4
Reaction score
0
I'm just trying to understand how exactly derivatives are to be specified in C++ for use in the 4th order Runge-Kutta method.

I am implementing the method in C++ (though this part of my program isn't complete yet) however what I am looking to do is first specify the derivatives of the variables in the two first-order differential equations I am working with:

θ' = ω
ω' = − g⁄R sin θ

(for modelling the motion of a single pendulum)

double theta - initial angular displacement (Radians)
double R - length of Pendulum Rod (Metres)
double g - gravitational constant (Distance/Time^2)

I just need a little help understanding how to specify the derivatives and basically what I'm doing when I am. (I'm familiar with calculus just not numerical solving algorithms).

Thanx.
 
Physics news on Phys.org
I will assume that the solution y = (y1, y2) = (θ, ω) you are seeking is defined in a "t x ANGLE2" space where θ \in ANGLE and ω \in ANGLE and variable t (e.g., time) is implicit -- that is, t does not appear as an argument of θ or ω. If that is not right, please post so.

Given the initial value y(0) = (y1(0), y2(0)) = (θ(0), ω(0)), and an (arbitrarily small) step size h, the 4 functional steps are:

k1(0) = (k11(0), k12(0)) = (θ'(0), ω'(0)) = (ω(0), −g⁄R sin θ(0));

k2(0) = (k21(0), k22(0)) = (θ'(0)+h k11(0)/2, ω'(0)+h k12(0)/2) = (ω(0)+hω(0)/2, −g⁄R sin θ(0)+h[−g⁄R sin θ(0)]/2);

Then define k3(0) = (k31(0), k32(0)) in terms of k2(0) = (k21(0), k22(0)) as described here: http://en.wikipedia.org/wiki/Runge-kutta#The_classical_fourth-order_Runge.E2.80.93Kutta_method;

Then define k4(0) = (k41(0), k42(0)) in terms of k3(0) = (k31(0), k32(0)) as described here: http://en.wikipedia.org/wiki/Runge-kutta#The_classical_fourth-order_Runge.E2.80.93Kutta_method.

Finally, calculate the weighted average y(1) = (θ(1), ω(1)) = y(0) + [k1(0) + 2k2(0) + 2k3(0) + k4(0)]h/6. And you are on your way to calculate y(2), ..., y(N).
 
Last edited:

Similar threads

Back
Top