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

  • Context: Undergrad 
  • Thread starter Thread starter brokenlynx
  • Start date Start date
  • Tags Tags
    Derivatives
Click For Summary
SUMMARY

This discussion focuses on specifying derivatives in C++ for implementing the 4th order Runge-Kutta method to model a single pendulum's motion. The derivatives are defined as θ' = ω and ω' = −g/R sin θ, where θ is the angular displacement, ω is the angular velocity, g is the gravitational constant, and R is the length of the pendulum rod. The user seeks clarity on how to implement these derivatives in C++, particularly in defining the functional steps of the Runge-Kutta algorithm, which includes calculating k1, k2, k3, and k4 based on the initial conditions and step size.

PREREQUISITES
  • Understanding of C++ programming language
  • Familiarity with calculus, specifically derivatives
  • Knowledge of numerical methods, particularly the Runge-Kutta method
  • Basic concepts of differential equations
NEXT STEPS
  • Implement the full 4th order Runge-Kutta method in C++ for the pendulum model
  • Explore the mathematical derivation of the Runge-Kutta method
  • Learn about error analysis in numerical methods
  • Investigate alternative numerical methods for solving differential equations
USEFUL FOR

Students and professionals in physics and engineering, C++ developers working on simulations, and anyone interested in numerical methods for solving differential equations.

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

  • · Replies 65 ·
3
Replies
65
Views
8K
  • · Replies 14 ·
Replies
14
Views
3K
  • · Replies 16 ·
Replies
16
Views
4K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 15 ·
Replies
15
Views
3K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 11 ·
Replies
11
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 9 ·
Replies
9
Views
4K
  • · Replies 6 ·
Replies
6
Views
4K