how to deal with the error "Attempted to access t(5001); index out of bounds because numel(t)=2000."
13 views (last 30 days)
Show older comments
if true
clc;
clear all;
close all;
to=0;
tf=2;
d=0.001;
n=tf/d;
y=zeros(1,n);
r=zeros(1,n);
x=zeros(4,n);
s=zeros(1,n);
v=zeros(1,n);
t=zeros(1,n);
e=zeros(4,n);
g=0.2;
M=0.005;
v(1)=0;
k1=0.1689;
k2=0.6785;
k3=0.0237;
k4=1;
u=zeros(1,n);
h=zeros(1,n);
a=-0.5;
b=0.5;
u(1,1)=0;
h(5001)=a;
for i=1:5000
u(i)=a;
end
for i=5001:15000
u(i)=t(i);
t(i+1)=t(i)+d;
end
for i=15001:20001
u(i)=b;
end
for i=1:(n+1)
r(1,1)=g*1.75;
r(1,(i+1))=g*1.75;
dx1=-8.376*x(1,i)-34.97*x(2,i)-4.14*x(3,i)-32.7*x(4,i)+108.77*(v(1,i));
dx2=2.54*x(1,i)-7.89*x(2,i)+77.75*(v(1,i));
dx3=-7.198*x(1,i)+3.36*x(2,i)-11.05*x(3,i)-87.2*x(4,i)+93.48*(v(1,i));
dx4=x(3,i);
x(1,(i+1))=x(1,i)+(d*dx1);
x(2,(i+1))=x(2,i)+(d*dx2);
x(3,(i+1))=x(3,i)+(d*dx3);
x(4,(i+1))=x(4,i)+(d*dx4);
t(1,(i+1))=t(1,i)+d;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
e(1,(i+1))=-x(1,(i+1));
e(2,(i+1))=-x(2,(i+1))+r(1,(i+1));
e(3,(i+1))=-x(3,(i+1));
e(4,(i+1))=-x(4,(i+1));
s(1,(i+1))=k1*e(1,(i+1))+k2*e(2,(i+1))+k3*e(3,(i+1))+k4*e(4,(i+1));
a=(-108.77*k1-77.75*k2-93.48*k3);
b=(-8.376*k1+2.54*k2-7.198*k3);
c=(-34.97*k1-7.89*k2+3.36*k3);
d=(-4.14*k1-11.05*k3+k4);
p=(-32.7*k1-87.2*k3);
v(1,(i+1))=((b*e(1,(i+1))+c*(e(2,(i+1)))+r(1,(i+1))+d*e(3,(i+1))+p*e(4,(i+1)))/a)-(M*sign(s(i)));
y(1,(i+1))=0.4419*x(3,(i+1))+3.98*x(4,(i+1));
end plot(t(1,:),y(1,:),'r'); ylabel('sliding surface'); xlabel('time(sec)*10^-3'); hold on; grid;
end
0 Comments
Answers (1)
Geoff Hayes
on 19 Apr 2015
Biswajit - the error message is telling you that you are trying to access an index that exceeds the dimension of your array t. You have defined t as
t=zeros(1,n);
where n is 2000, yet the code that uses this array is doing
for i=5001:15000
u(i)=t(i);
t(i+1)=t(i)+d;
end
So either you have to increase the size of t to 5000 elements, or change the code here so that you don't begin iterating at 5001.
Why has t been initialized to an array of all zeros with dimension 1x2000, and yet now the code is treating it as if it were an array of at least 5000?
0 Comments
See Also
Categories
Find more on Creating and Concatenating Matrices in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!