Home > Python > Ordinary Differential Equation-1

Ch. 6.1 Euler Algorithm

Chapters Index
Program 1

Euler Algorithm

Python 3.11 Ready

                                
Click "Run Code" to execute script and render figures.

Mathematical Problem Formulation

Euler's Method for Solving ODE

To solve the first-order ODE given by:

\(\frac{dy}{dx} = f(x, y)\)

with an initial value:

\(y(x_0) = y_0\)

Euler's method uses the following formula to approximate the value of \(y\) at each step:

\(y_{i+1} = y_i + h \cdot f(x_i, y_i)\)

Where:

  • \(h\) is the step size (how far you move along the x-axis at each step).
  • \(y_i\) is the current value of \(y\) at \(x_i\).
  • \(f(x_i, y_i)\) is the slope at the point \((x_i, y_i)\).

Steps to Implement:

  1. Write down the ODE and the initial value.
  2. Choose a step size \(h\). Smaller steps usually give better results.
  3. Start with the initial value \(y_0\).
  4. Use the formula to calculate \(y\) at each step \(x_i\).

Algorithm: Euler's Method for Solving ODE

Input:

  • Function \(f(x, y)\) representing the ODE \(\frac{dy}{dx} = f(x, y)\).
  • Initial condition \(x_0\), \(y_0\) where \(y(x_0) = y_0\).
  • Step size \(h\).
  • End value \(x_{\text{end}}\) where the solution is to be approximated.

Output:

Approximate values of \(y\) at discrete points \(x_0, x_1, x_2, \dots, x_n\) where \(x_n \leq x_{\text{end}}\).

Steps:

  1. Initialization:
    • Set \(x = x_0\).
    • Set \(y = y_0\).
    • Initialize a list \(x_values\) to store the values of \(x\).
    • Initialize a list \(y_values\) to store the values of \(y\).
    • Add the initial values \(x_0\) and \(y_0\) to \(x_values\) and \(y_values\).
  2. Iterate:
    • While \(x < x_{\text{end}}\):
      • Calculate the slope at the current point using the function \(f(x, y)\):
        \(\text{slope} = f(x, y)\)
      • Update the value of \(y\) using Euler's formula:
        \(y_{\text{new}} = y + h \cdot \text{slope}\)
      • Update the value of \(x\) to the next step:
        \(x_{\text{new}} = x + h\)
      • Append the new values \(x_{\text{new}}\) and \(y_{\text{new}}\) to \(x_values\) and \(y_values\).
      • Set \(x = x_{\text{new}}\) and \(y = y_{\text{new}}\).
  3. Termination:
    • Continue the iteration until \(x\) reaches or exceeds \(x_{\text{end}}\).
  4. Output the Results:
    • Return the lists \(x_(values)\) and \(y_(values)\), which contain the discrete points \(x\) and the corresponding approximated values \(y\).

Example:

Given the ODE:

\(\frac{dy}{dx} = x + y\)

with the initial condition:

\(y(0) = 1\)

Let’s find the value of \(y\) at \(x = 0.2\) with \(h = 0.1\).

Theoretical Principles & Computational Physics Guide

Euler Algorithm: First-Order Discretization for Differential Equations

Dr. Alorika Chatterjee & Dr. Anirban Shaw Science & Physics Learners Python 3 / NumPy / SciPy

1. Core Principles & Theoretical Framework

Foundational Stepper: The simplest explicit numerical scheme for initial value problems $\frac{dy}{dt} = f(t, y)$.
First-Order Truncation: Local truncation error scales as $\mathcal{O}(h^2)$, producing global cumulative error $\mathcal{O}(h)$.
Energy Drift: Standard explicit Euler artificially injects energy into conservative oscillatory systems.

2. Mathematical Formulation & Algorithmic Pipeline

First-order Taylor expansion stepping formula:

$$ y(t + h) = y(t) + h \frac{dy}{dt} + \mathcal{O}(h^2) \implies y_{n+1} = y_n + h f(t_n, y_n), \quad t_{n+1} = t_n + h $$

3. Numerical Mechanics & Implementation Logic

  • Initial State: Set initial time $t_0$, initial condition $y_0$, and step size $h = \Delta t$.
  • Slope Evaluation: Compute instantaneous rate of change $k_1 = f(t_n, y_n)$.
  • State Advancement: Update state $y_{n+1} = y_n + h \cdot k_1$ and advance time $t_{n+1} = t_n + h$.
  • Stability Criterion: Keep $h$ sufficiently small to avoid numerical divergence.

4. Relevant Computational Practice Exercises

Exercise 1
Solve radioactive decay $dN/dt = -\lambda N$ using Euler's method and compare against the exact solution $N_0 e^{-\lambda t}$.
Exercise 2
Simulate a harmonic oscillator using explicit Euler, observing how total mechanical energy artificially grows with time.
Exercise 3
Perform an error convergence test by halving step size $h$ ($h = 0.1, 0.05, 0.025$) and plot the global error vs $h$.
Standard Literature & References:
  • John R. Taylor, "Classical Mechanics", University Science Books (2005).
  • William H. Press et al., "Numerical Recipes", Cambridge University Press (2007).