Why do I keep getting Error using + Arrays have incompatible sizes for this operation. Error in solver_DE_1 (line 37) x_c=l*sin(​theta(ang,​1))+r*cos(​ang_ci);

3 次查看(过去 30 天)
Error using +
Arrays have incompatible sizes for this operation.
Error in solver_DE_1 (line 37)
x_c=l*sin(theta(ang,1))+r*cos(ang_ci);
Didnt get this error when i was running R2022a but now when i run R2022b it doesn't work. Is there any add ons i need?
clc
clear all
time=0:.01:15;
global l
[T,theta]=ode45(@vibdif,time,[pi/4;0;pi/4;0;pi/4;0;pi/4;0]);
theta(:,1)
figure(1)
plot(time,theta(:,1)*180/pi,'b',time,theta(:,3)*180/pi,'r','linewidth',2)
legend('LDE','NDE')
xlabel('Time(s)','fontsize',15)
ylabel('\theta(deg)','fontsize',15)
grid
% figure(2)
% plot(time,theta(:,5)*180/pi,'b','linewidth',2)
% xlabel('Time(s)','fontsize',15)
% ylabel('\theta(deg)','fontsize',15)
% grid
figure(3)
plot(time,theta(:,1)*180/pi,'b',time,theta(:,7)*180/pi,'r','linewidth',2)
legend('Sta','Mov')
xlabel('Time(s)','fontsize',15)
ylabel('\theta(deg)','fontsize',15)
grid
si=size(theta(:,1));
X_r=[-0.2 0.2 0.2 -0.2];
Y_r=[0 0 0.2 0.2];
ang_ci=0:0.1:2*pi;
r=0.06;
for ang=1:1:si(1,1)
x_c=l*sin(theta(ang,1))+r*cos(ang_ci);
y_c=-l*cos(theta(ang,1))+r*sin(ang_ci);
x_cn=l*sin(theta(ang,3))+r*cos(ang_ci);
y_cn=-l*cos(theta(ang,3))+r*sin(ang_ci);
figure(4)
plot([0;l*sin(theta(ang,1))],[0;-l*cos(theta(ang,1))],'b','linewidth',2)
hold on
plot([0;l*sin(theta(ang,3))],[0;-l*cos(theta(ang,3))],'r','linewidth',2)
hold on
fill(X_r,Y_r,'g')
hold on
text(0,-0.05,'O')
hold on
text(-1.5,-1.5,'Simulation for both linear and nonlinear','fontsize',14)
hold on
fill(x_c,y_c,'b')
hold on
fill(x_cn,y_cn,'r')
hold off
axis([-2 2 -2 0.3])
grid
end
  3 个评论
Drake Campo
Drake Campo 2022-9-27
function dm=vibdif(t,m);
global l
dm=zeros(8,1);
g=9.81;% this is gravity in SI system
l=1;% this is the lebbght of pendulum
dm(1)=m(2);
dm(2)=-(g/l)*m(1);%linearized DE around equli point
dm(3)=m(4);
dm(4)=-(g/l)*sin(m(3));%original nonlinear DE
dm(5)=m(6);
dm(6)=(g/l)*m(5);%inverted pendulum
dm(7)=m(8);
dm(8)=-(g/l)*m(8)+25*sin(5*t);%moving pendulum
Walter Roberson
Walter Roberson 2022-9-27
This code works for me in R2022b.
time=0:.01:15;
global l
[T,theta]=ode45(@vibdif,time,[pi/4;0;pi/4;0;pi/4;0;pi/4;0]);
theta(:,1)
figure(1)
plot(time,theta(:,1)*180/pi,'b',time,theta(:,3)*180/pi,'r','linewidth',2)
legend('LDE','NDE')
xlabel('Time(s)','fontsize',15)
ylabel('\theta(deg)','fontsize',15)
grid
% figure(2)
% plot(time,theta(:,5)*180/pi,'b','linewidth',2)
% xlabel('Time(s)','fontsize',15)
% ylabel('\theta(deg)','fontsize',15)
% grid
figure(3)
plot(time,theta(:,1)*180/pi,'b',time,theta(:,7)*180/pi,'r','linewidth',2)
legend('Sta','Mov')
xlabel('Time(s)','fontsize',15)
ylabel('\theta(deg)','fontsize',15)
grid
si=size(theta(:,1));
X_r=[-0.2 0.2 0.2 -0.2];
Y_r=[0 0 0.2 0.2];
ang_ci=0:0.1:2*pi;
r=0.06;
for ang=1:1:si(1,1)
x_c=l*sin(theta(ang,1))+r*cos(ang_ci);
y_c=-l*cos(theta(ang,1))+r*sin(ang_ci);
x_cn=l*sin(theta(ang,3))+r*cos(ang_ci);
y_cn=-l*cos(theta(ang,3))+r*sin(ang_ci);
figure(4)
plot([0;l*sin(theta(ang,1))],[0;-l*cos(theta(ang,1))],'b','linewidth',2)
hold on
plot([0;l*sin(theta(ang,3))],[0;-l*cos(theta(ang,3))],'r','linewidth',2)
hold on
fill(X_r,Y_r,'g')
hold on
text(0,-0.05,'O')
hold on
text(-1.5,-1.5,'Simulation for both linear and nonlinear','fontsize',14)
hold on
fill(x_c,y_c,'b')
hold on
fill(x_cn,y_cn,'r')
hold off
axis([-2 2 -2 0.3])
grid
end
function dm=vibdif(t,m);
global l
dm=zeros(8,1);
g=9.81;% this is gravity in SI system
l=1;% this is the lebbght of pendulum
dm(1)=m(2);
dm(2)=-(g/l)*m(1);%linearized DE around equli point
dm(3)=m(4);
dm(4)=-(g/l)*sin(m(3));%original nonlinear DE
dm(5)=m(6);
dm(6)=(g/l)*m(5);%inverted pendulum
dm(7)=m(8);
dm(8)=-(g/l)*m(8)+25*sin(5*t);%moving pendulum
end

请先登录,再进行评论。

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Programming 的更多信息

产品


版本

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by