Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

C++ Solving ODEs

  1. Mar 14, 2015 #1

    ChrisVer

    User Avatar
    Gold Member

    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 [itex]\frac{dy}{dx}= f(x,y)[/itex]?
     
  2. jcsd
  3. Mar 14, 2015 #2

    fluidistic

    User Avatar
    Gold Member

  4. Mar 14, 2015 #3

    jedishrfu

    Staff: Mentor

  5. Mar 14, 2015 #4

    ChrisVer

    User Avatar
    Gold Member

    Thanks, I will try to solve an ODE I solved with Euler method using RK4 this time and see how it goes...
     
  6. Mar 14, 2015 #5

    ChrisVer

    User Avatar
    Gold Member

    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:
    [itex] \frac{dy(x)}{dx} = ay(x)[/itex]
    which I know the exact solution: [itex]y(X)= y_0 e^{aX}[/itex]
    For [itex]y_0=1[/itex] and [itex]a=1[/itex] and an interval [itex]X=1[/itex] this is just the value of e=2.71828...
    Euler method for 100 steps gave me 2.70481 ( [itex]\frac{|e-2.70481|}{e} \approx 0.005[/itex] ) and the RungeKuta4 for 100 steps gave me 2.71828 ( [itex] \frac{|e-RK4|}{e} \approx 8 \times 10^{-12}[/itex]) -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 [itex]y(X)=10 e^{60}[/itex] 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
  7. Mar 14, 2015 #6

    DrClaude

    User Avatar

    Staff: Mentor

    The Euler method can be unstable and should be avoided.
     
  8. Mar 14, 2015 #7

    ChrisVer

    User Avatar
    Gold Member

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

    DrClaude

    User Avatar

    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.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: C++ Solving ODEs
Loading...