You are on page 1of 8

CPS118 > Lab 05 > Plotting in MATLAB 1

Lab 07 > Function Files in MATLAB [Fall 2023]

I. Overview and Objectives

Early in the course, we discussed that programming solutions for complex


problems typically is easier when these types of problems are broken up into
smaller, more manageable sub-problems.

Functions are special types of scripts that can provide an effective tool in this
approach by allowing reuse of the same set of instructions in the same (larger)
program, or across different programs.

In addition to hundreds of built-in MATLAB functions that you can utilize, you
can also easily define your own user-defined function.

The main goal of this lab is to allow you the opportunity to learn how to define
and use these types of functions.

The learning objectives of this lab can be summarised as follows:

1. Learning how to create function files.


2. Learning how to use anonymous or inline functions.
3. Learning how to use function functions and nested functions.

Reading and related topics: Lecture slides (Lesson #8) and chapter 7 of Amos
Gilat’s book.

II. Lab Tasks

Use Matlab to solve the following four problems. Save the code you wrote to solve
CPS118 > Lab 05 > Plotting in MATLAB 2

them, together with the result of it in a report. Make sure you include enough
comments in your code. Save your report in PDF or MLX format.

Problem 1:
Write a user-defined MATLAB function for the following math function:

The input to the function is x and the output is y. Write the function such that x
can be a vector (use element-by-element operations).

a) Use the function to calculate y(–2) and y(4).


b) Use the function to make a plot of the function y(x) for −4 ≤ x ≤ 8.

b.)
CPS118 > Lab 05 > Plotting in MATLAB 3

Problem 2:
The relative humidity, RH, at sea level can be calculated from measured values of
the dry-bulb temperature Tdb, , and the wet-bulb temperature Twb by
(temperatures in degrees Celsius):
𝑉𝑃
𝑅𝐻 = 𝑆𝑉𝑃
× 100
Where VP is the vapor pressure given by:

and SVP is the saturated vapor pressure given by:

Write a user-defined function for calculating RH for given Tdb and Twb. For the
function name and arguments, use RH = RelHum(Tdb,Twb). The input
arguments are Tdb and Twb are the dry-bulb and wet-bulb temperatures,
respectively in ◦F. The function's result RH is the relative humidity in percent
(rounded to the nearest integer). Inside the user-defined function use an
anonymous function to convert the unit of the temperature from Celsius to
Fahrenheit. Use the function to determine the relative humidity for the following
conditions:
CPS118 > Lab 05 > Plotting in MATLAB 4

a) Tdb = 75◦F, Twb = 69◦F.


b) Tdb = 93◦F, Twb = 90◦F.

Problem 3:
Write a user-defined MATLAB function that converts positive integers written in
decimal form to binary form. Do not use the MATLAB built-in function
dec2bin. Name the function b=Bina(d), where the input argument d is the
positive integer to be converted and the function's result b is a vector with 1’s and
CPS118 > Lab 05 > Plotting in MATLAB 5

0’s that represents the number in binary form. If the function’s input is a negative
number, the function should display an appropriate error message. The largest
number that could be converted with the function should be a binary number
with 16 1s. If a larger number is entered as d, the function should also display an
error message.

Write a script that prompts a user for decimal input to convert to binary using
your function. Test your script with the following test data:
a) = −5
b) = 1002
c) = 200090
CPS118 > Lab 05 > Plotting in MATLAB 6

Problem 4:
In the lottery the player has to guess correctly r numbers that are drawn out of n
numbers. The probability, P, of guessing m numbers out of the r numbers can be
calculated by the expression:

𝑥!
where 𝐶𝑥𝑦 = 𝑦! (𝑥−𝑦)!

Write a user-defined MATLAB function that calculates P. For the function name
and arguments, use P = ProbLottery(m, r, n). The input arguments are m, the
number of correct guesses; r, the number of numbers that need to be guessed; and
n, the number of numbers available.

Use a sub function inside ProbLottery for calculating Cx,y. You can use the
MATLAB built-in function factorial for this task.

(a) Use ProbLottery for calculating the probability of correctly selecting 3 of 6


numbers that are drawn out of 49 numbers in a lottery game.

(b) Consider a lottery game in which 6 numbers are drawn out of 49 numbers.
Write a program in a script file that displays a table with seven rows and two
columns. The first column has the numbers 0, 1, 2, 3, 4, 5, and 6, which are the
CPS118 > Lab 05 > Plotting in MATLAB 7

number of numbers guessed correctly. The second column shows the


corresponding probability of making the guess.

III. Submission Guidelines


CPS118 > Lab 05 > Plotting in MATLAB 8

This lab is to be done individually. You need to submit a lab report with the
solutions to the five problems, and the report submitted should represent your
work. Your lab report must be in PDF or MLX format (create one section for
each problem). Submit your lab report on D2L/Brightspace before November
22, 2023 at 11:59 PM. Late submissions will not be accepted.

Have fun!

You might also like