Description
Important Notes:
• This assignment is about writing user defined functions. The questions are based on
content from chapters 6 and 7 of the textbook “Matlab, An introduction with
applications”.
• Complete this assignment by filling in the template file, assign5.m, which already has the
function templates done for you.
• Before you submit your script file, make sure there are no syntax errors.
• The functions will be tested by the auto-tester using randomly generated data.
• Do NOT use ‘clear’ OR ‘clear all’ anywhere in your code.
Q1. Write a function that accepts a general row vector X as an input and generates a row vector
P such that P contains all of the positive elements of X.
Q2. Write a function that determines y for a given input x. Assume that � is a scalar (as opposed
to being a vector or a matrix).
� � = −0.2�( + 7�+ �-..(/
Q3. Modify the function above such that the input � can be a 2×2 matrix.
Q4. Write a function that uses the switch – case statement (refer to pg.189-192 in the textbook).
The function takes a vector of strings in a structure (see assign5.m to see how this is done) and a
number � as input. Each string is the case condition which specifies the type of operation to be
performed on ‘x’. The two cases for mathematical operations are ‘invert’ and ‘root2’. The
function produces a numeric output for a given numeric value of � and the condition �. If � is
anything other than ‘invert’ or ‘root2’, the function sets the output equal to 0. Place the results
in a row vector corresponding to operations as listed in the structure � i.e. for the list of
operations ‘invert’,’root2’,’none’ and x=0.5 the answer will be [2.0000 0.7071 0].
ENEL101 Assignment 5 Page 2 of 3
Q5. Write a function that takes the coefficients �, �, � of a quadratic equation of the form ��+ +
�� + � = 0 as inputs and calculates the discriminant � = �+ − 4��. Then,
If � > 0 the program sets ������� = 2.
If � < 0 the program sets ������� = 0.
If � = 0 the program sets ������� = 1.
Q6. Fibonacci numbers are the numbers in a sequence in which the first two elements are 0 and
1, and the value of each subsequent element is the sum of the previous two elements as
0,1,1,2,3,5,8,13, …. Write a function that takes an integer � > 2 as input and stores the first �
Fibonacci numbers in a column vector. You do not need to check for the condition of � > 2.
Q7. Write a function that finds the fifth root of input P using Newton’s method, applying the
recursive formula
�EFG = �E − �E
H − �
5 �E
J
For the first value use �G = �. Continue with the recursive formula until the estimated relative
error � < 0.00001 where
� = �EFG − �E
�E
The function must output a 1×2 vector, with the first element being the answer (the fifth root)
and the second element being the number of iterations that were needed.
Q8. Given a point �. = 0.25 and function � � = �+�/ approximate the function’s derivative
at �. using the four-point difference formula
� � �
� � = � �. − 2ℎ − � �. − ℎ + � �. + ℎ − �(�. + 2ℎ)
12 ℎ
using ℎ = /Q
G. (ℎ must be a small number relative to �.). The function must output a 1×3 vector,
the first element being the numerator, the second the denominator, and the third the resulting
approximation