MATLAB: Index exceeds array bounds error

error

Hi there I have a problem with the code below. I am trying to run a for loop for n=100 times where E is a constant and dnew is a 100×1 matrix. I am hoping for this to create S_FEM, an additional 100×1 matrix. However when trying to run I get the error "Index exceeds array bounds". How can I fix this?
for i = 1:n
S_FEM(i) = (E*(dnew(i+1)-dnew(i)))/l;
end
S_FEM(n+1) = S_FEM(n);

Best Answer

Consider:
n=100
‘dnew is a 100x1 matrix’
and you are addressing:
dnew(i+1)
that does not exist.
Try this instead:
for i = 1:n-1
S_FEM(i) = (E*(dnew(i+1)-dnew(i)))/l;
end
S_FEM(n+1) = S_FEM(n);