Objectives
The objectives are to introduce the following concepts:
• vector element access
• compound conditional statements
• pie charts
Deliverables
Submit your pre-lab answers in Bb Learn under the Lab 6 pre-lab assignment area prior to the start of class
1. We can access certain elements of a vector by specifying the index of the value we want in parentheses after the vector's name. Suppose we have a vector x = [1 5 3 6 3]. We would type x(2) to get the second value in the x vector or x(4) to get the fourth value in the x vector. If we want to multiply the fifth value in the x vector by 3, we would write x(5) = x(5) * 3.
Write the MATLAB code to add 1 to the third element of the x vector.
2. We can check for more than one condition in an if statement or while loop with the use of logical operators. The logical AND (&&) operator connects two Boolean values. If both of the Boolean values are true, then the expression returns true. Otherwise, the expression returns false. The logical OR (||) operator also connects two Boolean values. If either of the Boolean values are true, then the expression returns true. Otherwise, the expression returns false. Note that compound conditional operators can be chained together to make complex expressions.
i) The following would execute the code inside the while loop as long as a is greater than 7 or b is greater than 9. The code inside of the while loop adjusts the values of a andb so that there is not an infinite loop.
a = 14;
b = 12;
whilea>7 || b>9
a = a - 2;
b = b - 1;
end
Suppose we have a variable calledc, which represents some integer. Write the MATLAB code for a while loop that executes as long as c is greater than 4 or c is less than 11. Just show the while loop line. You do not need to include any code inside of the while loop.
ii) The following code would execute the code inside the while loop as long as d equals the character ‘u' and e equals the character ‘v'. The code inside the while loop asks the user for input to update the values of d and e.
d = input(‘Enter a value for d', ‘s');
e = input(‘Enter a value for e', ‘s');
while d == ‘u' &&e == ‘v'
d = input(‘Enter a value for d', ‘s');
e = input(‘Enter a value for e', ‘s');
end
Suppose we have a variable called f, which represents a character. Write the MATLAB code for a while loop that executes as long as f does not equal ‘u' and f does not equal ‘v'. Recall that MATLAB uses ~= to check if two values are not equal. Just show the while loop line. You do not need to include any code inside of the while loop.
3.The following links provide information on the pie, legend, and title commands.
https://www.mathworks.com/help/matlab/ref/pie.html
https://www.mathworks.com/help/matlab/ref/legend.html
https://www.mathworks.com/help/matlab/ref/title.html
Write the MATLAB code to set up the vector y = [2 4 3 6 2] and plot y in a pie chart. Add a legend and title to the pie chart. The legend should contain the following strings: ‘value1', value2', ‘value3', ‘value4', and ‘value5'. The title should be ‘My Awesome Pie Chart'.