0% found this document useful (0 votes)
39 views3 pages

Lab4 01

This document outlines Lab #4 for EECS 1560, focusing on input/output statements, user-defined functions, and selection statements in MATLAB. It includes detailed instructions for completing lab exercises, which involve creating scripts to calculate unit vectors, reverse vectors, and temperature conversions. Students are required to submit their work as a single M-file on eClass.

Uploaded by

kisla05
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
39 views3 pages

Lab4 01

This document outlines Lab #4 for EECS 1560, focusing on input/output statements, user-defined functions, and selection statements in MATLAB. It includes detailed instructions for completing lab exercises, which involve creating scripts to calculate unit vectors, reverse vectors, and temperature conversions. Students are required to submit their work as a single M-file on eClass.

Uploaded by

kisla05
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

EECS 1560 Introduction to Computing for Mathematics and Statistics

LAB #4 (Section 01): Input /Output Statements, Functions and Selection


Statements

(Due: as posted on eClass)


Objectives:

 This lab explores the use of input/output statements, user-defined functions and selection
statements in MATLAB.

Submit your script in eClass.


https://eclass.yorku.ca/

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. open MATLAB 2019a on your desktop,


2. open a new “M-file” by selecting “HOME” in the toolbar, then click on “New Script”. Save the file
under your current directory. NOTE: the filename must be a single_word
3. select “PUBLISH” in the toolbar
4. click “Section with Title”
5. replace “SECTION TITLE” with your name and student number
6. replace “DESCRIPTIVE TEXT” with a brief description of this lab
7. go to the next line, click “Section”, enter “Question 1” after “%%”
8. go to a new line, now you can proceed to lab question 1
9. repeat step 7 and enter the new question number whenever you finish a lab question
10. include functions within a m.file, at the end of the script file.

Please complete all the exercises in ONE M-file.

Please Submit only ONE M-file.

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.

Enter the x-coordinate: 8

E nter the y-coordinate: 9

(red text indicates what the user will enter)


You should use one of the output statements discussed in the lecture to output the unit vector, each value
should only have 2 decimal places. The command window will display exactly the following:

The unit vector of <8.00,9.00> is: 0.66 0.75

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,avg] = vecreverse(x)

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

QUESTION 3.a) [20]


Write a script that will print a list consisting of “C for temperature in degrees Celsius”, “F for temperature
in degrees Fahrenheit”, and “K for temperature in degrees Kelvin”. It prompts the user to choose one, and
then prompts the user for a temperature. The script should then make the conversions and print the
temperature in two others scales that has not been specified by the user. For example if the user choose C,
the scripts should return temperature in degrees Fahrenheit and degrees Kelvin.
If the user enters an invalid choice, the script simply prints an error message. The script should use a nested
if-else statement to accomplish this. For example, the followings illustrate 3 different scenarios:
(red numbers or character indicate values or character entered by the user).

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

You might also like