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

subscripted assignment dimension mismatch

clc;
clear all;
close all;
to=0;
tf=2;
d=0.01;
n=tf/d;
c=5;
y=zeros(1,n);
r=zeros(1,n);
x=zeros(2,n);
s=zeros(1,n);
t=zeros(1,n);
e=zeros(2,n);
M=0.75;
thr=30; % rest position of the knee
% v(1)=0;
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=.5;
u=zeros(1,n);
u(1,1)=0;
for i=1:n+1
dx1=x(2,i);
dx2=(-Tg*cos(x(1,i)-A*sign(s)*x(2,i)-B*x(2,i)-K*(x(1,i)-thr)+K1+u(1,i))/J);
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)); %#ok<AGROW>

xd(2,(i+1))=sin(t(1,i)); %#ok<AGROW>
e(1,(i+1))=xd(1,i+1)-x(1,i+1);
e(2,(i+1))=xd(2,i+1)-x(2,i+1);
s(1,(i+1))=c*e(1,1+i)+e(2,1+i);
u(1,i)=J*c*cos(t(1,i))-J*c*x(2,i)+J*cos(t(1,i))+Tg*cos(x(1,i)+A*sign(s)*x(2,i)+B*x(2,i)+K(x(1,i)-thr)-K1);
u(1,i+1)=u(1,i)-M*sign(s(1,i));
y(1,i+1)=x(1,i+1);
end
figure(1)
plot(t(1,:),y(1,:),'r');
xlabel('time(s)');ylabel('Position tracking');
%ylabel('sliding surface');
%xlabel('time(sec)*10^-3');
%hold on;
figure(2)
plot(t(1,:),e(1,:));
xlabel('time(s)');ylabel('Error tracking');
grid;

Best Answer

  • dx2=(-Tg*cos(x(1,i)-A*sign(s)*x(2,i)-B*x(2,i)-K*(x(1,i)-thr)+K1+u(1,i))/J)
    dx2 is the same size as sign(s) which in turn is the same size as s, that is a 1 x n vector. Of course, you can't assign a vector to a single value.
    Possibly you meant sign(s(1, i)), which is equivalent to sign(s(i))