Lab 12
User-defined Functions and
Arguments
CS211 Lab Policy:
- This lab exercise will not be graded.
- Submit as much as you have completed before the end of the lab period in
which it is assigned.
- If you do not finish this lab work, it is to your advantage to finish it
outside of class. Please re-submit your finished work to the course web
site.
- You may receive help from anyone in completing this lab.
- You may not submit another student's code as part of your
lab.
Instructions:
For this lab you will create two files, Lab12.m
and Dot_Product.m. For the
Lab12() function you will experiment with
using functions that already exist. For the
Dot_Product() function you will define a new,
user-defined function, and then modify your Lab12()
function to use this function.
Part 1:
Download and save to your CS211 MATLAB programs folder the example functions
in lesson 12, which are:
Wait_For_Enter,
New_Lines,
Current_Time,
DegF_To_DegC,
Point_Distance and
Min_Max_N.
(Right-click and use "Save Target As...". Make sure you save them with file
names that are identical to the function names)
- Create your Lab12a file and include a comment header block.
- Begin your program by clearing the command window and displaying your
name and "Lab 12".
- Display the current time by making a call to the
Current_Time() function and displaying
its return value (its output argument).
- Pause your program by calling the
Wait_For_Enter() function.
- Display 3 blank lines to the command window using the
New_Lines()
function.
- Now display the current time again by using a new call to the
Current_Time() function.
- Display 5 blank lines to the command window using the
New_Lines()
function.
- Using the DegF_To_DegC()
function, display the the degrees Celsius that is equivalent to the
following degrees Fahrenheit: -10, 32, 70, and 100.
- Pause your program by calling the
Wait_For_Enter() function.
- Create four variables in your program as follows:
A = [5, 2]; B = [7, 12]; C = [6, 14]; D = [2,
3];. Assuming that these variables represent the 4 corners of an
irregular polygon, calculate its perimeter length using 4 function calls to
the Point_Distance() function and
display your result.
- Prompt the user to enter an array. (The array can be a row vector,
column vector, or a 2D matrix). Then use the Min_Max_N() function to report
the minimum value in the array, the maximum value in the array, and the
number of elements in the array.
- Repeat step 11 again, but this time use all new variables for your input
and output arguments.
Part 2:
Create a new, user-defined function called
Dot_Product and save it in a file called
Dot_Product.m.
- Create your Dot_Product.m file
and include a comment header block. (This will be first time that you need
to change the comment lines for INPUTS: and OUTPUTS:.)
- Your Dot_Product function should
have two input arguments and one output argument. Write you function header
(function line) appropriately.
- Add code to calculate the dot product of the two input vectors. The dot
product of two vectors is calculated by multiplying corresponding elements
of each vector and adding the results. For example, the dot product of [1 2
3] and [4 5 6] is 1*4 + 2*5 + 3*6.
- Return your calculated answer.
- After your function is finished, make sure it is saved to your file,
and test it by calling it from the
command line with different input arguments. If you discover any errors, fix
them.
- Add code to your Lab12() function
that will call your Dot_Product()
function 3 times. For the first call, send it two vectors with only 2
elements each. For the second call, send it two vectors with 3 elements
each. For the third call, send it 2 vectors with 6 elements each. After each
function call display the answer that was returned.
This lab assignment hopefully helped you learned how to pass input arguments to a function
and capture the function's output arguments after its work is
completed. Please make sure you understand how argument passing works. Remember, the
names of the arguments does not matter. What matters is the number and
order of the arguments!
Turn-in:
Submit your
Lab12.m
and Dot_Product.m
files.