MATLAB Answers

Solve equations of Motion using Matlab ODE45

43 views (last 30 days)
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

  0 Comments

Sign in to comment.

Accepted Answer

James Tursa
James Tursa on 9 Apr 2020
Edited: James Tursa on 9 Apr 2020
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.

  2 Comments

Brian Peoples
Brian Peoples on 9 Apr 2020
in the function where you write dydt = [y(3);y(4);something;something];
can you further explain that?
James Tursa
James Tursa on 9 Apr 2020
The "something,something" is for you to fill in. You solve the matrix differential equations for xdotdot and thetadotdot, and those expressions go into the "something,something"

Sign in to comment.

More Answers (0)

Sign in to answer this question.


Translated by