**Lab 07: Logical Functions and Selection Structures**

**Objectives**

- To understand how MATLAB interprets relational and logical operators.
- Understand the appropriate uses of the if/else family of cimmands
- Understand the switch/case structure

**Background**

**Questions**

- Consider the following matrices:

$x = \left[ {\matrix{1 & {10} & {42} & 6 \cr 5 & 8 & {78} & {23} \cr {56} & {45} & 9 & {13} \cr {23} & {22} & 8 & 9 \cr} } \right]$ $y = \left[ {\matrix{ 1 & 2 & 3 \cr 4 & {10} & {12} \cr 7 & {21} & {27} \cr } } \right]$ $z = \left[ {\matrix{ {10} & {22} & 5 & {13} \cr } } \right]$- Using single-index notation, find the index numbers of the elements in each matrix that contain values greater than 10.
- Find the row and column numbers (sometimes called subscripts) of the elements in each matrix that contain values greater than 10.
- Find the values in each matrix that are greater than 10.
- Using single-index notation, find the index numbers of the elements in each matrix that contain values greater than 10 and less than 40.
- Find the row and column numbers for the elements in each matrix that contain values greater than 10 and less than 40.
- Find the values in each matrix that are greater than 10 and less than 40.
- Using single-index notation, find the index numbers of the elements in each matrix that contain values between 0 and 10 or between 70 and 80.
- Use the length command together with results from the find command to determine how many values in each matrix are between 0 and 10 or between 70 and 80.

- The if-else statement is particularly useful in functions. Write and test a function for each of these problems, assuming that the input to the function is a scalar:
- Suppose the legal drinking age is 21 in your state. Write and test a function to determine whether a person is old enough to drink.
- Many rides at amusement parks require riders to be a certain minimum height. Assume that the minimum height is 48" for a certain ride. Write and test a function to determine whether the rider is tall enough.
- When a part is manufactured, the dimensions are usually specified with a tolerance. Assume that a certain part needs to be 5.4 cm long, plus or minus 0.1 cm (5.4 ± 0.1 cm). Write a function to determine whether a part is within these specifications.
- Unfortunately, the United States currently uses both metric and English units. Suppose the part in Question 2.c was inspected by measuring the length in inches instead of centimeters. Write and test a function that determines whether the part is within specifications and that accepts input into the function in inches.
- Many solid-fuel rocket motors consist of three stages. Once the first stage burns out, it separates from the missile and the second stage lights. Then the second stage burns out and separates, and the third stage lights. Finally, once the third stage burns out, it also separates from the missile. Assume that the following data approximately represent the times during which each stage burns:

Stage 1 0–100 seconds

Stage 2 100–170 seconds

Stage 3 170-260 seconds

Write and test a function to determine whether the missile is in Stage 1 flight, Stage 2 flight, Stage 3 flight, or free flight (unpowered).

- Use the switch/case structure to solve these problems:
- Create a program that prompts the user to enter his or her year in school — freshman, sophomore, junior, or senior. The input will be a string. Use the switch/case structure to determine which day finals will be given for each group — Monday for freshmen, Tuesday for sophomores, Wednesday for juniors, and Thursday for seniors.
- Create a program to prompt the user to enter the number of candy bars he or she would like to buy. The input will be a number. Use the switch/case structure to determine the bill, where

1 bar $\$ 75$

2 bars $\$1.25$

3 bars $\$1.65$

more than 3 bars = $\$ 1.65 + \$ 0.30 * (number\,ordered - 3)$