# 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

## 3 Answers

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.

-

## Did you find this question interesting? Try our newsletter

email address

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.

-
{"extraction_info": {"found_math": true, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 2, "mathjax_display_tex": 1, "mathjax_asciimath": 0, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0}, "config": {"markdown_headings": true, "markdown_code": true, "boilerplate_config": {"ratio_threshold": 0.18, "absolute_threshold": 10, "end_threshold": 15, "enable": true}, "remove_buttons": true, "remove_image_figures": true, "remove_link_clusters": true, "table_config": {"min_rows": 2, "min_cols": 3, "format": "github"}, "remove_chinese": true, "remove_edit_buttons": true, "extract_latex": true}, "english_confidence": 0.9227153062820435, "perplexity_flag": "middle"}