Solving ODEs in MATLAB
MM 612 Computational Lab
Ajay Singh Panwar
Spring 2019
Ordinary differential equations: Examples
Newton’s equation of motion for an first order
object of mass m falling under the
influence of gravity
second order
Modified equation assuming that a
drag/ resisting force is acting on the
falling object first order
second order
• Only one independent variable, t or x
• All derivatives are ordinary derivatives second order
• The order of the equation is the order of the
highest derivative
second order
Methods for solving first order ODEs: Separable equations
Subject to an initial condition
y(0) = const
Intial Value Problem
Separable equations: Initial Value Problem
Solving ODEs in MATLAB: ode45
[x, y] = ode45(@odefun, tspan, y0)
This solver is based on multi-step methods, such as, the Runge-Kutta method for solving ODEs
𝑑𝑦
@odefun: a function handle that returns the value of the derivative, , for a given x and y
𝑑𝑥
tspan: the range of x for which the ODE has to be solved
y0: the initial condition for y, 𝑦 𝑥0 = 𝑦0
Using ode45
𝑑𝑦
Solve = 𝑥𝑦, subject to the initial condition 𝑦 0 = 1
𝑑𝑥
• First define the derivative, odefun is xy
function dydx = derivative(x,y)
dydx = x*y;
• Solve the differential equation using ode45
%Function to solve dydx = xy
function simple_ode(y0)
[x,y] = ode45(@derivative, [0,1], y0);
plot(x,y,’b-x’);
MATLAB script for solving the ODE: simple_ode.m
%Function to solve dydx = xy
function simple_ode(y0)
[x,y] = ode45(@derivative, [0,1], y0);
plot(x,y,’b-x’);
Run the code for y0 = 1
function dydx = derivative(x,y)
dydx = x*y;
Comparison with the exact analytical solution
2 Τ2
• The exact solution for the equation is 𝑦 = 𝑒𝑥
• Generate a plot to compare the numerically calculated and the exact solution
Solving coupled ODEs
𝑑𝑦1
= −𝑦2 Initial conditions: 𝑦1 0 = 1 and 𝑦2 0 = 0
𝑑𝑥
𝑑𝑦2 Solve over the interval [0,5]
= 𝑦1
𝑑𝑥
Here Y and dYdx are now vectors
• First define the function that returns the derivative
function dYdx = coupled(x,Y)
dYdx = [-Y(2); Y(1)]
Solving coupled ODEs
Solve the system of coupled equations using ode45
function coupled_ode()
y10 = 1; y20 = 0;
[x,Y] = ode45(@coupled, [0,5], [y10,y20]);
plot(x, Y(:,1), ‘b’, x, Y(:,2), ‘r’);
legend(‘y_1’, ‘y_2’);
MATLAB script for solving coupled ODEs: coupled_ode.m
Solve the system of coupled equations using ode45
function coupled_ode()
y10 = 1; y20 = 0;
[x,Y] = ode45(@coupled, [0,5], [y10,y20]);
plot(x, Y(:,1), ‘b’, x, Y(:,2), ‘r’);
legend(‘y_1’, ‘y_2’);
function dYdx = coupled(x,Y)
dYdx = [-Y(2); Y(1)];
Comparison with the exact analytical solution
• The exact solution for the equation is 𝑦1 = cos 𝑥 𝑦2 = sin 𝑥
• Generate a plot to compare the numerically calculated and the exact solution
Assignment Problems
Problem 1:
Solve the Bernoulli differential equation
𝑑𝑦 1
= 𝑡𝑦 4 + 2𝑦
𝑑𝑡 6
subject to the intitial condition, y = -2 at t = 0.
Assignment Problems
Problem 2:
A tank contains 200 litres of brine in which 40 kgs of salt are dissolved. Beginning at
time, t = 0, brine containing 2 kg of salt per litre flows in at the rate of 8 litres per
minute, and the mixture (which is kept uniform by stirring) flows out at the same rate.
When will there be 60 kgs of dissolved salt in the tank?
If x = x(t) is the number of kgs of dissolved salt in the tank at any time t > 0, the
concentration is x/200 kgs/litre . The rate of change of x is
𝑑𝑥
= 𝑟𝑎𝑡𝑒 𝑎𝑡 𝑤ℎ𝑖𝑐ℎ 𝑠𝑎𝑙𝑡 𝑒𝑛𝑡𝑒𝑟𝑠 − 𝑟𝑎𝑡𝑒 𝑎𝑡 𝑤ℎ𝑖𝑐ℎ 𝑠𝑎𝑙𝑡 𝑙𝑒𝑎𝑣𝑒𝑠
𝑑𝑡