Question: Given the following piecewise function, f(x): f(x)= x + 3 when x < -3 f(x)= -2x - 6 when -3

Given the following piecewise function, f(x):

f(x)= x + 3 when x < -3

f(x)= -2x - 6 when -3 <= x < -2

f(x)= x when -2 <= x < 2

f(x)= -2x + 6 when 2 <= x < 3

f(x)= x - 3 when 3 <= x < 7

f(x)= -2x + 18 when 7 <= x < 8

f(x)= 2 when x >= 8

Create a function M-file, PieceZigZag, that accepts one or more values as input, evaluates the piecewise function at that number & returns this as output.

The function should accept an array as input for x.

Use a for loop to build the output array containing f(x). Preallocate this array.

Examples:

 PieceZigZag(-10) ans = -7 
 PieceZigZag([-10 10]) ans = -7 2 

When you plot the piecewise function from -10 to 10, it looks like this. Try it on your own, for a visual check on your code.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!