MathGroup Archive 2002

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

Search the Archive

RE: How to ...? Piecewise Function Definition

  • To: mathgroup at
  • Subject: [mg36337] RE: [mg36309] How to ...? Piecewise Function Definition
  • From: "David Park" <djmp at>
  • Date: Mon, 2 Sep 2002 04:08:45 -0400 (EDT)
  • Sender: owner-wri-mathgroup at


You could look up "piecewise functions" in the Master Index in Help.
Unfortunately, the book does not have a unified discussion of methods for
defining piecewise functions.

The first method is to use a set of conditional definitions. For
completeness I made f[x] = 0 if one of the conditional definitions is not

f[x_] /; 0 < x < 6 := x^2
f[x_] /; x >= 6 := x + 1
f[x_] := 0

Plot[f[x], {x, 0, 10}];

The second method is to use a Which statement.

f[x_] :=
    0 < x < 6, x^2,
    x >= 6, x + 1,
    True, 0]

Plot[f[x], {x, 0, 10}];

The above methods are fine in many cases, but if you want to perform
functions on f, such as differentiation or integration, you should use the
UnitStep function. So our third method is...

f[x_] := x^2(UnitStep[x] - UnitStep[x - 6]) + (x + 1)UnitStep[x - 6]

Plot[f'[x], {x, 0, 10}];

Plot[Integrate[f[y], {y, 0, x}] // Evaluate, {x, 0, 10}];

David Park
djmp at

From: Paskoski [mailto:berlusconi_pagliusi at]
To: mathgroup at

Dear MathGroup,
I'd like to use Mathematica 4.0 to write a function having different
expressions in different domain's intervals.
Let's say:

F[x_]=    x^2   if 0<x<6
               x+1   if x>=6

I know It's a stupid syntax problem, but I really do not know how/where to
search the solution on the Mathematica Book

Thank you in advance,


  • Prev by Date: RE: L2 inner product. Integrate and Conjugate?
  • Next by Date: GraphicsArray, scaling and AspectRatio
  • Previous by thread: RE: Re: Mathematica 4.2 & Strange Plot results
  • Next by thread: GraphicsArray, scaling and AspectRatio