89.1k views
4 votes
EXERCISE 5 Write a function file that creates the following piecewise function: f(x) =   ex−11 , x ≤ 5 −2x−1 , 5 < x ≤ 10 x x−11 , 10 < x 6= 11 Assume x is a scalar. The function file should contain an if statement to distinguish between the different cases. Also, if the input is x = 11, the function should display "the function is undefined at x = 11". Test your function by evaluating f(4), f(5), f(5.5), f(10), f(11) and f(12)

User Jkap
by
5.2k points

1 Answer

7 votes

Answer:

Complete Matlab code with explanation and output results is given below

Step-by-step explanation:

The given piece-wise function is

f(x) = eˣ⁻¹¹ for x ≤ 5

-2x - 1 for 5 < x ≤ 10

x/(x - 11) for x > 10 and x ≠ 11

A function f(x) is created using if else statements to select piece-wise functions with corresponding ranges. Also the last function is undefined at x = 11 therefore, it shows error whenever we enter x = 11.

Matlab Code:

function y=f(x)

if x==11

fprintf("sorry! f(x) is not defined at x=11")

else if x <= 5

y = exp(x-11);

else if x > 5 & x <= 10

y = (-2.*x) - 1;

else if x > 10

y = x/(x - 11);

end

end

end

end

end

Output:

f(4)

ans = 9.1188e-04

f(5)

ans = 0.0025

f(5.5)

ans = -12

f(10)

ans = -21

f(11)

sorry! f(x) is not defined at x=11

f(12)

ans = 12

output is tested for a wide range of inputs and the program is returning correct output values.

User Gabriel Mesquita
by
4.5k points