Professional Documents
Culture Documents
Solving ODEs in MATLAB PDF
Solving ODEs in MATLAB PDF
Solving ODEs in MATLAB PDF
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
second order
Methods for solving first order ODEs: Separable equations
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
𝑑𝑥
dydx = x*y;
function simple_ode(y0)
plot(x,y,’b-x’);
MATLAB script for solving the ODE: simple_ode.m
function simple_ode(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
𝑑𝑥
function coupled_ode()
y10 = 1; y20 = 0;
legend(‘y_1’, ‘y_2’);
MATLAB script for solving coupled ODEs: coupled_ode.m
function coupled_ode()
y10 = 1; y20 = 0;
legend(‘y_1’, ‘y_2’);
• Generate a plot to compare the numerically calculated and the exact solution
Assignment Problems
Problem 1:
𝑑𝑦 1
= 𝑡𝑦 4 + 2𝑦
𝑑𝑡 6
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
𝑑𝑥
= 𝑟𝑎𝑡𝑒 𝑎𝑡 𝑤ℎ𝑖𝑐ℎ 𝑠𝑎𝑙𝑡 𝑒𝑛𝑡𝑒𝑟𝑠 − 𝑟𝑎𝑡𝑒 𝑎𝑡 𝑤ℎ𝑖𝑐ℎ 𝑠𝑎𝑙𝑡 𝑙𝑒𝑎𝑣𝑒𝑠
𝑑𝑡