Lab4 01
Lab4 01
This lab explores the use of input/output statements, user-defined functions and selection
statements in MATLAB.
You may submit files as many times as you like. The most recent submission overwrites all previous
submissions.
____________________________________________________________________________________
Please follow the steps below before you proceed to the lab exercises:
1|Page
EECS 1560 Introduction to Computing for Mathematics and Statistics
Lab questions:
QUESTION 1: [20] A unit vector is a vector that has a certain direction, but a magnitude of 1. The
equation for a unit vector in two-dimensional space is:
x, y
u
x2 y 2
Write a script that prompts the user for x and y values (for example, in this case you can use x = 8, y = 9),
and then calculate the unit vector. MATLAB should display the following when you run the script.
QUESTION 2: [30]
Create a local function at the end of the script named “vecreverse” that will receive a 1 × n row
vector and will return: (1) a vector with only even numbers in the original vector in reverse order, and (2)
the average of these numbers. For example:
>> x = [3 2 6 7 8 11 2 1 14];
new_vec =
14 2 8 6 2
avg =
6.4000
In your script, test out your function with input x which is an array of 10 random integers in the range
from 1 to 20.
(Note: “mean()” Built-in function and conditional statements such as if-else, for-loop, while loop
statements are not allowed)
2|Page
EECS 1560 Introduction to Computing for Mathematics and Statistics
Scenario #1
C for temperature in degrees Celsius
F for temperature in degrees Fahrenheit
K for temperature in degrees Kelvin
Please choose one: a
Error! Not a valid choice.
Scenario #2
C for temperature in degrees Celsius
F for temperature in degrees Fahrenheit
K for temperature in degrees Kelvin
Please choose one: C
Enter the temperature in degrees Celsius: -14
The temperature in degrees Fahrenheit is: 6.80
and the temperature in degrees Kelvin is: 259.15
Scenario #3
C for temperature in degrees Celsius
F for temperature in degrees Fahrenheit
K for temperature in degrees Kelvin
Please choose one: K
Enter the temperature in degrees Kelvin: 300
The temperature in degrees Celsius is: 26.85
and the temperature in degrees Fahrenheit is: 80.33
The format of the output should be exactly as specified above. The conversions are:
9
F C 32
5
K C 273.15
QUESTION 3.b) [15] Modify your script in question 3.a to accept both lower case and upper case letters.
This means that for example both F and f should be accepted as the scale for degrees Fahrenheit.
QUESTION 3.c) [15] Modify your script in question 3.a to use the switch statement instead of else-if.
3|Page