MathGroup Archive 2004

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Piecewise functions

  • To: mathgroup at
  • Subject: [mg51587] Re: Piecewise functions
  • From: Bill Rowe <readnewsciv at>
  • Date: Sat, 23 Oct 2004 00:23:10 -0400 (EDT)
  • Sender: owner-wri-mathgroup at

On 10/21/04 at 10:22 PM, luca at (Luca) wrote:

>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). I found out in the guide the chapter
>about this, and I learned that it 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

Yes, it is possible to do this. For example you could do

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


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

verifies the function y behaves as desired. But there are advantages to using Unit step

For example, trying to integrate y

Integrate[y[x], {x, -2, 0}]
Integrate[y[x], {x, -2, 0}]

simply returns the integral unevaluated.

Using Unit step

g[x_] := UnitStep[-x - 1] - x*(UnitStep[x + 1] - UnitStep[x - 3]) + 
         x*UnitStep[x - 3]
and then

Plot[g[x], {x, -2, 5}];

show g is the same function as y


Integrate[g[x], {x, -2, 0}]

works since Mathematica knows how to deal with the UnitStep function. Note g could have been written as

g[x_]:=UnitStep[-x - 1] + 2*x*UnitStep[x - 3] - x*UnitStep[x + 1]

which is somewhat simpler than what I wrote above. But the way I wrote it above is easier for me to see what g does.
To reply via email subtract one hundred and four

  • Prev by Date: Re: Re: MultipleListPlot without any Point Shape, only with line made by PlotJoined -> True tests=INVALID_DATE,QUOTED_EMAIL_TEXT,RCVD_IN_ORBS,
  • Next by Date: Re: Step by step answer ?
  • Previous by thread: Re: Piecewise functions
  • Next by thread: Re: Piecewise functions