|
On 07.02.12 15:56, Thomas Schreiter wrote:
> Hi,
>
> I want to solve a system of equations with the symbolic toolbox. One of the functions is defined by cases, e.g.
>
> f(x) = 1 if x < 2,
> f(x) = 1/2*x else
>
> How can f be expressed in a symbolic expression?
One option would be to not use MATLAB-style input and go for MuPAD's
piecewise expressions:
f = evalin(symengine, 'piecewise([x < 2, 1], [Otherwise, x/2])');
That works fine for int, subs, etc., and the output isn't too bad when
using pretty(f), but it fails for matlabFunction, ezplot etc.
Alternatively, if you know x is real-valued (and if f had a
discontinuity, were not too worried about the value exactly at x==2),
you could use Heaviside functions:
f = heaviside(2-x) * 1 + heaviside(x-2) * x/2
Christopher
|