       Re: Piecewise functions

• To: mathgroup at smc.vnet.net
• Subject: [mg51581] Re: [mg51553] Piecewise functions
• From: "David Park" <djmp at earthlink.net>
• Date: Sat, 23 Oct 2004 00:22:36 -0400 (EDT)
• Sender: owner-wri-mathgroup at wolfram.com

```Luca,

Not totally clear. But here are two ways to define the function.

Clear[y]
y[x_] /; x > 3 := x
y[x_] /; -1 < x <= 3 := -x
y[x_] := 1

Plot[y[x], {x, -2, 4}];

But this method does not allow you to correctly calculate

y'[x]
0

So, UnitStep is usually the better way.

Clear[y]
y[x_] := 1(1 - UnitStep[x + 1]) - x(UnitStep[x + 1] - UnitStep[x - 3]) +
x UnitStep[x - 3]

This can also be written as

Clear[y]
y[x_] := 1(UnitStep[x + Infinity] - UnitStep[x + 1]) -
x(UnitStep[x + 1] - UnitStep[x - 3]) + x UnitStep[x - 3]

which has the form of a sum of (value expression)(turn on point - turn off
point)

Plot[y[x], {x, -2, 4}];

Now, you could integrate the function...

yintegral[x_] = Integrate[y[t], {t, -2, x}]
2 + x - 9*UnitStep[-3 + x] + x^2*UnitStep[-3 + x] -
(1/2)*UnitStep[1 + x] - x*UnitStep[1 + x] -
(1/2)*x^2*UnitStep[1 + x]

Plot[yintegral[x], {x, -2, 4}];

or differentiate it.

y'[x]
x DiracDelta[-3 + x] - DiracDelta[1 + x] -
x (-DiracDelta[-3 + x] + DiracDelta[1 + x]) + 2 UnitStep[-3 + x] -
UnitStep[1 + x]

But the plot usually doesn't catch the deltas.

Plot[y'[x], {x, -2, 4}, PlotRange -> All];

With DrawGraphics, from my web site, it's easy to make somewhat better
plots.

Needs["DrawGraphics`DrawingMaster`"]

DrawUnitStep eliminates the jump lines at the discontinuities.

Draw2D[
{DrawUnitStep[y[x], {x, -2, 4}]},
Frame -> True,
PlotLabel -> HoldForm[y[x]],
Background -> Linen,
ImageSize -> 400];

We can even get in the DiracDeltas by converting them to Arrows with the
following routine. Notice that the second delta is actually zero.

Cases[Collect[y'[x], DiracDelta[_]], a_ DiracDelta[x + b_]]
diracs = % /.
a_ DiracDelta[x + b_] :>
Arrow[{-b, 0}, {-b, (a /. x -> -b)}, HeadCenter -> 1/2]

Draw2D[
{DrawUnitStep[y'[x], {x, -2, 4}],
Red, diracs},
Frame -> True,
PlotRange -> All,
Background -> Linen,
PlotLabel -> HoldForm[y'[x]],
ImageSize -> 400];

David Park

From: Luca [mailto:luca at nospam.it]
To: mathgroup at smc.vnet.net

Hi all. I'm studying for the exam of signals and systems and I was
trying to plot some kind of functions I transformed for exercise. So, I
need to plot piecewise functions like:

y(x) = x if x > 3
y(x) = -x if -1 < x < 3
y(x) = 1 else

(should have been a system).
is possible with the function UnitStep, which I know. Anyway, I found
it difficult to determine the equation of the function using this
method. Is it possible to do it simply writing everything like I did
before, more or less? i.e. without having to determine the equation
with the UnitStep function.
Hope I've been clear enought. Many thanks.

Luca

```

• Prev by Date: Re: Step by step answer ?
• Next by Date: Fitting question
• Previous by thread: Re: Piecewise functions
• Next by thread: Re: Piecewise functions