# MATLAB: Creating a Piecewise Symbolic function

createfunctionpiecewisesymbolic

hello!
I need help creating a piecewise function out of 2 symbolic functions. I have:
a = poly2sym(polyfun); %Convert to symbolic
b = poly2sym(polyfun1); %Convert to symbolic
Now I want to create a function f, that is the piecewise composition of a and b. I can use the heavisde function for regular numeric functions like this:
j = '(heaviside(x)-heaviside(x-1))'*x.^2;
But the above is a numeric expression. If I try to make mine numeric by doing:
j = '(heaviside(x)-heaviside(x-1))'*matlabFunction(a)
It returns an error about using "mtimes". A and b are both symbolic, and I need j to be pieciewise and also symbolic. Any ideas?
thanks in advance!

#### Best Answer

• '(heaviside(x)-heaviside(x-1))'*x.^2 means the string '(heaviside(x)-heaviside(x-1))' matrix-multiplied by the element-wise square of x.
Anything in quotes is a string, not symbolic.
Also, matlabFunction() returns a function handle, not a symbolic function.
syms x(heaviside(x)-heaviside(x-1)) * a
would be a symbolic expression. You could matlabFunction() the result, I suppose.