AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |
Back to Blog
Using a for loop in matlab12/28/2023 So i = 2 which will be the current value of the loop variable and then it’s squared. Now we are going to assign a value to x which is equal to x= i 2. So press the enter key which will start a new line that will be automatically indented. So our loop variable is I and it is assigned so we do an equal sign the following values we start with our initial value which is 2 then we do a colon our incremental or our step value which is also two then a colon and our final or terminating value which is equal to four. In the MATLAB editor we will create for loop which will start with the word for and then we will have a space and now we need to assign an initial value a step or incremental value and a final or terminating value to our loop variable. Let’s take a look at implementing this example in MATLAB first we will start a new script file as we have discussed in the previous tutorial how to create script file. Now we increment i by 2 i had a value of 4 we increment by 2 and the new value of i is equal to 6 and this begins our third and final loop pass and that is because our current value of i is equal to 6 which is greater than 4 that statement is true and our loop terminates with the endstatement. So the value of 16 is now assigned to x that completes our second loop pass. So x is equal to x= i 2 and as the value of I is now 4 so the square will become 16. Now our current value of i is 4 which is not greater than 4 the statement is false and now we assign a new value to x. Next we increment I by 2 so i now has a value of 4 and we begin our second loop pass. So that is the value assigned to x and that completes our first loop pass. Let’s take a look at a flow chart of our example which can help demonstrate each loop pass our loop variable i has an initial value of 2 and this initial value of I is not greater than our final value of 4 that statement is false and so we assign a value to x which is equal x= i 2 now as the value of I = 2 so the value of x will be equal to 4. In this case i is our loop variable i has an initial value of 2 an incremental or step value of 2 in a final or terminating value of 4 and for each value of I our statement is evaluated as x= i 2.
0 Comments
Read More
Leave a Reply. |