Plotting Piecewise functions in Matlab

192 Views Asked by At

I tried this code in MATLAB in order to plot the piecewise function:

f(x) = cos (x) for x < 0 and e^-x (1 - x^2) for x >= 0: p y=piecewise(x)

plot(x,y)

xlabel('x')

ylabel('y')

title('Plots of $y = \cos x$ and $e^(-x)(1 - x^2)$', 'interpreter', 'latex')

I can't seem to find much information online in regards to plotting these quite simplistic Piecewise functions in MATLAB.

1

There are 1 best solutions below

0
On

Install the symbolic math toolbox and then this should work:

syms x
y = piecewise(cos(x), x<0, exp(-x)*(1-x^2), x>=0)
fplot(y)