# C++ Solving ODEs

1. Mar 14, 2015

### ChrisVer

Hi,

Apart from the Euler's method, is there any other method (with better efficiency) that can let us solve an Ordinary Differential Equation of the form $\frac{dy}{dx}= f(x,y)$?

2. Mar 14, 2015

### fluidistic

3. Mar 14, 2015

### Staff: Mentor

4. Mar 14, 2015

### ChrisVer

Thanks, I will try to solve an ODE I solved with Euler method using RK4 this time and see how it goes...

5. Mar 14, 2015

### ChrisVer

I am not sure if that can be actually an answerable question, but I will give it a try...
Is it possible for the Euler method to give a deviation from the actual solution of around 0.5%
While the RK4 method gives a deviation of 0.8 *10^(-9) % ?
The correction seems enormous if you asked me...

I solved for the two methods the ODE:
$\frac{dy(x)}{dx} = ay(x)$
which I know the exact solution: $y(X)= y_0 e^{aX}$
For $y_0=1$ and $a=1$ and an interval $X=1$ this is just the value of e=2.71828...
Euler method for 100 steps gave me 2.70481 ( $\frac{|e-2.70481|}{e} \approx 0.005$ ) and the RungeKuta4 for 100 steps gave me 2.71828 ( $\frac{|e-RK4|}{e} \approx 8 \times 10^{-12}$) -i got the last result by letting the program do the calculation of the deviation for me. Otherwise the values as given are indistinguishable.

Not to say that things get even worse for EulerMethod when I apply it for example at $y(X)=10 e^{60}$ where the EM gives almost double the actual value (86% off) while the RK4 keeps itself safe (10^-6 % off).

Last edited: Mar 14, 2015
6. Mar 14, 2015

### Staff: Mentor

The Euler method can be unstable and should be avoided.

7. Mar 14, 2015

### ChrisVer

I also tried the example wiki uses: $y=1 \cdot e^{-2.3 X}$ with 5 (as I see steps)...
The exact solution for X=5 is $10^{-5}$
Euler gives a very bad result indeed $=-3.71$...
RK4 gives a bad result as well $0.0263$...
Things become better for RK4 at >20 steps...

8. Mar 14, 2015

### Staff: Mentor

Runge-Kutta is not wonderful for stiff equations. Have a look at the GNU Scientific Library (GSL). You will find different methods implemented in there, including some that are well suited to stiff equations.