symbolic calculations for physical system doesn't give apropriated answer.

1 view (last 30 days)
Hello,
I'm trying to solve the equations of motion of a triple inverted pendulum with matlab as in figure below
my code is as follows :
clear variables
digits(5);
syms phi1(t)
syms phi2(t)
syms phi3(t)
syms s(t)
syms u(t)
d1=0.215;
d2=0.002;
d3=0.002;
J1=0.013;
J2=0.024;
J3=0.018;
a1=0.215;
a2=0.269;
a3=0.226;
g=9.81;
l1=0.323;
l2=0.419;
m1=0.876;
m2=0.938;
m3=0.553;
mc=1; %actually we I didn't find the mass of the cart.
R=0.5*d1*diff(phi1,t)^2+0.5*d2*(diff(phi2,t)-diff(phi1,t))^2+0.5*d3*(diff(phi3,t)-diff(phi2,t))^2;
q=[phi1;phi2;phi3;s];
qdot=diff(q,t);
q=formula(q);
qdot=formula(qdot);
pc0=[s;0]; %position of the cart
pc1=[s-a1*sin(phi1);a1*cos(phi1)];
pc2=[s-l1*sin(phi1)-a2*sin(phi2);l1*cos(phi1)+a2*cos(phi2)];
pc3=[s-l1*sin(phi1)-l2*sin(phi2)-a3*sin(phi3);l1*cos(phi1)+l2*cos(phi2)+a3*cos(phi3)];
yc1=[0,1]*pc1;
yc2=[0,1]*pc2;
yc3=[0,1]*pc3;
vc1=diff(pc1,t);
vc2=diff(pc2,t);
vc3=diff(pc3,t);
vc1Norm2=transpose(vc1)*vc1;
vc1Norm2=simplify(vc1Norm2);
vc2Norm2=transpose(vc2)*vc2;
vc2Norm2=simplify(vc2Norm2);
vc3Norm2=transpose(vc3)*vc3;
vc3Norm2=simplify(vc3Norm2);
V=g*(m1*yc1+m2*yc2+m3*yc3);
T=0.5*(mc*diff(s,t)^2+m1*vc1Norm2+m2*vc2Norm2+m3*vc3Norm2+J1*diff(phi1,t)^2+J2*diff(phi2,t)^t+J3*diff(phi3,t)^2);
T=simplify(T);
L=T-V;
L=simplify(L);
R=0.5*(d1*diff(phi1,t)^2+d2*(diff(phi2,t)-diff(phi1,t))^2+d3*(diff(phi3,t)-diff(phi2,t))^2);
eulerLagrange=@(f,t,x,xd) diff(diffDepVar(f,xd),t)-diffDepVar(f,x)+diffDepVar(R,xd);
dL1=eulerLagrange(L,t,phi1,diff(phi1,t));
eqn1=dL1==0;
eqn1=simplify(eqn1);
dL2=eulerLagrange(L,t,phi2,diff(phi2,t));
eqn2=dL2==0;
eqn2=simplify(eqn2);
dL3=eulerLagrange(L,t,phi3,diff(phi3,t));
eqn3=dL3==0;
eqn3=simplify(eqn3);
eqn4=u==diff(diff(s,t),t);
[V,Y]=odeToVectorField(eqn1,eqn2,eqn3,eqn4);
V=subs(V,u,0);
f=matlabFunction(V,'vars',{'t','Y'});
tspan=[0,1];
y0=[0;0;0;0;0;0;pi/2;0];
[t,y]=ode45(f,tspan,y0);
for some reason, when I plot the answer y is a matrix containing more NaN than anything else. I thought that this may be due to the fact that f contains very big values (ex : 10^22). What do you think?

Accepted Answer

Mischa Kim
Mischa Kim on 24 Oct 2016
Check out the Euler-Lagrange tool package on File Exchange.
  4 Comments
edamondo
edamondo on 25 Oct 2016
I have tried this but if I type in the first exmample
syms g m x dx y dy z dz
L = m*(dx^2 + dy^2 + dz^2)/2 - m*g*z;
X = {x dx y dy z dz};
Q_i = {0 0 0}; Q_e = {0 0 0};
R = 0;
par = {g m};
VF = EulerLagrange(L,X,Q_i,Q_e,R,par);
I get the error:
Undefined function or variable 't'.
Error in EulerLagrange>@(ii)symfun(['q',int2str(ii),'(t)'],t)
Error in EulerLagrange (line 86) qt = arrayfun(@(ii) symfun(['q' int2str(ii) '(t)'],t),1:numcoor,'UniformOutput',false);
edamondo
edamondo on 25 Oct 2016
Edited: edamondo on 25 Oct 2016
Apparently I changed the code by error. It is working now

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!