Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
How to express Symbolic Function with Cases?

Subject: How to express Symbolic Function with Cases?

From: Thomas Schreiter

Date: 7 Feb, 2012 14:56:11

Message: 1 of 2

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?

(The approach with f = (x<2) * 1 + (x>=2) * 1/2*x leads to an error: "Function 'lt' is not implemented for MuPAD symbolic objects.")

Regards
/Thomas

Subject: How to express Symbolic Function with Cases?

From: Christopher Creutzig

Date: 8 Feb, 2012 08:37:28

Message: 2 of 2

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

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us