In: Computer Science
Please show MATLAB code to plot below function from x = −3 to x = 12:
function f = piecewise(x)
% implements piecewise function using if statements
if x < 0
f = -x^3 - 2*x^2 + 3*x;
elseif x <= 8
f = (12/pi) * sin(pi*x/4);
else
f = (600*exp(x-8))/(7*(14 + 6*exp(x-8)))
-30/7;
end
x = [-3,-2,-1,0,1,2,3,4,5,6,7,8,9,10,11,12,13]; % take the input array
function f = piecewise(x) %define function - piecewise
for c = 1:17 %Travel through the input array by taking each element
where c is index
if x(c) < 0 % x(c) is element of the input array where "c" is
index
f(c) = -x(c)^3 - 2*x(c)^2 + 3*x(c); %calculate the value as
given
elseif x(c) <= 8 % x(c) is element of the input array where "c"
is index
f(c) = (12/pi) * sin(pi*x(c)/4);%calculate the value as given
else
f(c) = (600*exp(x(c)-8))/(7*(14 + 6*exp(x(c)-8))) -30/7;%calculate
the value as given
end %end of the if condition
end %end of the for loop
end %end of the function
f = piecewise(x); % call the piecewise function
plot(x,f) %plot graph