C++ Alternatives to Euler's Method for Solving ODEs

In summary, the Euler method can give a deviation from the actual solution of around 0.5%, while the RK4 method gives a deviation of 0.8 *10^(-9) %.
  • #1
ChrisVer
Gold Member
3,378
464
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]?
 
Physics news on Phys.org
  • #4
Thanks, I will try to solve an ODE I solved with Euler method using RK4 this time and see how it goes...
 
  • #5
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:
  • #6
The Euler method can be unstable and should be avoided.
 
  • Like
Likes ChrisVer
  • #7
DrClaude said:
The Euler method can be unstable and should be avoided.

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...
 
  • #8
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.
 

Related to C++ Alternatives to Euler's Method for Solving ODEs

1. What is Euler's method for solving ODEs?

Euler's method is a numerical method for approximating the solution to ordinary differential equations (ODEs). It uses a Taylor series expansion to estimate the value of the function at a given point, based on its derivative. This method is simple to implement but can be inaccurate for certain types of ODEs.

2. What are some alternatives to Euler's method?

There are several alternatives to Euler's method for solving ODEs, including Runge-Kutta methods, Adams-Bashforth methods, and backward differentiation formulas. These methods may have higher accuracy and stability for certain types of ODEs.

3. How does Runge-Kutta method differ from Euler's method?

Unlike Euler's method, which uses a first-order Taylor series expansion, Runge-Kutta methods use higher-order Taylor series expansions to approximate the solution of an ODE. This can result in a more accurate approximation, especially for complex ODEs.

4. Are there any drawbacks to using higher-order methods?

While higher-order methods may provide more accurate solutions, they may also require more computational resources and may be more complex to implement. Additionally, some higher-order methods may introduce numerical instability, which can affect the accuracy of the solution.

5. Can I use these methods in any programming language?

Yes, these methods can be implemented in various programming languages, including C++, MATLAB, Python, and more. However, the specific implementation may vary between languages and may require some knowledge of numerical methods and programming concepts.

Similar threads

Replies
7
Views
2K
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Science and Math Textbooks
Replies
5
Views
2K
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
Replies
8
Views
341
  • Differential Equations
Replies
11
Views
20K
Replies
4
Views
588
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
Back
Top