Lecture 5 - Part 2 - Solving ODEs in GNU Octave

Download as pdf or txt
Download as pdf or txt
You are on page 1of 12

Solving ODEs in GNU Octave

MM 305: Kinetics of Materials

Ajay Singh Panwar and N. Venkataramani

Fall 2020

MEMS, IIT Bombay


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 GNU Octave: lsode

[x] = lsode(fcn, x0, tspan)


for a differential equation

𝑑𝑥
= 𝑓 𝑥, 𝑡
𝑑𝑡

This command uses Hindmarsh’s ODE solver LSODE

𝑑𝑥
• fcn: a function handle that returns the value of the derivative, , for a given t and x
𝑑𝑡

• x0: the initial condition for x, 𝑥 𝑡0 = 𝑥0

• tspan: the range of t for which the ODE has to be solved


Using lsode
𝑑𝑥
Solve = 𝑡𝑥, subject to the initial condition x 0 = 1
𝑑𝑡

• First define the derivative, 𝑓 = 𝑡𝑥

function xdot = f(x,t) NOTE: The function must have the form
xdot = f(x,t)
xdot = t*x;

endfunction

• Solve the differential equation using lsode

x = lsode(“f”, x0, linspace(0,1,10)’);

plot(t,x,’b-x’);
GNU Octave script for solving the ODE: simple_ode.m

%Function to solve dxdt = tx

x0=1.0; Run the code for x0 = 1


t=linspace(0,1,10)’;

function xdot = f(x,t)


xdot = t*x;
endfunction

x = lsode(“f”, x0, t);

plot(t, x, ’b-x’);
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 x and dxdt are now vectors

• First define the function that returns the derivative Both dx/dt and x are
2 × 1 column vectors
function xdot = f(x,t)
xdot(1) = -x(2); 𝑑𝑥1 Τ𝑑𝑡 −𝑥2
xdot(2) = x(1); = 𝑥
endfunction
𝑑𝑥2 Τ𝑑𝑡 1
Solving coupled ODEs

Solve the system of coupled equations using lsode

x10 = 1; x20 = 0;

x = lsode(“f”, [x10;x20], linspace(0,5,50)’);

plot(t, x(:,1), ‘b’, t, x(:,2), ‘r’);

legend(‘x_1’, ‘x_2’);
MATLAB script for solving coupled ODEs: coupled_ode.m

Solve the system of coupled equations using ode45

%Solving a system of coupled ODEs


x10 = 1; x20 = 0;

function xdot = f(x,t)


xdot(1) = -x(2);
xdot(2) = x(1);
endfunction

x = lsode(“f”, [x10;x20], linspace(0,5,50)’);

plot(t, x(:,1), ‘b’, t, x(:,2), ‘r’);


xlabel(“x”, “fontsize”, 14);
ylabel(“y”, “fontsize”, 14);
legend(‘x_1’, ‘x_2’);
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

You might also like