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 views (last 30 days)
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 Comments
Drake Campo
Drake Campo on 27 Sep 2022
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 on 27 Sep 2022
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

Sign in to comment.

Answers (0)

Categories

Find more on Programming in Help Center and File Exchange

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!