Re: Problem with conditional definitions

• To: mathgroup at smc.vnet.net
• Subject: [mg19535] Re: [mg19464] Problem with conditional definitions
• From: "David Park" <djmp at earthlink.net>
• Date: Tue, 31 Aug 1999 00:52:24 -0400
• Sender: owner-wri-mathgroup at wolfram.com

```Kevin McCann wrote:

>I have recently uncovered a problem with conditional definitions of
>functions.  The following is a simple example
>
>Remove[f]
>
>f[x_ /; x <= 0] := x^2
>f[x_ /; 0 < x < 1] := x^3
>f[x_ /; x >= 1] := x^4
>
>{f[1.], f'[1.],f''[1.]}
>
>{1.,3.54208,18.8746}
>
>Clearly incorrect. Or
>
>{f[1.1],f'[1.1],f''[1.1]}
>
>             {1.4641, 5.28974, 8.77428}
>Correct: {1.4641, 5.324,  14.52}
>
>So it is not that the function was evaluated at a boundary.
>
>Also, if I use exact arguments:
>
>{f[1],f'[1],f''[1]}
>
>{1,f'[1],f''[1]}
>
>None of the function definitions lead to the answers I got. Did I miss
>something?
>--
>
>Kevin J. McCann
>Johns Hopkins University APL
>

Kevin,

For piecewise functions, it is far better to use UnitStep. Then derivatives and
integrals all work properly.

g[x_] :=
x^2*(1 - UnitStep[x]) +
x^3*(UnitStep[x] -
UnitStep[x - 1]) +
x^4*UnitStep[x - 1]

{g[1], g'[1], g''[1]}
{1, 4, 12 + 2 DiracDelta[0]}

{g[1.1], g'[1.1], g''[1.1]}
{1.4641, 5.324, 14.52}

I am sending a notebook giving more detail in a separate email.

David Park