To solve a second order differential equation with initial conditions using matrix method
Show older comments
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
1 Comment
madhan ravi
on 4 Jun 2020
Edited: madhan ravi
on 4 Jun 2020
Is it your homework ? What did you try so far? https://www.mathworks.com/help/symbolic/massmatrixform.html
Answers (1)
Ameer Hamza
on 4 Jun 2020
Edited: Ameer Hamza
on 4 Jun 2020
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
Find more on Symbolic Math Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!