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

  • Thread starter brokenlynx
  • Start date
  • Tags
    Derivatives
In summary, the conversation discusses how to specify derivatives in C++ for use in the 4th order Runge-Kutta method. The solution being sought is defined in a "t x ANGLE2" space, and the steps for calculating the solution are outlined using the initial value and step size.
  • #1
brokenlynx
4
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
  • #2
I will assume that the solution y = (y1, y2) = (θ, ω) you are seeking is defined in a "t x ANGLE2" space where θ [itex]\in[/itex] ANGLE and ω [itex]\in[/itex] 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:
  • #3


In order to use the 4th order Runge-Kutta method to solve a system of differential equations, you need to first specify the derivatives of the variables in the equations. In your case, the two first-order differential equations you are working with are:

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

To specify these derivatives in C++, you will need to define two functions: one for θ' and one for ω'. These functions will take in the current values of θ and ω, and return the values of θ' and ω' respectively. The code would look something like this:

// Function for θ'
double theta_prime(double theta, double omega, double R, double g) {
return omega;
}

// Function for ω'
double omega_prime(double theta, double omega, double R, double g) {
return (-g/R) * sin(theta);
}

Once you have these functions defined, you can use them in your implementation of the 4th order Runge-Kutta method. This method involves using a series of calculations to approximate the values of θ and ω at each time step. You can refer to a numerical methods textbook or online resources for the exact implementation of the algorithm in C++.

In summary, to specify derivatives in C++ for use in the 4th order Runge-Kutta method, you will need to define functions for each derivative in your system of differential equations, and use those functions in your implementation of the method. I hope this helps clarify your understanding of how derivatives are specified in this context.
 

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

1. What is a derivative?

A derivative is a mathematical concept that represents the rate of change of a function at a specific point. It is calculated as the slope of the tangent line to the function at that point.

2. Why are derivatives important?

Derivatives are important because they allow us to understand and analyze the behavior of functions. They are used in various fields such as physics, economics, and engineering to model and predict changes in systems.

3. How do you specify a derivative?

To specify a derivative, you need to provide the function and the point at which you want to calculate the derivative. This can be done using the notation f'(x) or dy/dx, where f is the function and x is the point.

4. What are the rules for calculating derivatives?

There are several rules for calculating derivatives, including the power rule, product rule, quotient rule, and chain rule. These rules allow us to find the derivative of more complex functions by breaking them down into simpler parts.

5. How are derivatives used in real life?

Derivatives have many real-life applications, such as in optimizing business strategies, predicting stock market trends, and designing efficient engineering systems. They are also used in physics to study the motion of objects and in economics to analyze supply and demand curves.

Similar threads

Replies
61
Views
932
  • Calculus and Beyond Homework Help
Replies
14
Views
2K
  • Programming and Computer Science
Replies
15
Views
2K
Replies
9
Views
2K
  • Differential Equations
Replies
16
Views
3K
  • Differential Equations
Replies
6
Views
2K
  • Programming and Computer Science
Replies
8
Views
2K
  • Atomic and Condensed Matter
Replies
3
Views
1K
  • Differential Equations
Replies
6
Views
3K
  • Programming and Computer Science
Replies
13
Views
2K
Back
Top