Comparative Study of Euler vs. Runge-Kutta for ODEs

Categories: Math

Abstract

Euler’s method and the Runge-Kutta method are both widely used numerical techniques for solving ordinary differential equations. Euler's method provides a simple and straightforward approach for solving ODEs, but its accuracy diminishes when the step size is increased. In such cases, the Runge-Kutta method, particularly the second-order variant, offers better accuracy. In this paper, we utilize Python programming to compare the results obtained from both methods. Our computational approach demonstrates that the Runge-Kutta method is superior for solving differential equations compared to Euler's method.

Introduction

The Euler method is a fundamental first-order numerical technique for solving ordinary differential equations (ODEs) with a given initial value.

While it serves as a basic method for numerical integration, its accuracy decreases when larger step sizes are used. In contrast, the Runge-Kutta method, specifically the second-order variant, provides more accurate results. This paper focuses on comparing Euler's method and the Runge-Kutta method. We will discuss each method separately before highlighting the differences through theoretical and computational approaches, utilizing Python programs for visualization.

Materials and Methods

Euler Method

The Euler method is a first-order numerical technique used to solve first-order ordinary differential equations with an initial value.

Get quality help now
Doctor Jennifer
Doctor Jennifer
checked Verified writer

Proficient in: Math

star star star star 5 (893)

“ Thank you so much for accepting my assignment the night before it was due. I look forward to working with you moving forward ”

avatar avatar avatar
+84 relevant experts are online
Hire writer

Consider the differential equation:

$$frac{dx}{dt} = frac{2x}{t}$$

where the initial condition is (x = 0) at (t = 0). We can represent a first-order differential equation involving one variable as:

$$frac{dx}{dt} = f(x, t)$$

Using Taylor expansion, we can approximate (x) after a short interval (h) as:

$$x(t + h) = x(t) + hfrac{dx}{dt} + frac{1}{2}h^2frac{d^2x}{dt^2} + ldots$$

Ignoring higher-order terms, we get:

$$x(t + h) = x(t) + hf(x, t)$$

By repeating this process with known initial values, we can obtain a numerical solution for (x(t)).

Get to Know The Price Estimate For Your Paper
Topic
Number of pages
Email Invalid email

By clicking “Check Writers’ Offers”, you agree to our terms of service and privacy policy. We’ll occasionally send you promo and account related email

"You must agree to out terms of services and privacy policy"
Write my paper

You won’t be charged yet!

Although we can only obtain (x(t)) at discrete points in time, sufficiently small (h) can yield accurate results.

We use a computational approach to solve the differential equation:

$$frac{dx}{dt} = -x^3 + sin(t)$$

Table 1 displays the numerical solution obtained using Euler's method:

-1.0 -0.5 0.0 0.5 1.0
2 4 6 8 10

Example

Let's consider the differential equation:

$$frac{dx}{dt} = -x^3 + cos(t)$$

with the initial condition (x = 0) at (t = 0). We use a Python program to plot the results:


from math import sin, cos
from numpy import arange
from pylab import plot, xlabel, ylabel, show

def f(x, t):
    return -x**3 + cos(t)

a = 0.0
b = 10.0
N = 1000
h = (b - a) / N
x = 0.0
tpoints = arange(a, b, h)
xpoints = []

for t in tpoints:
    xpoints.append(x)
    x += h * f(x, t)

plot(tpoints, xpoints)
xlabel("t")
ylabel("x(t)")
show()

To run this code, use Jupyter Notebook through Anaconda Prompt.

Euler's method provides approximate solutions, with the error decreasing as (h) becomes smaller.

Runge-Kutta Method

In Euler's method, we neglected terms of order (h^2) or higher. However, the Runge-Kutta method allows us to retain the (h^2) term, given by:

$$frac{1}{2}h^2frac{d^2x}{dt^2}$$

The Runge-Kutta method is also known as the second-order Runge-Kutta method, whereas Euler's method is a first-order Runge-Kutta method.

Using Taylor's expansion, we can express (x(t + h)) around (t + frac{1}{2}h):

$$x(t + h) = xleft(t + frac{1}{2}hright) + frac{1}{2}hfrac{dx}{dt}left(t + frac{1}{2}hright) + frac{1}{8}h^2frac{d^2x}{dt^2}left(t + frac{1}{2}hright) + ldots$$

Similarly, we can derive an expression for (x(t)):

$$x(t) = xleft(t + frac{1}{2}hright) - frac{1}{2}hfrac{dx}{dt}left(t + frac{1}{2}hright) + frac{1}{8}h^2frac{d^2x}{dt^2}left(t + frac{1}{2}hright) + ldots$$

Subtracting the second expression from the first and rearranging, we get:

$$x(t + h) - x(t) = hfrac{dx}{dt}left(t + frac{1}{2}hright) + ldots$$

This allows us to rewrite (x(t + h)) as:

$$x(t + h) = x(t) + hcdot fleft(xleft(t + frac{1}{2}hright), t + frac{1}{2}hright)$$

The Runge-Kutta method is accurate to order (h^2) with an error of order (h^3). In contrast, Euler's method is a first-order method with an error of order (h^2).

Approximating (xleft(t + frac{1}{2}hright)) using Euler's method:

$$xleft(t + frac{1}{2}hright) = x(t) + frac{1}{2}hf(x, t)$$

Let (k_1 = frac{1}{2}hf(x, t)), then (k_2 = frac{1}{2}hfleft(x + frac{1}{2}k_1, t + frac{1}{2}hright)).

Using these values, we can express equation (9) as:

$$x(t + h) = x(t) + k_2$$

The Python program for the Runge-Kutta method is as follows:


from math import cos
from numpy import arange
from pylab import plot, xlabel, ylabel, show

def f(x, t):
    return -x**3 + cos(t)

a = 0.0
b = 10.0
N = 10
h = (b - a) / N
tpoints = arange(a, b, h)
xpoints = []
x = 0.0

for t in tpoints:
    xpoints.append(x)
    k1 = h * f(x, t)
    k2 = h * f(x + 0.5 * k1, t + 0.5 * h)
    x += k2

plot(tpoints, xpoints)
xlabel("t")
ylabel("x(t)")
show()

By varying (N) with different values such as 10, 20, 50, etc., and plotting the results, we can observe how the accuracy changes. Table 2 shows the plot for (N = 10):

2 4 6 8
-0.6 -0.4 -0.2 0.0
0.2 0.4 0.6 0.8

Results and Discussion

We consider the differential equation (dx/dt = -x^3 + sin(t)) and solve it using both the Runge-Kutta method and Euler's Method.

Table 3 illustrates the results for (N = 10) and (N = 50) using the Runge-Kutta method and (N = 1000) using Euler's method. It shows that the Runge-Kutta method approaches the accuracy of Euler's method when (N = 50). This suggests that the Runge-Kutta method provides accurate results even with larger (h) values, equivalent to the accuracy of Euler's method with smaller (h) values (e.g., (N = 1000)). As (N) increases, the Runge-Kutta curves gradually converge towards the Euler's method.

0 2 4 6 8 10
-0.75 -0.50 -0.25 0.00 0.25 0.50 0.75

Conclusions

Both the Runge-Kutta and Euler's methods are valuable tools for solving differential equations. Euler's method is suitable when using small step sizes, but its accuracy decreases as the step size increases. In contrast, the Runge-Kutta method provides more accurate results, up to second order ((h^2)). Euler's method is limited to first-order accuracy ((h)). Therefore, the Runge-Kutta method remains accurate even with relatively larger step sizes ((h)), equivalent to the accuracy of Euler's method with smaller step sizes.

References

  1. LANLEGE, D., KEHINDE, R., SOBANKE, D., ABDULGANIYU, A., & GARBA, U. (2019). Comparison of Euler and Range-Kutta methods in solving ordinary differential equations of order two and four.
  2. Kaw, A., & Charlie, B. (2010). Solving ODEs Euler Method and RK2/RK4.
  3. Kamruzzaman, M., & Nath, M. C. (2018). A Comparative Study on Numerical Solution of Initial Value Problem by Using Euler’s Method, Modified Euler’s Method and Runge-Kutta Method. Journal of Computer and Mathematical Sciences, 9(5), 493-500.
  4. Euler’s Method—A numerical solution for Differential Equations. (n.d.). Retrieved from https://www.intmath.com/differential-equations/11-eulers-method-des.php
  5. Newman, M. (2013). Computational physics. CreateSpace Independent Publ..
  6. Newman, M. (2013). Computational Physics-Figures.
  7. Python Software Foundation. Python Language Reference, version 3.7.3.
Updated: Sep 26, 2024
Cite this page

Comparative Study of Euler vs. Runge-Kutta for ODEs. (2024, Jan 06). Retrieved from https://studymoose.com/document/comparative-study-of-euler-vs-runge-kutta-for-odes

Live chat  with support 24/7

👋 Hi! I’m your smart assistant Amy!

Don’t know where to start? Type your requirements and I’ll connect you to an academic expert within 3 minutes.

get help with your assignment