Index exceeds the number of array elements. Index must not exceed 0.

5 次查看(过去 30 天)
clc
close all
R=0.05; % radius of the bearing
X=2*pi*R; % circumference of the bearing
Z=0.1; % length of the bearing
M=200;
N=64;
delx=1/M;
delz=1/N;
xbar=0:delx:1;
zbar=0:delz:1;
theta=xbar*2*pi;
C=0.0001; %clearence
E=0.3; %eccentricity ratio
Ha=0.00009;
nx=6;
nz=6;
const=(R^2)/(Z^2);
fun_hbar = @(theta,z)(1+E*cosd(theta))-((Ha/C)*sind(pi*((theta*nx/2*pi)-1)).^2*sind(pi*(z*nz-1)).^2);
deltheta=theta(end)-theta(end-1);
h_bar=fun_hbar;
for theta=1:M
for z=1:N
H(theta,z)=(1+E*cosd(theta))-((Ha/C)*sind(pi*((theta*nx/2*pi)-1)).^2*sind(pi*(z*nz-1)).^2);
end
end
fun_hhalf = @(h)(-((h(2)-h(1))/2) + h(2));
fun_hhalf2 = @(h)(((h(2)-h(1))/2) + h(1));
h_halfminus=zeros(size(xbar,2),1);
for i=2: length(h_bar)-1
h_halfminus(i)=fun_hhalf([h_bar(i-1) h_bar(i)]);
end
h_halfplus=zeros(size(xbar,2),1);
for i=2: length(h_bar)-1
h_halfplus(i)=fun_hhalf2([h_bar(i) h_bar(i+1)]);
end
h_halfmius=zeros(size(zbar,2),1);
for j=2: length(h_bar)-1
h_halfminus(j)=fun_hhalf([h_bar(j-1) h_bar(j)]);
end
h_halfplus=zeros(size(zbar,2),1);
for j=2: length(h_bar)-1
h_halfplus(j)=fun_hhalf2([h_bar(j) h_bar(j+1)]);
end
hm1=h_halfminus(i).^3;
hp1=h_halfplus(i).^3;
hm2=h_halfminus(j).^3;
hp2=h_halfplus(j).^3;
const1=((hp1+hm1)*delz^2+const*deltheta^2*(hp2+hm2));
ITER=1000;
p_bar=zeros(length(xbar),length(zbar));
A1_fun=@(hm1,hp1,hm2,hp2)(hp1*delz^2/const1);
A2_fun=@(hm1,hp1,hm2,hp2)(hm1*delz^2/const1);
A3_fun=@(hm1,hp1,hm2,hp2)(hp2*deltheta^2*const/const1);
A4_fun=@(hm1,hp1,hm2,hp2)(hm2*const*deltheta^2/const1);
A5_fun=@(hm1,hp1,hm2,hp2)(E*sin(theta)*deltheta^2*delz^2/const1);
p_init=p_bar;
error_target = 0.0000001;
h = waitbar(0,'Please wait...');
for k=1:ITER
for j=2:size(p_bar,2)-1
for i=2:size(p_bar,2)-1
p_bar(i,j)=A1_fun(hp1(i),hm1(i),hp2(j),hm2(j))*p_bar(i+1,j)+.....
A2_fun(hp1(i),hm1(i),hp2(j),hm2(j))*p_bar(i-1,j)+.....
A3_fun(hp1(i),hm1(i),hp2(j),hm2(j))*p_bar(i,j+1)+.....
A4_fun(hp1(i),hm1(i),hp2(j),hm2(j))*p_bar(i,j+1)-.....
A5_fun(theta(i),hp1(i),hm1(i),hp2(j),hm2(j));
end
end
error = (sum(p_bar(:)) - sum(p_init(:)))/sum(p_bar(:));
p_init = p_bar;
if error < error_target
break;
end
waitbar(k / ITER)
end
close(h);
in this at line where p_bar(i,j) starts it gives the error Index exceeds the number of array elements. Index must not exceed 0.
  4 个评论
Walter Roberson
Walter Roberson 2022-4-19
The error is indexing an empty vector according to the error message, but theta would not be empty as a result of the loop. But hp1 would be empty for the reasons that I described in my answer.
Torsten
Torsten 2022-4-19
Ok, then theta(i) would be the next error after repairing hm1,hm2,hp1,hp2.
And unfortunately, many errors follow according to my tests.

请先登录,再进行评论。

回答(1 个)

Walter Roberson
Walter Roberson 2022-4-19
fun_hbar = @(theta,z)(1+E*cosd(theta))-((Ha/C)*sind(pi*((theta*nx/2*pi)-1)).^2*sind(pi*(z*nz-1)).^2);
deltheta=theta(end)-theta(end-1);
h_bar=fun_hbar;
fun_hbar is an anonymous function. h_bar is assigned a copy of the pointer to the anonymous function.
for i=2: length(h_bar)-1
The length() of an anonymous function is 1, so you are asking for i=2:1-1 which is i=2:0 .
In MATLAB, when the increment is positive and the upper bound is less than the lower bound, the loop control variable is assigned [] (the empty vector); the same is true for negative increment when the upper bound is greater than the lower bound.
hm1=h_halfminus(i).^3;
i is the last value it was assigned by the for loop; as indicated above, that means that i will be [] because of the bounds issue. So you are indexing some variables at the empty vector, i and getting out empty vectors.

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by