How do I deal with the following error "??? Subscripted assignment dimension mismatch" occuring in line 32 of the following code..
古いコメントを表示
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;
採用された回答
その他の回答 (1 件)
Brendan Hamm
2015 年 3 月 11 日
It is on this line that you have an error:
x(2,(i+1))=x(2,i)+(d*dx2);
The issue is dx2 is a 1-by-200 double vector. You multiply by a scalar and add to a scalar which is causing a scalar expansion to occur, resulting in a 1-by-200 vector. Now you are trying to assign this to a single element of a matrix. My guess is that you wanted dx2 to be scalar, and the only vector used in the creation of dx2 occurring on line 30 is:
sign(s) % s is a vector of zeros (1-by-200)
カテゴリ
ヘルプ センター および File Exchange で Assembly についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!