Lab 12

User-defined Functions and Arguments

CS211 Lab Policy:

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)

  1. Create your Lab12a file and include a comment header block.
     
  2. Begin your program by clearing the command window and displaying your name and "Lab 12".
     
  3. Display the current time by making a call to the Current_Time() function and displaying its return value (its output argument).
     
  4. Pause your program by calling the Wait_For_Enter() function.
     
  5. Display 3 blank lines to the command window using the New_Lines() function.
     
  6. Now display the current time again by using a new call to the Current_Time() function.
     
  7. Display 5 blank lines to the command window using the New_Lines() function.
     
  8. Using the DegF_To_DegC() function, display the the degrees Celsius that is equivalent to the following degrees Fahrenheit: -10, 32, 70, and 100.
     
  9. Pause your program by calling the Wait_For_Enter() function.
     
  10. 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.
     
  11. 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.
     
  12. 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.

  1. 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:.)
     
  2. Your Dot_Product function should have two input arguments and one output argument. Write you function header (function line) appropriately.
     
  3. 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.
     
  4. Return your calculated answer.
     
  5. 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.
     
  6. 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.