MATLAB Answers

How to model and simulate a system using its EOM?

15 views (last 30 days)
Thomas Lillie
Thomas Lillie on 27 May 2021
Answered: Tanmay Das on 6 Aug 2021
I have two point-masses, each with mass ( m ), which are connected by a rigid, massless rod of length l . Mass 1 can move without friction on the vertical y-axis and mass 2 can move without friction on the horizontal x-axis, set up as below:
I've calculated and defined the EOM as:
syms g m l a da dda %a=angle, da=1st derivative of 1, dda=2nd derivative of a
%g=gravity, m=mass, l=hypotenuse length
r1 = [0 l*cos(a)]
r2 = [l*sin(a) 0]
dr1 = diff(r1, a)*da
dr2 = diff(r2, a)*da
T0 = 0.5*m*(dr1(1,2))^2 + 0.5*m*(dr2(1,1))^2;
T = simplify(T0)
V = m*g*l*cos(a)
L = T - V
M = jacobian(diff(L,da), a)*da + jacobian(diff(L,da), da) * dda - diff(L,a) == 0 %Euler-Lagrange equation
dda = solve(M, dda)
Which gives me dda = gsin(a)/l (where a is the angle between mass 1 and the y-axis). This is where I'm stuck.. how can I use Matlab to set up a function f(t, a) in order to track the position of each weight as they move due to gravity so that I can plot it?
I apologise for the open endedness of my question, I am very new and entirely self taught on how to use Matlab.

Answers (1)

Tanmay Das
Tanmay Das on 6 Aug 2021
The following code might solve your problem:
syms g m l a da dda a_var(t) %a=angle, da=1st derivative of 1, dda=2nd derivative of a
%g=gravity, m=mass, l=hypotenuse length, a_var = a as a function of time
r1 = [0 l*cos(a)];
r2 = [l*sin(a) 0];
dr1 = diff(r1, a)*da;
dr2 = diff(r2, a)*da;
T0 = 0.5*m*(dr1(1,2))^2 + 0.5*m*(dr2(1,1))^2;
T = simplify(T0);
V = m*g*l*cos(a);
L = T - V;
M = jacobian(diff(L,da), a)*da + jacobian(diff(L,da), da) * dda - diff(L,a) == 0; %Euler-Lagrange equation
dda = solve(M, dda);
%substitute symbolic variable a with time dependent symbolic variable a_var
ddaTime(t) = subs(dda,a,a_var);
%Write the differential equation
ode = diff(a_var,t,2) == ddaTime(t);
%-----------------------------------
%Place your initial conditions here
%cond1 = <Condition 1>;
%cond2 = <Condition 2>;
%conds = [cond1,cond2];
%aSol(t) = dsolve(ode,conds);
%-----------------------------------
aSol(t) = dsolve(ode);
Please note that in the above code, the symbolic variable ‘a’ has been substituted with the time dependent symbolic variable ‘a_var’. This is necessary to solve differential equation. You can refer to this link for further information. This will give you an array of time dependent symbolic variables which is the solution to the differential equation. In order to plot it, you need to substitute the symbolic variable with numeric values. You can look into subs Documentation for further information. Also, please note that in absence of initial conditions, there will be integration constants in the solution. So, you should put the initial conditions before solving it. To know more about solving differential equations, you can look into its documentation.

Community Treasure Hunt

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

Start Hunting!