ENEL101 Problem set 5 M File Programming

$30.00

Download Details:

  • Name: Assignment-5-rqrswq.zip
  • Type: zip
  • Size: 89.27 KB

Category:

Description

5/5 - (1 vote)

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.