Index exceeds array bounds error

1 回表示 (過去 30 日間)
Josh
Josh 2019 年 3 月 16 日
編集済み: madhan ravi 2019 年 3 月 16 日
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 100x1 matrix. I am hoping for this to create S_FEM, an additional 100x1 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);

採用された回答

Star Strider
Star Strider 2019 年 3 月 16 日
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);

その他の回答 (1 件)

madhan ravi
madhan ravi 2019 年 3 月 16 日
編集済み: madhan ravi 2019 年 3 月 16 日
When the loop iterator becomes n which is 100 , what happens is dnew(i+1) tries to grab 101’st element from the variable dnew which doesn’t exist.
So anyway you don’t need a loop to acquire this task:
S_FEM = E*diff(dnew)/l;
S_FEM = [S_FEM;S_FEM(n)]

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

タグ

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by