MATLAB: I am getting the following error “Index exceeds the number of array elements (70).” , please help me out!

errorMATLAB

D =[3.7688 2.6753 2.0275
2.6753 3.7688 2.0275
2.0275 2.0275 2.9674]
L = 100e-3 ; a=30e-3; Ex = 12/((1e-3)^3 * D(1,1));
I = (5e-3)*(1e-3)^3 / 12;
x2 = 31e-3:1e-3:100e-3;
w=zeros;
for r=31:1:100
w(r)=((100*a^2) /(24*L*Ex*I))*(2*(x2(r))^3-6*L*(x2(r))^2+a^2 *(x2(r))+4*L^2 *(x2(r))-a^2*L);
end
plot(x2,w)

Best Answer

Although the values within x2 range from 0.031 to 0.1, they are stored at indices ranging from 1 to 70:
>> x2(1)
ans =
0.031
>> x2(70)
ans =
0.1
>> x2(71)
Index exceeds the number of array elements (70).
In your for loop, have r range from 1 to 70 instead of 31 to 100:
D =[3.7688 2.6753 2.0275
2.6753 3.7688 2.0275
2.0275 2.0275 2.9674]
L = 100e-3 ; a=30e-3; Ex = 12/((1e-3)^3 * D(1,1));
I = (5e-3)*(1e-3)^3 / 12;
x2 = 31e-3:1e-3:100e-3;
w=zeros;
for r=1:numel(x2)
w(r)=((100*a^2) /(24*L*Ex*I))*(2*(x2(r))^3-6*L*(x2(r))^2+a^2 *(x2(r))+4*L^2 *(x2(r))-a^2*L);
end
plot(x2,w)