
> Use array operations from Practical 3 to solve the problem in Exercise 1. Square = num^2 % this line prints the square % Use a for- loop to print out the square of integers from 1 up to %maxValue.įor num = % ** increment num from 1 to maxValue ** % Enter your name, student ID and the date here > Fill in the correspondent commands doing what required within **.**. If you comment out a line containing for, while or if-else, don’t forget to comment out the corresponding end statements. Tip: If you are having trouble finding an error, try commenting out a few lines to determine the line that is causing i.
#FOR LOOP IN MATLAB CODE#
Run each cell separately and follow the instructions, making sure that you also add in the required code marked by starred lines. > Complete the exercises in the script M-file for this week. > What happens when x equals to zero or x is not zero? % This code assigns the value of x to zeroValue only % if x is zero If x What is wrong with the following code? Type it in MATLAB to check the error message. % This code assigns the value of x to negativeValue True if a is equal to 6 (note double equals sign) How many times will the starred line of the previous example be executed? What are starting point, increment (step) and ending point of this loop? S=0 % Initial assignment for sum to be able to Type in the following code and examine the output. While i Calculate the sum S of elements a i =√2i-1, i=1, 2. I=1 %we need to have a starting point to be able Now, let’s do the same by using while- loop Note: the command ‘ for i=1:1:10’ is equivalent to ‘ for i=1:10’.Note: the command ‘ for i=1:1:10’ does not create an array.The Syntax for for and while loops is as follows Else the hour of the day is not 6pm, print the statement “it is not dinner-time, it’s o’clock”. If the hour of the day is 6pm, print the statement “it is dinner-time, it’s o’clock”. In this case you do not know what will be your last number.Įxecutes a set of commands once, if a given condition is met, else- executes a set of commands, if the given condition is not met. In other words, while chronometer hand have not done a whole circle, keep counting. You are asked to count during one minute. CommandĮxecutes a set of commands repeatedly by incrementing a variable by a given step size until the set maximum is reached.įor each hour from 1pm to 12pm, print the statement “it is o’clock”.Įxecutes a set of commands if a condition after while is true. The following table shows the main types. MATLAB offers features to allow you to control the sequencing of commands by setting conditions. Use this word document as a template to prepare your responses for submission. Please submit your responses to the activities within this practical for formative feedback from your MATLAB eTutor.

You can also print this resource as a single document (using the print icon in 1.9 or the Admin section in 2.5/2.6).Use the arrows on the top right and bottom right of this display to move between pages, or select a page using the left hand navigation pane.An arrow > at the start of the line in an exercise indicates an activity for you to complete.MATLAB code will always be denoted by the Courier font.Ask your MATLAB eTutor if you have any questions or need advice using the Practical's Forum.


