The plot for t and ssdot in the following code cannot be displayed as it shows a vector length mismatch. Please suggest how I can get around it...

10 views (last 30 days)
if true
% clc;
clear all;
close all;
%to=0;
tf=10;
d=.001;
n=tf/d;
y=zeros(1,n);
r=zeros(1,n);
x=zeros(2,n);
s=zeros(1,n);
u=zeros(1,n);
v=zeros(1,n);
sdot=zeros(1,n);
ssdot=zeros(1,n);
t=zeros(1,n);
e=zeros(2,n);
c=23;
M=0.6;
u(1,1)=0;
for i=1:n+1
dx1=x(2,i);
dx2=3*x(1,i)+u(1,i);
x(1,(i+1))=x(1,i)+(d*dx1);
x(2,(i+1))=x(2,i)+(d*dx2);
t(1,(i+1))=t(1,i)+d;
xd(1,(i+1))=sin(t(1,i));
xd(2,(i+1))=cos(t(1,i));
e(1,(i+1))=x(1,i+1)-xd(1,i+1);
e(2,(i+1))=x(2,i+1)-xd(1,i+1);
s(1,(i+1))=c*e(1,1+i)+e(2,1+i);
v(1,i)=-sin(t(1,i))-10*x(1,i)+c*cos(t(1,i))-c*x(2,i);
u(1,i)=v(1,i)-M*sign(s(1,i));
sdot(1,i)=c*(x(2,i)-cos(t(1,i)))+dx2+sin(t(1,i));
ssdot(1,i)=s(1,i)*sdot(1,i);
u(1,i+1)=v(1,i)-M*sign(s(1,i));
y(1,i+1)=x(1,i+1);
y(2,i+1)=x(2,i+1);
end
figure(1)
plot(t(1,:),xd(1,:),'r',t(1,:),y(1,:),'b','linewidth',2);
legend('Ideal angular position','Actual Angular Position');
xlabel('time(s)');ylabel('Position tracking x1');
xlabel('e1');ylabel('e2');
figure(2)
plot(t(1,:),ssdot(1,:),'g','linewidth',2);
end

Accepted Answer

James Tursa
James Tursa on 17 Apr 2015
Edited: James Tursa on 17 Apr 2015
Your loop is
for i=1:n+1
So the last iteration increases the size of ssdot when this line is encountered:
ssdot(1,i)=s(1,i)*sdot(1,i);
And inside the loop you have this:
t(1,(i+1))=t(1,i)+d;
So the size of t is also increased inside the loop, by a different amount than ssdot.
Assuming you have all of these indexes correct, you could simply pick the shortest length and plot that much. E.g.,
k = min(numel(t),numel(ssdot));
plot(t(1,1:k),ssdot(1,1:k),'g','linewidth',2);

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!