Write a function called piecewise that has one input a numbe
Write a function called \"piecewise\" that has one input, a number x, and one output, number y, that uses a conditional statement that implements the following piecewise function. Y = x^2 when 0 > x > -1 y = squareroot (-x) when -1 >= x y = cos (x - 1) when x >= 0
Solution
Please create piecewise.m file in matlab and paste below code
% this is function defination
function y = piecewise(x)
if((x > -1) && (x < 0)) %condition 1
y = x * x;
end
if(x <= -1) %condition2
y = sqrt(-x);
end
if(x >= 0) %condition3
y = cos(x-1);
end
end
Now from command window call the function as
>> piecewise(1)
>> piecewise(-0.3)
You will get answers for all the values and conditions mentioned in question
