Skip to content

Ordinary Differential Equations

1. Review of Ordinary Differential Equations (ODE)

Intuition

An Ordinary Differential Equation (ODE) relates a function y(x) to its derivatives. The general form of an ODE is:

F(x,y,y,y,,y(n))=0.

ODEs are classified based on:

  • Order: The highest derivative present.
  • Linearity: Whether the equation can be written as a linear combination of the dependent variable and its derivatives.

Example

y+y=0(First-order linear ODE).

The solution is y=Cex, where C is a constant.

Python Visualization

import numpy as np
import matplotlib.pyplot as plt

# Define the solution
def y(x, C):
    return C * np.exp(-x)

# Plot
x = np.linspace(0, 5, 500)
C = 1
plt.plot(x, y(x, C), label='y = Ce^{-x}')
plt.title('Solution of y' + y = 0')
plt.xlabel('x')
plt.ylabel('y')
plt.legend()
plt.grid()
plt.show()

2. Linear Equations

Intuition

A first-order linear ODE has the form:

y+P(x)y=Q(x).

The solution is obtained using an integrating factor:

μ(x)=eP(x)dx,y=1μ(x)μ(x)Q(x)dx.

Example

Solve y+y=x:

P(x)=1,Q(x)=x,μ(x)=ex.
y=exexxdx=ex(xexex)+C=x1+Cex.

Python Visualization

from sympy import symbols, Function, Eq, exp, dsolve

# Define symbols
x = symbols('x')
y = Function('y')

# Define ODE
otde = Eq(y(x).diff(x) + y(x), x)

# Solve ODE
solution = dsolve(otde)
print(f"Solution: {solution}")

# Plot
C = 1
x_vals = np.linspace(0, 5, 500)
y_vals = x_vals - 1 + C * np.exp(-x_vals)
plt.plot(x_vals, y_vals, label='y = x - 1 + Ce^{-x}')
plt.title('Solution of y' + y = x')
plt.xlabel('x')
plt.ylabel('y')
plt.legend()
plt.grid()
plt.show()

3. Second-Order Linear Equations

Intuition

A second-order linear ODE has the form:

ay+by+cy=0.

The solution depends on the roots of the characteristic equation ar2+br+c=0:

  1. Distinct real roots: y=C1er1x+C2er2x.
  2. Repeated root: y=(C1+C2x)er1x.
  3. Complex roots: y=eαx(C1cosβx+C2sinβx).

Example

Solve y3y+2y=0:

Characteristic equation: r23r+2=0,r=1,2.
y=C1ex+C2e2x.

Python Visualization

# Define ODE
otde2 = Eq(y(x).diff(x, 2) - 3*y(x).diff(x) + 2*y(x), 0)

# Solve ODE
solution2 = dsolve(otde2)
print(f"Solution: {solution2}")

# Plot
C1, C2 = 1, 1
x_vals = np.linspace(0, 5, 500)
y_vals = C1 * np.exp(x_vals) + C2 * np.exp(2 * x_vals)
plt.plot(x_vals, y_vals, label='y = C1 e^x + C2 e^{2x}')
plt.title('Solution of y'' - 3y' + 2y = 0')
plt.xlabel('x')
plt.ylabel('y')
plt.legend()
plt.grid()
plt.show()

4. Non-Homogeneous Linear Equations

Intuition

A second-order non-homogeneous ODE has the form:

ay+by+cy=G(x).

The solution is:

y=yh+yp

where:

  • yh: Solution of the homogeneous equation.
  • yp: Particular solution.

Example

Solve yy=ex:

yh=C1ex+C2ex,yp=12xex.
y=C1ex+C2ex+12xex.

Python Visualization

# Define ODE
otde3 = Eq(y(x).diff(x, 2) - y(x), exp(x))

# Solve ODE
solution3 = dsolve(otde3)
print(f"Solution: {solution3}")

# Plot
C1, C2 = 1, 1
x_vals = np.linspace(0, 5, 500)
y_vals = C1 * np.exp(x_vals) + C2 * np.exp(-x_vals) + 0.5 * x_vals * np.exp(x_vals)
plt.plot(x_vals, y_vals, label='y = C1 e^x + C2 e^{-x} + 0.5x e^x')
plt.title('Solution of y'' - y = e^x')
plt.xlabel('x')
plt.ylabel('y')
plt.legend()
plt.grid()
plt.show()