COMS W3101-6 (CVN)
Homework 5
Worth 15% of the total grade.
For this assignment you will write a script m-file called hw5.m. There are multiple exercises in this assignment, be sure to label your solution to each exercise with a comment. Some of the exercises ask for additional script m-files that will be called from within hw5.m. Please include your name and UNI in a comment at the top of all of your script m-files.
Exercises:
If there are no input arguments given to the function, have the program throw an error. Be sure to properly comment your function.
In 'hw5.m' execute findstats on various inputs that demonstrates your function (including no input, to demonstrate your error handling).
The present value of an annuity (a yearly sum of money) may be computed from the formula:
P = (A/i) [(1 + i)n - 1]/(1 + i)n
where A is the annuity (in $/year), i is the nominal yearly interest rate (in decimal form), n is the number of years over which the annuity is paid and P is the present value ($).
Example computation: If i = 0.15 (15%), A = $100/year and n = 10 years then P = $501.88.
a. Set a function handle "present_value" equal to an anonymous function that solves for P in terms of A, n and i. Verify that it works for the above example.
b. Create an inline function called "present_value_inline" that performs the same operation as in part a. Verify that it works for the above example.
c. If you won the $1,000,000 State Lottery and the Lottery offered you the choice of $500,000 today or $50,000/year for 20 years, which would you take? You can assume an inflation (interest) rate of 5%. Solve this problem using the function referenced by "present_value." Explain which you would pick in the comments.
The program's first argument is an arbitrarily sized cell array. Each cell should contain a function handle pointing to a function that takes only a single argument (such as sin, cos, etc). The second argument should be a variable with which to evaluate all of the functions. The program should return the same number of output arguments as their are function handles in your cell array. Each output argument should be the result of evaluating the corresponding input function handle at the specified value. For example you might test your program with code like this:
f_handles = { @cos @sin @tan };
[s c t] = evaluate_handles(f_handles,pi);
disp(s)
1.2246e-016
disp(c)
-1
disp(t)
1.2246e-016
Your function should use the try/catch structure to catch any errors that occur when evaluating each function handle in your array. Instead of terminating, your program should catch the error, and display a string like this:
Could not evaluate function "sin"
To To display a string like this you will need to extract the name of the function that failed. Use the "functions" function. It returns a structure with several fields. The field called 'function' contains the name of the function. Use the "sprintf" function to build the error message. Then pass that string to the "disp" function.
The output value for functions that fail should be NaN. Once you've displayed the error message and set the output value, your function should continue evaluating the other functions in your input cell array. For example, your function might fail if you pass it a function handle to a function that takes more than one argument:
myhandle = @(x,y) x+y
f_handles = {myhandle @cos}
[a b] = evaluate_handles(f_handles,pi);
Unable to evaluate function @(x,y) x+y
disp(a)
NaN
disp(b)
-1
From within 'hw5.m' demonstrate the evaluate_handles function on various lists of function handles. These should include both builtin functions and simple anonymous functions (such as 2*x, or x.^2). Also be sure to demonstrate the error handling capability of the function.
a. Write a script m-file called "prob4a.m" that plots the functions x, x3, ex and ex2 (all in a single plot) over the interval 0 < x < 4. Be sure to use sufficiently sampling rate of x to get a smooth good plot.
b. Write a script m-file called "prob4b.m" that does the following:
Create a function handle f that points to an anonymous function such that f(x) = sin(1/x).
Plot f over the interval 0.01 < x < .1. Again be sure to sample the interval densely to get a smooth plot.
c. Write a script m-file called "prob4c.m" that does the following: Create a function f (using anonymous functions) such that
f(x,y) = x + yCreate a 3-D mesh of f(x,y) over the interval 1 <= x <= 5 and 1 <= y <= 5.
From within 'hw5.m' call all three script m-files in sequence. Seperate them with calls to the function pause (which waits for the user to press a key before continuing with the script) so that the plots can be examined before they are replaced by the next one.
Create an array x that contains 100 evenly spaced values between 0 and 2*pi. Create an array y that contains the sine of x. Generate a plot of the sine using x and y.
Next create a 4th order polynomial approximation of the sine function using the x and y arrays as data points for the polynomial fitting function.
Finally plot the values of the polynomial approximation (over x) of sine on the same set of axes as the actual sine of x.
From 'hw5.m' call the prob5 script and then issue a pause command.
Create a function handle that points to an anonymous function that computes the following:
f(x) = x5 - x2Display a plot of the function over the range 0 <= x <= 1.
Compute the local minimum of the function in the region .4 <= x <= 1.
On top of the previous plot, plot the value of the function evaluated at the minimum. You are plotting a single value here, so you should have the plot function display a 'o' at the data point (see the '-o' option to plot in the help).
From 'hw5.m' call the prob6 script and then issue a pause command.
Create an array x that contains 100 evenly spaced values between 0 and 2*pi. Create an array y that contains the cosine of x. Create a 4th order polynomial approximation of the cosine function using the x and y arrays as data points for the polynomial fitting function.
Numerically integrate the cosine function (use the quad function) between the values of .2 and .5.
Directly integrate the polynomial approximation of the cosine (using 0 as the constant of integration). Evaluate the resulting polynomial at .5 and .2. Take the difference and compare this result to the result of the numerical integration.
From 'hw5.m' call the prob7 script and then issue a pause command.
What you need to submit:
You need to submit these files:
(There is no need for a diary on this assignment.)
Send a single email to:
with all the files as attachments. The subject of the email should be:
MATLAB Homework 5
In the email body include your name and UNI.