Computational Science Stack Exchange is a question and answer site for scientists using computers to solve scientific problems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have encountered the following system of differential equations in lagrangian mechanics. Can you suggest a numerical method, with relevant links and references on how can I solve it, and the implementation in C (if possible) Also, is there a shorter implementation on Matlab or Mathematica?

\begin{align*} mx \dot y^2 + mg\cos(y) - Mg - (m+M)\,\ddot x &= 0 \\ g\sin(y) + 2\dot x\dot y + x \,\ddot y &= 0 \end{align*}

where $\dot x$ or $\dot y$ are time derivatives, and the double dots indicate a 2nd derivative wrt time.

share|improve this question
@ramanujan_dirac: Check my edit. Is this the set of equations you meant to type? – Paul Sep 6 '12 at 18:10
@Paul: Sorry, it was actually M + m, where m, M are distinct constants in general. I have edited to reflect the same. – user1717 Sep 6 '12 at 18:35

Why implement it by hand? Matlab, Maple and Mathematica all have tools builtin to solve differential equations numerically, and they use far better methods than you could implement yourself in finite time. In Matlab, you want to look at ode45. In Maple it's called dsolve (with the 'numeric' option set), in Mathematica it is NDSolve.

share|improve this answer

You could use Runge-Kutta method to solve this system numerically, first rewrite your second order equation as a first order system by doing following substitution trick:

$$ \left\{ \begin{aligned} x_1' &= x_2 \\ y_1' & = y_2 \\ x_2' &= x_1 y_2 + g \cos y_1 -(M+m)g/m \\ y_2' &= -g(\sin y_1) /x_1 - 2x_2 y_2/x_1 \end{aligned} \right. $$

Now you could use MATLAB's ode45 or ode23 to solve it, if you wanna implement the method on C, I believe there are many pkgs available there on the internet, like this.

share|improve this answer

Runge-Kutta methods such as (4,5) are also available in the GNU Scientific Library (which is written in C). They also include adaptive time-stepping.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.