Numerical Solution of Differential Equations in MATLAB: Video Tutorial
Solve initial value, boundary value and delay differential equations in MATLAB. Follow oscillator examples and check tolerances, boundary conditions and history functions.

On this page
This video tutorial reviews the numerical solution of differential equations and implements it in MATLAB. It is part of the Numerical Computations course.
Three kinds of problems
The ODE and BVP examples use systems of first order equations:
Here is the independent variable and can be a vector. An equation of higher order is first rewritten as a first order system. For example, becomes two equations with and :
The three videos then treat three kinds of problems:
- Initial value problems (ODEs): the state is known at the start time, .
- Boundary value problems (BVPs): conditions are given at both ends of an interval .
- Delay differential equations (DDEs): the rate of change also depends on past states, such as .
Watch online
The three sections are available on YouTube and embedded below as a playlist.
What each video covers
Section 1: Ordinary differential equations
Solving Ordinary Differential Equations (YouTube (opens in a new tab)). The video first sorts the MATLAB ODE solvers into two groups:
- Nonstiff solvers:
ode45,ode23andode113. Tryode45first, as it solves most problems well.ode23can be better with crude tolerances or moderate stiffness, andode113with strict tolerances or a model that is expensive to evaluate. - Stiff solvers:
ode15s,ode23s,ode23tandode23tb. The video compares these methods on its examples. Their relative speed and accuracy depend on the equations, tolerances and solver settings, so there is no fixed ranking for every model.
The first example is with on . A solver call needs the function, the time span and the initial condition:
f = @(t, y) -y + 2*t;
[t, y] = ode45(f, [0 2], 1);
plot(t, y)The video changes the initial condition and compares ode45 with ode23 (which gives a less smooth result) and ode113.
The second example is the van der Pol oscillator:
It becomes a first order system with and . The model is written as a function with as an extra parameter, and an anonymous function passes the value of to it. A phase plane plot of against shows the limit cycle. With the system is a pure oscillator and the phase plot is a circle. As grows the model becomes stiff, ode45 slows down, and ode15s solves it faster.
The third example is the Lorenz system, a chaotic model with three states:
With , , and the initial state , ode45 and plot3 show the Lorenz attractor.
Section 2: Boundary value problems
Solving Boundary Value Problems (YouTube (opens in a new tab)). MATLAB solves BVPs with bvp4c and bvp5c, which use collocation methods of fourth and fifth order. They are called in the same way. Besides the ODE function, you write a function for the boundary conditions in the form , and you give an initial guess built with bvpinit.
The first example is
which becomes and . The boundary condition function returns [ya(1) - 1; yb(1)].
f = @(x, y) [y(2); -y(1)];
bc = @(ya, yb) [ya(1) - 1; yb(1)];
solinit = bvpinit(linspace(0, pi/2, 100), [1 0]);
sol = bvp4c(f, bc, solinit);
plot(sol.x, sol.y(1, :))The solution sol has the fields x, y and yp. Changing a boundary condition changes the mathematical problem. Depending on the equations and the new conditions, a BVP may have one solution, several solutions or no solution, and the initial guess can affect which solution a numerical solver finds.
The second example has a condition on the derivative: the equation on with and . Here the second condition uses the second state, yb(2) + 1.
Section 3: Delay differential equations
Solving Delayed Differential Equations (YouTube (opens in a new tab)). A DDE also needs a history: the solution for all times before the start. MATLAB has three solvers:
dde23for constant delays.ddesdfor delays that depend on time or on the state.ddensdfor neutral equations, where the derivative also appears with a delay.
The video solves four examples:
- The scalar equation with the history , solved with
dde23on . The functiondevalevaluates the solution at chosen times. The video also tries other histories, such as . - A system of three equations with two constant delays, 1 and 0.5, and the history . Each column of the lag matrix holds the states at one delay.
- A system with a state dependent delay, solved with
ddesd:The video compares the result with and and starts at 0.1 rather than 0. The displayed comparison functions are not defined at . A DDE history must also be defined at every past time the solver requests. Check the delay arguments and the history domain when adapting this example. - A neutral equation, solved with
ddensd:Its exact solution and history is . The video plots the numerical and exact solutions together, and the error is small.
Applications
- Simulating dynamic systems such as oscillators, circuits and chaotic models.
- Boundary value problems in mechanics and heat transfer, such as the shape of a beam or a temperature profile.
- Models with delays, for example in control systems and population dynamics.
The Runge-Kutta video tutorial shows how a classic ODE method works inside, in Python and MATLAB.
Check the solution
Compare results at tighter tolerances and inspect initial or boundary residuals. A convincing plot alone does not establish accuracy. For a delay model, verify that the history matches the intended past states and uses the same state ordering as the derivative function. In chaotic systems such as Lorenz, trajectories can separate over time even when both runs are numerically reasonable.
How to cite
If you use this code or tutorial in your work, articles or publications, please cite it.
Mostapha Kalami Heris, Numerical Solution of Differential Equations in MATLAB: Video Tutorial (YPTNM190520S17), Yarpiz, 2019. https://yarpiz.com/numerical-solution-of-differential-equations-video-tutorial-yptnm190520s17@misc{yarpiz-yptnm190520s17,
author = {{Mostapha Kalami Heris}},
title = {{Numerical Solution of Differential Equations in MATLAB: Video Tutorial}},
howpublished = {Yarpiz},
year = {2019},
note = {Code YPTNM190520S17},
url = {https://yarpiz.com/numerical-solution-of-differential-equations-video-tutorial-yptnm190520s17}
}Published Updated

Comments