Can someone please help me with a loop issue.

I am trying to write a simple strain loop such that the first strain value E_1=0, and subsequent values follow with an increment of 0.02 that is delta strain between each step is 0.02, the loop I wrote is as follows:
for i=2:10
E_1=0;
E_(i)=E_(i-1)+0.02
end
but this does not work for some reason it gives an error that E_ is an undefined function. Can anyone please help me with this very simple problem.

2 commentaires

Stephen23
Stephen23 le 21 Mar 2018
Modifié(e) : Stephen23 le 21 Mar 2018
E_1=0;
defines a variable named exactly E_1. Then you try to access the first element of a variable named E_ which clearly does not exist at that point. Note that E_1 is not how indexing works: it does not treat the 1 as an index, it is part of the name!
Asim Gillani
Asim Gillani le 21 Mar 2018
Thank you for your reply, I have understood the issue I wrote for i=2:11 E(1)=0; E(i)=E(i-1)+0.02; end and it worked :)

Connectez-vous pour commenter.

Réponses (1)

David Fletcher
David Fletcher le 21 Mar 2018
Modifié(e) : David Fletcher le 21 Mar 2018
E_=0:0.02:0.2
Values are indexed as E_(1), E_(2) and so on

4 commentaires

Asim Gillani
Asim Gillani le 21 Mar 2018
Thank you for your reply, I thought of writing this in this form as well, but I want the values to be displayed as E_1=0, E_2=0.02, E_3=0.04... In the way you wrote the matrix E_ is formed with all values in 1st column
It's up to you, but I really wouldn't advise you to separate your variables like that - it's makes processing your data much, much more difficult. The code you've posted won't work in that way anyway, and I'm not in the habit of propagating bad habits, so I'll leave it up to you to amend it yourself if you really want to make them all separate variables.
James Tursa
James Tursa le 21 Mar 2018
"... but I want the values to be displayed as E_1=0, E_2=0.02, E_3=0.04... "
Please don't name variables this way! There's lots of posts on this site telling you why, but just don't do it! Keep the numbers as a vector and use indexing, or if you really need separate variables for some reason (that you haven't told us), try a cell array E_{1}, E_{2}, etc.
Asim Gillani
Asim Gillani le 21 Mar 2018
Thank you, will avoid it from now on, forgive me I am new to this.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Loops and Conditional Statements dans Centre d'aide et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by