MATLAB: Index out of bounds because size(Z_wire)

indexing

Hi I'm trying to execute a program but i keep getting this error:
>> inductanceTest (1,2,4,4,1)
Attempted to access Z_wire(2,2,4); index out of bounds because size(Z_wire)=[1,0,1].
Error in inductanceTest (line 52)
L0 = L0 + Z_wire (z(1,i),w,h);
I do not understand what does it mean. Is someone ready to help me?
here is the code
function [ inductance ] = inductance( N,w,h,d_in,s)
u = pi*4e-7;
r_in = d_in/2;
p = w+s;
k= w+h;
%THE LENGHTH AND THE DISTANCE OF THE TRACKS FROM CENTER:
z(1:2,1) = [r_in r_in];
z(1:2,2) = [d_in -r_in];
z(1:2,3) = [d_in -r_in];
z(1:2,4) = [r_in+p r_in];
if N==1
z(1:2,5) = [r_in r_in+p];
else
z(1:2,5) = [d_in+p r_in+p];
end
for i = 6:1:4*N+1
z(1:2,i) = [z(1,i-4)+2*p z(2,i-4)+ sign z(2,i-4)] ;
if i==4*N+1
z(1:2,i) = [z(2,i-1) z(2,i-4)+ sign z(2,i-4)] ;
end
end
Z_wire = (log(2)*z(1,i)/0.22/k)- 1.25 + ((k/3*z(1,i))+ (u/4));
L0 = 0;
for i = 1:1:4*N+1
L0 = L0 + Z_wire (z(1,i),w,h);
end

Best Answer

You have
for i = 6:1:4*N+1
N is 1 in your test, so that is
for i = 6:1:4*1+1
which is
for i = 6:5
in which the end point is less than the start point. In this case, the body of the loop is not executed, and i is set to []
Then the next line of code after the loop is
Z_wire = (log(2)*z(1,i)/0.22/k)- 1.25 + ((k/3*z(1,i))+ (u/4));
but i is empty, so z(1,i) is empty. When you combine an empty expression with other expressions using * or + the result is empty, so Z_wire ends up being empty.
Then you enter the for i = 1:1:4*N+1 loop, and have problems accessing Z_wire elements because Z_wire is empty.