# Solving two coupled non-linear second order differential equations numerically

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.

-
@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. –  ramanujan_dirac 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.

-

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.

-

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.

-