MATLAB: Hi, While working with the code, I am prompted by “Attempted to access w(41); index out of bounds because numel(w)=1.” Being new to this message I am unable to understand what exactly is wrong in this code

numel(x)=1.temperature field

function[Td,T,Tc,p,x,y,Pt,e,err,Tcav]=chptmpl(v)
%v(m/sec) is cutting speed
%con is thermal conductivity of workpiece
%cont is thermal conductivity of tool

%roc is specific heat per volume
%fi is shear angle
%Ts is shear plane temperature
%vc is chip velocity
%Pf is friction power
%alp is thermal diffusivity of workpiece
%Tr is room temperature
Ks=2250;
h1=0.2;
con=70;
roc=3.7;
KK=400;
fi=28*pi/180;
beta=20*pi/180;
h2=h1/tan(fi);
b=10;
KKK=fix(KK*1.5);
Ts=Ks*cos(fi+beta)/(roc*cos(fi)*cos(beta))+20;
vc=v*tan(fi);
Pf=Ks*b*h1*tan(beta)*vc*1000;
alp=con/roc;
Tr=20;
Lc=4*h1;
dx=Lc/KK;
dy=h2/20;
dt=dx/(1000*vc);
dz=0.5;
%initialize the shear plane temperature
for j=1:20
T(j,1)=Ts;
y(j)=j*dy;
end
%calculate thermal resistance r of the tool
%cont is thermal conductivity of tool
w(1)=Lc+dz/2;R(1)=dz/(2*w(1));
for l=1:1:200
w(1)=Lc+dz/2;
R(41)=dz/(2*w(41));
r=0;
end
for l=1:41
r=r+R(l)/cont;
end
l=1;Tcav(l)=Ts;Pt(l)=0;e(l)=0;err(l)=1.0;
%start the outer loop indexed by i; it provides correction
%for heat Pt escaping through the tool; it is the "while" loop;
%err is defined in the last part of this loop, Pf is friction power,
%pc is power entering the chip, Pt is power passing through tool
while abs(err(1i))>0.02
Pc=Pf-Pt(1i);
%with input Pc calculate pmax
for k=1:(KKK-1)
pmax=Pc*16/(9*KK);
dp=pmax*8/(KK*7);
if k<=(KK/8)
p(k)=pmax;
elseif k>KK/8 && k<=KK
p(k)=pmax-dp*(k-KK/8);
else
p(k)=0;
end
%start the computation of thermal field in chip
T(1,k+1)=(p(k)/(b*dx*dy*roc)-(T(1,k)-T(2,k))*alp*dt/dy^2+T(1,k));
for j=2:19
T(j,k+1)=(T(j-1,k+1)+T(j+1,k)-2*alp*dt/dy^2+T(20,k));
end
end
%this ends the computationof field in the chip
%calculate the temperatures Tc in the chip tool contact
%and later the avertage temp Tcav
sum=0;
for k=1:KK
x(k)=(k-1)*dx;
Tc(k)=T(1,k);
sum=sum+Tc(k);
end
%reducing the size of T matrix to Td(m,n) matrix
for m=1:10
for n=1:50
Td(m,n)=round(T(2*m-1,12*n-11));
end
end
yi=yi+1;
Tcav(yi)=sum/KK;
Pt(yi)=(Tcav(yi)-Tr)*b/r;
%e is the fraction of the friction power passing into the tool
e(yi)=Pt(yi)/Pf;
err(yi)=(Tcav(yi)-Tcav(yi-1))/Tcav(yi);
if yi>8,break,end
end

Best Answer

You have
w(1)=Lc+dz/2;R(1)=dz/(2*w(1));
That creates w as a scalar value (and then immediately uses that scalar value.) The only valid subscripts for w at that point are w(1) or w(1,1) or w with any positive number of 1's in the ()
Then you have
for l=1:1:200
w(1)=Lc+dz/2;
R(41)=dz/(2*w(41));
r=0;
end
The assignment to w(1) inside that loop overwrites all of the w(1) that was just created, leaving w as being a scalar. Then on the next line you try to use w(41), but w only has one entry.
Be careful: you are using l (lower-case L) as your for loop variable, and that is easily confused with 1 (the digit). It is recommended that you avoid using l (lower-case L) as a variable due to the potential for confusion -- use L instead, as in
for L=1:1:200
w(1)=Lc+dz/2;
R(41)=dz/(2*w(41));
r=0;
end
this would make it more obvious that you are always overwriting the same location, suggesting that perhaps you want
for L=1:1:200
w(L)=Lc+dz/2;
R(41)=dz/(2*w(41));
r=0;
end
This would still be a problem, however, because w(41) is not going to be assigned to until L reaches 41. On the other hand there is no point in assigning to R(41) every time through the loop, and no point in overwriting all of r every time through the loop, so perhaps you want
for L = 1 : 200
w(L) = Lc + dz/2;
end
R(41) = dz/(2*w(41));
r = 0;
This could then be made more efficient as
w(1:200) = Lc + dz/2;
R(41) = dz/(2*w(41));
r = 0;
with no loop.