Ordinary Differential equations
An ordinary equation of order n has the following form
Unfortunately, mathematicians use as independent variable. For a physicist a more natural choice would be since everything depends on time.
The acceleration of a body is the first derivative of velocity with respect to the time and equals to the force divided by mass
And we obtain the canonical form for the first order ODE
We define the following variables
We can rewrite order ODE as a system of first order ODEs
This is the system of equations and thus requires constraints.
With Cauchy boundary conditions, we specify i.e. initial conditions
If acceleration of the particle is given, then we find the position of the particle as a function of time by solving First, we need to convert it to the canonical form: the system of the first order ODEs.
so
We also need to provide the initial conditions:
position
and velocity
Let’s, for simplicity, consider a simple first order ODE (notice the lack of the vector notation)
There is an exact way to write the solution
The problem is that depends on itself.
However, on a small interval , we can assume that is constant.
Then, we can use the familiar box integration formula. In application to the ODE, this is called the Euler’s method.
Prompt
Using python write Euler method of ODE solver, follow ‘solve-ivp’ specs for fun, t_span, and y0. Save outcomes to this dir in file ‘euler.py’
We need to replace “time” with x variable to convert it to math notation.
import numpy as np
def euler_method(f, x_span, y0, num_steps):
"""
Solve an initial value problem using the Euler method.
Parameters:
f: callable, the derivative function dy/dx = f(x, y)
x_span: tuple, (x0, xf) the initial and final x points
y0: array-like, initial condition
num_steps: int, number of steps to take
Returns:
x: array of x points
y: array of solution values at each x point
"""
x0, xf = x_span
dx = (xf - x0) / num_steps
x = np.linspace(x0, xf, num_steps + 1)
y = np.zeros((num_steps + 1, len(y0)))
y[0] = y0
for i in range(num_steps):
y[i+1] = y[i] + dx * f(x[i], y[i])
return x, y
Let’s solve where
Analytical solution
import numpy as np
import matplotlib.pyplot as plt
from euler import euler_method
def example1_ode(x, y):
return -2 * y # Example: dy/dx = -2y, y(0) = 1
x_span = (0, 2)
y0 = [1]
num_steps = 100
x, y = euler_method(example1_ode, x_span, y0, num_steps)
plt.plot(x,y, label="Euler's method")
plt.plot(x,np.exp(-2*x), label="Analytical solution")
plt.title("Solution of y'=-2y, y(0)=1")
plt.legend()
plt.savefig("../pics/example1.png")
Let’s have a look at the first order ODE It has the following analytical solution If the initial condition , then the solution is
import numpy as np
import matplotlib.pyplot as plt
from euler import euler_method
def example2_ode(x, y):
return 3 * y - 4*np.exp(-x) # Example: dy/dx = 3y - 4*exp(-x), y(0) = 1
x_span = (0, 2)
y0 = [1]
num_steps = 100
x, y = euler_method(example2_ode, x_span, y0, num_steps)
plt.plot(x,y, label="Euler's method")
plt.plot(x,np.exp(-x), label="Analytical solution, exp(-x)")
plt.title("Solution of y'=3y-4*exp(-x), y(0)=1")
plt.legend()
plt.savefig("../pics/example2.png")
It is clear that the numerical solution diverges from the analytical solution. The problem is in the round off errors. From a computer point of view, . Thus, and the numerical solution diverges.
Do not trust the numerical solutions (regardless of a method) without proper consideration!
Let’s have a look at the first order ODE It has the following analytical solution If the initial condition , then the solution is
import numpy as np
import matplotlib.pyplot as plt
from scipy.integrate import solve_ivp
def example2_ode(x, y):
return 3 * y - 4*np.exp(-x) # Example: dy/dx = 3y - 4*exp(-x), y(0) = 1
x_span = (0, 2)
y0 = [1]
num_steps = 100
sol = solve_ivp(example2_ode, x_span, y0, t_eval=np.linspace(x_span[0], x_span[1], num_steps), method='RK45')
x = sol.t # note t !!!
y = sol.y
plt.plot(x.reshape([num_steps,1]),y.reshape([num_steps,1]), label="Runge-Kutta method of order 5(4)")
plt.plot(x,np.exp(-x), label="Analytical solution, exp(-x)")
plt.title("Solution of y'=3y-4*exp(-x), y(0)=1")
plt.legend()
plt.savefig("../pics/example3.png")
All we need is to split our interval into a bunch of steps of the size and leap frog from the first to the next one , then to the , and so on.
Now, we can make an easy transformation to the vector case (i.e. the order ODE)
Similarly to the boxes integration method, which is inferior in comparison to more advance methods, for example, the trapezoidal and Simpson’s, the Euler's method is very imprecise for a given and there are better ways.
Most notable improvements are the Runge-Kutta methods of 2nd and 4th order.