Index exceeds the number of array elements (1)
1 view (last 30 days)
Show older comments
I'm getting the error as stated in the title, however I cannot determine where it comes from. Can anyone help me locate it and try and help me fix it?
% SPECIFY BOUNDARY VALUES AND NECESSARY PARAMETERS
A=1;B=1;
V1 = 0; V2 = 10; V3 = 20; V4 = -10;
NX = 20; %4 12 20
NY = NX;
H = A/NX;
for I = 1:NX-1
V(I+1, 1) = V1;
V(I+1, NY+1) = V3;
end
for J = 1:NY-1
V(1, J+1) = V4;
V(NX+1, J+1) = V2;
end
V(1,1) = (V1 + V4)/2.0;
V(NX+1, 1) = (V1 + V2)/2.0;
V(1, NY+1) = (V3 + V4)/2.0;
V(NX+1, NY+1) = (V2 + V3)/2.0;
%optimum
T = cos(pi/NX) + cos(pi/NY);
W = (8- sqrt(64-16*T^2))/(T^2);
disp(['SOR Factor Omega = ', num2str(W)])
W4 = W/4;
%iteration begin
NCOUNT = 0;
loop = 1;
while loop == 1;
RMIN = 0;
for I = 1:NX-1
X = H*I;
for J = 1:NY-1
Y = H*J;
G = -36.0*pi*X*(Y-1.0);
R = W4*(V(I+1, J+1) + V(I, J+1) + V(I+1, J+2) + V(I+1, J) -4.0*V(I+1, J+1) - G*H*H);
RMIN = RMIN + abs(R);
V(I+1, J+1) = V(I+1, J+1) + R;
end
end
RMIN = RMIN(NX*NY);
if(RMIN >= 0.0001)
NCOUNT = NCOUNT +1;
if(NCOUNT >100)
loop = 0;
disp('Solution non convergent in 100 iterations')
end
else
loop = 0;
disp(['Solution convergent in ',num2str(NCOUNT),'iterations'])
disp(['h = ', num2str(H)])
end
end
Vnum = V;
%original pts through i
abc = zeros(1,9);
a_tic = 1;
vec = [0:h:1];
for ii= .25:.25:.75
for jj = .25:.25:.75
xind = find(vec==ii);
yinf = find(vec==kk);
%disp([xind, yind])
abc(a_tic) = Vnum(xind, yind);
a_tic = a_tic +1;
end
end
%Output approxiamte results
for I = 1:NX-1
X = H*I;
for J = 1:NY-1
Y = H*J
SUM = 0;
for M = 1:10 %only need first 10 terms in series
FM = M;
for N = 1:10
FN = N;
FACTOR1 = (FM*pi/A)^2 + (FN*pi/B)^2;
FACTOR2 = ((-1)^(M+N))*144*A*B/(pi*FM*FN);
FACTOR3 = 1 - (1- (-1)^N/B);
FACTOR = FACTOR2 * FACTOR3 / FACTOR1;
SUM = SUM + FACTOR*sin(FM*pi*X/A)*sin(FN*pi*Y/B);
end
end
VH = SUM;
V = V';
E = gradient(V);
E = -E;
[Ex,Ey] =gradient(V);
Ex = -Ex;
Ey = -Ey;
x_range = (1:Nx)-mpx;
y_range = (1:Ny)-mpy;
figure(1)
contour_range_V = -130:1:130;
contour(x_range,y_range,V,contour_range_V,'linewidth',3);
axis([min(x_range) max(x_range) min(y_range) max(y_range)]);
colorbar('location','eastoutside','fontsize',14);
xlabel('x ','fontsize',14);
ylabel('y ','fontsize',14);
title('Electric Potential distribution, V in volts','fontsize',14);
h1=gca;
set(h1,'fontsize',14);
fh1 = figure(1);
set(fh1, 'color', 'white')
xlim([0 0.1])
ylim([0 0.05])
figure(2)
contour(x_range,y_range,E,'linewidth',0.5);
hold on;
quiver(x_range,y_range,Ex,Ey,4);
axis([-10 10 -10 10]);
title('Electric field Lines, E (x,y) in V/m','fontsize',14);
colorbar('location','eastoutside','fontsize',14);
xlabel('x-axis in meters','fontsize',14);
ylabel('y-axis in meters','fontsize',14);
h2=gca;
set(h2,'fontsize',14);
fh2 = figure(2);
set(fh2, 'color', 'white')
xlim([0 0.1])
ylim([0 0.05])
0 Comments
Answers (2)
Star Strider
on 20 Feb 2019
Edited: Star Strider
on 20 Feb 2019
This line is throwing the error:
RMIN = RMIN(NX*NY);
Since ‘RMIN’ is a scalar, it seems that you might be missing a multiplication (or other) operator, for example:
RMIN = RMIN*(NX*NY);
or some such.
EDIT —
There are also some end calls missing from the code you posted.
6 Comments
Star Strider
on 20 Feb 2019
Indeed they must.
Note that:
x_range = (1:NX)-V;
creates ‘(1:NX)’ as a (1 x 20) vector, and ‘V’ is a (21 x 21) matrix. If they both had one dimension in common, you could probably use the bsxfun function to do the subtraction. As it is, they are completely incompatible.
James Tursa
on 20 Feb 2019
Edited: James Tursa
on 20 Feb 2019
Type the following at the command line:
dbstop if error
Then run your code. When the error occurs, the code will pause at that line with all variables intact. Examine them to see what the size issues are, then backtrack in your code to figure out why they are not the sizes you expected.
0 Comments
See Also
Categories
Find more on Matrices and Arrays 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!