Search code examples
matlabpiecewise

Piecewise function not recognised by MATLAB


Im trying to set up a symbolic expression V using the symbolic variable x like:

V = piecewise(x<=0.5, 2*x, x>0.5, 2-2*x)

However MATLAB doesn't even recognise this command although it is mentioned in their documentation.

The error it keeps returning is:

Undefined function or variable 'piecewise'.

I searched online for this error message, and some names like MuPad Notebook kept popping up, but I don't know what those mean either. I do have the Symbolic Math Toolbox ( I checked using which privResolveArgs). Finally, Im using MATLAB 2016a if that helps.

Thanks for all the help, in advance.


Solution

  • The piecewise function is available starting from MATLAB version 2016b. In 2016a you have to use the function heaviside:

    syms y(x)
    y(x)= heaviside(x-0.5)*(2-2*x)+heaviside(0.5-x)*2*x