Question

I'm trying to solve a differential equation for a pendulum movement, given the pendulum initial angle (x), gravity acceleration (g), line length (l), and a time step (h). I've tried this one using Euler method and everything's alright. But now i am to use Runge-Kutta method implemented in GSL. I've tried to implement it learning from the gsl manual, but I'm stuck at one problem. The pendulum doesn't want to stop. Let's say that I start it with initial angle 1 rad, it always has it's peak tilt at 1 rad, no matter how many swings it already did. Here's the equation and the function i use to give it to GSL:

x''(t) + g/l*sin(x(t)) = 0

transforming it:

x''(t) = -g/l*sin(x(t))

and decomposing:

y(t) = x'(t)
y'(t) = -g/l*sin(x(t))

Here's the code snippet, if that's not enough i can post the whole program (it's not too long), but maybe here's the problem somewhere:

    int func (double t, const double x[], double dxdt[], void *params){
        double l = *(double*) params;
        double g = *(double*) (params+sizeof(double));
        dxdt[0] = x[1];
        dxdt[1] = -g/l*sin(x[0]);
        return GSL_SUCCESS;
    }

The parameters g and l are passed correctly to the function, I've already checked that.

Was it helpful?

Solution

As Barton Chittenden noted in a comment, the pendulum should keep going in the absence of friction. This is expected.

As for why it slows and stops when you use the Euler method, that's touching on a subtle and interesting subject. A (ideal, friction-free) physical pendulum has the property that energy in the system is conserved. Different integration schemes preserve that property to different degrees. With some integration schemes, the energy in the system will grow, and the pendulum will swing progressively higher. With others, energy is lost, and the pendulum comes to a halt. The speed at which either of these happens depends partially on the order of the method; a more accurate method will often lose energy more slowly.

You can easily observe this by plotting the total energy in your system (potential + kinetic) for different integration schemes.

Finally, there is a whole fascinating sub-field of integration methods which preserve certain conserved quantities of a system like this, called symplectic methods.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top