Lecture 5 - Part 2 - Solving ODEs in GNU Octave
Lecture 5 - Part 2 - Solving ODEs in GNU Octave
Lecture 5 - Part 2 - Solving ODEs in GNU Octave
Fall 2020
second order
second order
Methods for solving first order ODEs: Separable equations
𝑑𝑥
= 𝑓 𝑥, 𝑡
𝑑𝑡
𝑑𝑥
• fcn: a function handle that returns the value of the derivative, , for a given t and x
𝑑𝑡
function xdot = f(x,t) NOTE: The function must have the form
xdot = f(x,t)
xdot = t*x;
endfunction
plot(t,x,’b-x’);
GNU Octave script for solving the ODE: simple_ode.m
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
𝑑𝑡
• 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
x10 = 1; x20 = 0;
legend(‘x_1’, ‘x_2’);
MATLAB script for solving coupled ODEs: coupled_ode.m
• Generate a plot to compare the numerically calculated and the exact solution