MATLAB: 2nd Order ODE by ode45

ode45

Hi community, i request assistance in getting the code for this particular question.
I tried watching yotube and looking around matlab answer but i still don't understand the approach in solving it.
Please advice, thanks.

Best Answer

Hi,
You need to treat your 2nd order differential equation as a system of two 1st order equations and arrange them in a vector, like:
f = [y; y']
such that
f' = [y'; y'']
Then you can obtain the solution using the following code
clc; clear; close all
[t,y] = ode45(@deriv, [0, 25], [0, 0]);
plot(t, y(:,1), t, y(:,2))
legend('y(t)', 'y''(t)')
function f_prime = deriv(t,f)
f_prime = zeros(2,1);
f_prime(1) = f(2);
f_prime(2) = 3*cos(t) -1.5*sin(t) - 3*f(2) - 3.25*f(1);
end
I myself also had problems to wrap my head around this approach when i learned it :)
// Per