TP3 2
TP3 2
1. Brief Theory
The Euler method is a straightforward numerical technique used to solve ODEs and systems of ODEs. It is based on
the idea of approximating the solution by incrementally stepping through the ODE using discrete time intervals.
Given a first-order ODE in the form:
dy
dt = f (t, y), with the initial condition y(t0 ) = y0 .
The Euler method updates the solution at each time step tn using the formula:
yn+1 = yn + h . f (tn , yn )
The computation is performed in the interval [t0 , tf ] with a calculation step size of h, and tn+1 = tn + h.
dx
= xn + h . f1 (tn , xn , yn , zn )
dt
dy
For a system of two differential equations: = yn + h . f2 (tn , xn , yn , zn )
dt
dz = z + h . f (t , x , y , z )
n 3 n n n n
dt
The Euler method demonstrates local error proportional to the square of the step size (O(h 2 )), while its global error
is proportional to the step size (O(h)). This method serves as a foundational approach upon which more sophisticated
numerical techniques are built.
The Runge-Kutta (RK4) method is a numerical technique commonly used to solve ODEs. RK4 is a fourth-order method,
which means that its local error is on the order of O(h5 ) and the global error is on the order of O(h4 ), where h is the
step size. Here’s a brief overview of the RK4 method:
dy
The general form of a first-order ODE is given by: dt = f (t, y),
The RK4 method is an iterative procedure that updates the solution at each time step t n to obtain yn+1 .
The update formula for RK4 is as follows:
k1 = h . f (tn , yn )
h k1
k2 = h . f (tn + , yn + )
2 2
h k2
k3 = h . f (tn + , yn + )
2 2
k4 = h . f (tn + h, yn + k3 )
1
yn+1 = (k1 + 2k2 + 2k3 + k4 )
6
2. Required work
To be solved:
dy
• The ODE 1: dt + 2y = 2 − e−4t , on the interval [0, 0.5] with y(0) = 1;
dy t t
• The ODE 2: dt − y = − 12 e 2 sin(5t) + 5e 2 cos(5t) on the interval [0, 5] with y(0) = 0;
d2 y
• The ODE 3: dt2 + 2 dy
dt + y = t , on the interval [0, 5] with y(0) = 1 and
dy
dt (0) = 0;
• The ODE 4:
dy
= z, with y(0) = 1,
dt
dz = −y, z(0) = 0,
dt
This system represents harmonic oscillation, where y is the position and z is the velocity. The second equation
( dz
dt = −y) is analogous to Hooke’s law for a mass-spring system.
2. Write, for each of the two methods Euler and RK4, a Matlab program that solves the given ODEs with step sizes
h = 0.5, h = 0.1, h = 0.01, h = 0.001 and h = 0.0001.
The number N typically represents the total number of data points or time steps in your simulation, where
tf −t0
N= h +1
4. Draw in the same figure, the exact solution as well as the estimated solutions.
2 Error calculation is performed by comparing the numerical solutions with the analytically obtained solution.