MATLAB: How to deal with the “Subscripted assignment dimension mismatch” occuring in line 32 of the code

dimension mismatch

if true
clc;
clear all;
close all;
to=0;
tf=10;
r=0.001;
n=tf/r;
y=zeros(1,n);
d=zeros(1,n);
d_cap=zeros(1,n);
d_caret=zeros(1,n);
x=zeros(2,n);
s=zeros(1,n);
t=zeros(1,n);
e=zeros(2,n);
sdot=zeros(1,n);
ssdot=zeros(1,n);
c=100;
M=0;
thr=90; % rest position of the knee
J=.2639; % moment of inertia
Tg=8.57; %gravitational term
A=4.4289; %solid friction parameters
B=0.595; %viscous friction parameters
K=.3382;
k1=3;
k2=k1/J;
u=zeros(1,n);
u(1,1)=0;
v=zeros(1,n);
for i=1:n+1
d(1,(i+1))=rand(1,i);
%d_cap(1,(i+1))=rand(1,i);
dx1=x(2,i);
dx2=(-Tg*cos(x(1,i)-A*sign(s(1,i))*x(2,i)-B*x(2,i)-K*(x(1,i)-thr)+d(1,i)+u(1,i))/J);
x(1,(i+1))=x(1,i)+(r*dx1);
x(2,(i+1))=x(2,i)+(r*dx2);
t(1,(i+1))=t(1,i)+r;
xd(1,(i+1))=sin(t(1,i)); %#ok<AGROW>

xd(2,(i+1))=cos(t(1,i)); %#ok<AGROW>
e(1,(i+1))=x(1,i+1)-xd(1,i+1);
e(2,(i+1))=x(2,i+1)-xd(2,i+1);
s(1,(i+1))=c*e(1,1+i)+e(2,1+i);
d_caret(1,(i+1))=k2*(x(1,i)-d(1,i));
%sdot(1,(i+1))=c*(cos(t(1,i))-x(2,i)-sin(t(1,i))-dx2);
v(1,i)=J*c*cos(t(1,i))-J*c*x(2,i)-J*sin(t(1,i))+Tg*cos(x(1,i)+A*sign(x(2,i))+B*x(2,i)+K*(x(1,i)-thr)-d_caret(1,i));
%ssdot(1,i+1)=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('desired position','Actual Position');
xlabel('time(s)');ylabel('Position tracking x1');
figure(2)
plot(t(1,:),xd(2,:),'r',t(1,:),y(2,:),'b','linewidth',1);
legend('Desired angular speed','Actual angular speed');
xlabel('time(s)');ylabel('Angular velocity x2');
figure(3)
plot(t(1,:),e(1,:),'r',t(1,:),e(2,:),'b');
legend('Error Tracking for x1','Error tracking for x2');
xlabel('Time(sec)');ylabel('Error tracking');
figure(4)
plot(t(1,:),u(1,:),'k','linewidth',0.01);
xlabel('time(s))');ylabel('Control input(Torque(Nm))');
figure(5)
plot(e(1,:),e(2,:),'r',e(1,:),-c.*e(1,:),'b','linewidth',2)
xlabel('e1');ylabel('e2');
title('phase trajectory')
end

Best Answer

  • I believe that is this line:
    d(1,(i+1))=rand(1,i);
    Giving two arguments to rand creates (in this instance) a (1xi) vector. You are assigning it to a (1x1) scalar, and that won’t work.
    Do this instead:
    d(1,(i+1))=rand;
    to create a single random scalar value, and it should work. (I didn’t run your code.)