Starting from:

$35

ENEL101 - Problem set 5 - M File Programming Solved


 

 

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 2x2 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          1               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  

𝑥H

                                                              𝑥EFG = 𝑥E −                E5            −𝑥EJ𝑃

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 1x2 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 ℎ = G./Q    (ℎ        must be a small number relative to 𝑥.).                       The function must output a 1x3 vector, the first element being the numerator, the second the denominator, and the third the resulting approximation.  

 


               

More products