我遇到以下代码问题。接下来是缩短的,但它试图代表一连串的四个摆锤。
% Simulation of double pendulum by Lagrangian mechanics
% see also http://www.eng.auburn.edu/~marghitu/MECH6710/06_Fall/MATLAB/C6_analytical/C6_MATLAB.pdf
close all
% generalized coordinates==================================================
syms t real
theta=sym('theta(t)');
phi=sym('phi(t)');
alpha=sym('alpha(t)'); %tercer angulo
gamma=sym('gamma(t)'); %cuarto ángulo
betha=sym('betha(t)'); %quinto ángulo
% constants, length, mass, g===============================================
L1=7; %lengths of all the pendulums
L2=6;
L3=6;
L4=6;
L5=6;
m1=3; %mass of all the particles
m2=3;
m3=3;
m4=3;
m5=3;
g=9.81;
i=0;
% positions and velocities as function of the generalized coordinates======
x1=L1*sin(theta);
y1=-i-L1*cos(theta);
x2=x1+L2*sin(phi);
y2=y1-L2*cos(phi);
x3=x2+L3*sin(alpha);
y3=y2-L3*cos(alpha);
x4=x3+L4*sin(gamma);
y4=y3-L4*cos(gamma);
x5=x4+L5*cos(betha);
y5=y4-L5*sin(betha);
x1dot=diff(x1,t);
x2dot=diff(x2,t);
x3dot=diff(x3,t);
x4dot=diff(x4,t);
x5dot=diff(x5,t);
y1dot=diff(y1,t);
y2dot=diff(y2,t);
y3dot=diff(y3,t);
y4dot=diff(y4,t);
y5dot=diff(y5,t);
% kinetic and potential energy=============================================
T=m1/2*(x1dot^2+y1dot^2)+m2/2*(x2dot^2+y2dot^2)+m3/2*(x3dot^2+y3dot^2)+m4/2*(x4dot^2+y4dot^2)+m5/2*(x5dot^2+y5dot^2);
V=m1*g*y1+m2*g*y2+m3*g*y3+m4*g*y4+m5*g*y5;
% Lagrangian ==============================================================
L=T-V;
% dL/d(qdot)===============================================================
dummy=sym('dummy');
dLthetadot = subs(diff(subs(L,diff(theta,t),dummy),dummy),dummy,diff(theta,t));
dLphidot = subs(diff(subs(L,diff(phi,t),dummy),dummy),dummy,diff(phi,t));
dLalphadot= subs(diff(subs(L,diff(alpha,t),dummy),dummy),dummy,diff(alpha,t));
dLgammadot=subs(diff(subs(L,diff(gamma,t),dummy),dummy),dummy,diff(gamma,t));
dLbetadot=subs(diff(subs(L,diff(betha,t),dummy),dummy),dummy,diff(betha,t));
% dL/dq====================================================================
dLdtheta=subs(diff(subs(L,theta,dummy),dummy),dummy,theta);
dLdphi=subs(diff(subs(L,phi,dummy),dummy),dummy,phi);
dLdalpha=subs(diff(subs(L,alpha,dummy),dummy),dummy,alpha);
dLdgamma=subs(diff(subs(L,gamma,dummy),dummy),dummy,gamma);
dLdbeta=subs(diff(subs(L,betha,dummy),dummy),dummy,betha);
% dFdq=====================================================================
k=0.5; % dissipation constant
F=1/2*k*(x1dot^2+y1dot^2+x2dot^2+y2dot^2+x3dot^2+y3dot^2+x4dot^2+y4dot^2+x5dot^2+y5dot^2);
dFdthetadot=subs(diff(subs(F,diff(theta,t),dummy),dummy),dummy,diff(theta,t));
dFdphidot=subs(diff(subs(F,diff(phi,t),dummy),dummy),dummy,diff(phi,t));
dFdalphadot=subs(diff(subs(F,diff(alpha,t),dummy),dummy),dummy,diff(alpha,t));
dFdgammadot=subs(diff(subs(F,diff(gamma,t),dummy),dummy),dummy,diff(gamma,t));
dFdbetadot=subs(diff(subs(F,diff(betha,t),dummy),dummy),dummy,diff(betha,t));
% generalized equations of motion==========================================
differentialequation1=diff(dLthetadot,t)-dLdtheta+dFdthetadot;
differentialequation2=diff(dLphidot,t)-dLdphi+dFdphidot;
differentialequation3=diff(dLalphadot,t)-dLdalpha+dFdalphadot;
differentialequation4=diff(dLgammadot,t)-dLdgamma+dFdgammadot;
differentialequation5=diff(dLbetadot,t)-dLdbeta+dFdbetadot;
% abbreviation of variables================================================
variables={theta,phi,alpha,gamma,betha,diff(theta,t),diff(phi,t),diff(alpha,t),diff(gamma,t),diff(betha,t),diff(theta,t,2),diff(phi,t,2),diff(alpha,t,2),diff(gamma,t,2),diff(betha,t,2)};
variablesshort={'x(1)','x(2)','x(3)','x(4)','x(5)','x(6)','x(7)','x(8)','x(9)','x(10)','thetaddot','phiddot','alphaddot','gammaddot','betaddot'};
DifferentialEquation1=subs(differentialequation1,variables,variablesshort);
DifferentialEquation2=subs(differentialequation2,variables,variablesshort);
DifferentialEquation3=subs(differentialequation3,variables,variablesshort);
DifferentialEquation4=subs(differentialequation4,variables,variablesshort);
DifferentialEquation5=subs(differentialequation5,variables,variablesshort);
% solve for thetaddot, phiddot=============================================
solution=solve(DifferentialEquation1,DifferentialEquation2,DifferentialEquation3,DifferentialEquation4,DifferentialEquation5,'thetaddot','phiddot','alphaddot','gammaddot','betaddot');
THETADDOT=solution.thetaddot;
PHIDDOT=solution.phiddot;
ALPHADDOT=solution.alphaddot;
GAMMADDOT=solution.gammaddot;
BETADDOT=solution.betaddot;
% solve non linear ode system==============================================
time=linspace(0,30,2000);
% initial conditions [theta,phi,thetadot,phidot]===========================
x0=[pi/4 0 0 0 0 0.5 0 0 0 0];
str=['xdot=@(t,x)[;x(6);x(7);x(8);x(9);x(10);',char(THETADDOT),';',char(PHIDDOT),';',char(ALPHADDOT),';',char(GAMMADDOT),';',char(BETADDOT),'];'];
eval(str);
[t,q]=ode23(xdot,time,x0);
% Calculute positions as function of generalized coordinates===============
X1=L1*sin(q(:,1));
Y1=-L1*cos(q(:,1));
X2=X1+L2*sin(q(:,2));
Y2=Y1-L2*cos(q(:,2));
X3=X2+L3*sin(q(:,3));
Y3=Y2-L3*cos(q(:,3));
X4=X3+L4*sin(q(:,4));
Y4=Y3-L4*cos(q(:,4));
X5=X4+L5*sin(q(:,5));
Y5=Y4-L5*cos(q(:,5));
% plot solution============================================================
set(gcf,'color',[1 1 1])
hold on
box on
axis equal
for i=1:numel(time)
cla;
plot([0,X1(i)],[0,Y1(i)],'k','linewidth',4);
plot(X1(i),Y1(i),'o','MarkerFaceColor','k','MarkerEdgeColor','k','MarkerSize',4*m1);
plot([X1(i),X2(i)],[Y1(i),Y2(i)],'k','linewidth',4);
plot(X2(i),Y2(i),'o','MarkerFaceColor','k','MarkerEdgeColor','k','MarkerSize',4*m2);
plot([X2(i),X3(i)],[Y2(i),Y3(i)],'k','linewidth',4);
plot(X3(i),Y3(i),'o','MarkerFaceColor','k','MarkerEdgeColor','k','MarkerSize',4*m3);
plot([X3(i),X4(i)],[Y3(i),Y4(i)],'k','linewidth',4);
plot(X4(i),Y4(i),'o','MarkerFaceColor','k','MarkerEdgeColor','k','MarkerSize',4*m4);
plot([X4(i),X5(i)],[Y4(i),Y5(i)],'k','linewidth',4);
plot(X5(i),Y5(i),'o','MarkerFaceColor','k','MarkerEdgeColor','k','MarkerSize',4*m5);
% plot(X2(1:i),Y2(1:i),'g-'); %aqui pinta la trayectoria de la bola 2
axis([-8,8,-1.05*(L1+L2+L3+L4+L5),0]);
drawnow
end
我看到的错误是下一个:
未定义的函数或变量'矩阵'。 QuadruplePendulum2D中的错误> @(t,x)[x(5); x(6); x(7); x(8);矩阵(0,1,[]);矩阵(0,1,[]) ;矩阵(0,1,[]);矩阵(0,1,[])]
odearguments中的错误(第87行)f0 = feval(ode,t0,y0,args {:}); %ODE15I将args {1}设置为yp0
odearguments中的错误(第87行)f0 = feval(ode,t0,y0,args {:}); %ODE15I将args {1}设置为yp0。
ode23(第114行)odearguments(FcnHandlesUsed,solver_name,ode,tspan,y0,options,varargin)中的错误;
QuadruplePendulum2D中的错误(第86行)[t,q] = ode23(xdot,time,x0);
有人可以帮忙解决这个错误吗?感谢