We are going to write a script file to evaluate the function (y) where y is equal to:įor x starting at a value of 0 and ending at a value of 8 in increments of 2. First I will introduce you to how arrays are handled in MATLAB then we will take a look at an example. We are going to use a for loop to store data inside of an array using an array index. Indexing and storage in arrays using for loop: In addition in the workspace on the right hand side you can see the final value for each of our variables so i had a final value of 4 before the loop terminated and x had a final value of 16 this completes our example. Now what you will see here is that we have two values for x one value is x was equal to 4 on the first pass of the loop and x was equal to16 on the second pass of the loop.Now on the third pass our loop variable had a value of 16 which was greater than our final or terminating value of 4 and that is why you do not see a third value assigned to x. so I am going to go to the top of the screen and click run.Now the program has run and you can see the output of the program in the command window at the bottom. now I am going to hit enter and then our for loop ends with the word end which again is automatically indented.Now we are ready to run our program. Now normally you would end this statement with a semicolon to suppress the output in this case I am going to remove that semicolon so the value of x is output to the command window. 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.