MATLAB: Solve equations of Motion using Matlab ODE45

differential equationsequationfunctionmathematicsmatrixodeode45

Solve the following set of equations of motion using Matlab ODE45:
(m +m )x+m Lcos−m L2 sin+kx=01222
L + x cos + g sin = 0
Assume
m1=1 kg, m2=2 kg, L=1 m, k=1 N/m, g=10 m/s2.
Consider the following initial conditions:
x(0)=1x(0)=0(0)=1(0)=0
To enter this set of equations into your Matlab code, you need to re-write them in the first order form. That will give you 4 equations, and you will have to enter those equations into your ODE solver. You will have y(1), y(2), y(3) and y(4) as your unknowns.
Basically all we've done to solve 2nd order differential equations thus far was use a script and function only solving for one ode at a time w two initial conditions. Here would be my code for an older assignment:
script
R = 1;
g = 10; %coefficients
phi = 30;
tspan = [0 10]; % Time
initial_cond = [0,0]; %initial conditions
[t,y] = ode45(@(t,y)ODE_funct_second_order(t, y, R, g, phi),tspan,initial_cond);
theta = y(:,1);
L = -R.*y(:,1);
function
function dydt = ODE_fnct_second_order(t, y, L, g)
dydt = [y(2); -(3/2)*g*sin(y(1))/L];
end

Best Answer

You will have a 4-element state vector instead of 2.
initial_cond = [1;1;0;0];
[t,y] = ode45(@(t,y)ODE_funct_fourth_order(t, y, m1, m2, L, k, g),tspan,initial_cond);
with
function dydt = ODE_funct_fourth_order(t, y, m1, m2, L, k, g)
dydt = [y(3);y(4);something;something];
end
where y is
y(1) = x
y(2) = theta
y(3) = xdot
y(4) = thetadot
You get the expressions for dydt(3) and dydt(4) by solving (e.g. on paper or inside your derivative function using backslash \ operator) your matrix equations for xdotdot and thetadotdot.