To solve a second order differential equation with initial conditions using matrix method

Consider a system governed by a second ODE y''+6y'+5y = 8*exp(-t) with the initial conditions y(0)=y'(0)=0, I need a matlab code to solve the equations using matrix methods

Answers (1)

Try the following code using ode45 (a numerical solver). Also, see this example from the documentation: https://www.mathworks.com/help/matlab/ref/ode45.html#bu3uj8b
[t, y] = ode45(@odeFun, [0 10], [0; 0]);
plot(t, y, 'o-')
function dydt = odeFun(t, y)
A = [0 1;
-5 -6];
B = [0; 8];
u = exp(-t);
dydt = A*y + B*u;
end
Alternative method using symbolic toolbox
syms y(t)
eq = diff(y, 2) + 6*diff(y, 1) + 5*y == 8*exp(-t);
odeFun = matlabFunction(odeToVectorField(eq), 'Vars', {'t', 'Y'});
ode45(odeFun, [0 10], [0; 0])

Categories

Edited:

on 4 Jun 2020

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!