Re: Symbolic Derivative of Piecewise Contin Fcn

• To: mathgroup at smc.vnet.net
• Subject: [mg15532] Re: [mg15490] Symbolic Derivative of Piecewise Contin Fcn
• From: BobHanlon at aol.com
• Date: Tue, 26 Jan 1999 13:44:23 -0500 (EST)
• Sender: owner-wri-mathgroup at wolfram.com

```In a message dated 1/18/99 9:14:16 AM, sullivan at indra.com writes:

>How can I get a symbolic derivative on a region of a piecewise
>continuous function using Mathematica? Two simple examples, extracted
>from long Modules are below.
>
>Alternatively, is there some magic functional M that, given a piecewise
>contin function, would return a piece of it?  For example, let:   f[x_]
>:= If[ x < 0, x^2, x^5] Is there a M such that:   M[ f, {x < 0}] would
>return:  x^2
>
>Many thanks for any help ...
>Steve
>
>
>The following examples of the failure of D[] are extracted from much
>more complicated functions.  They were run using Mathematica 3.0.
>
>htest[ uval_ ] := Module [
>	{res},
>	If[ uval < 0, res = uval^2, res = uval^5];
>	res
>]
>
>D[ htest[ uval], uval]
>==> returns: 0
>
>tstb[ pow_Integer, uval_ ] := Module [
>	{res},			(* local vars *)
>	If[ pow == 0,
>		If[ uval < 0, res = 0, res = uval^2],
>		res = uval^5 * tstb[ pow-1, uval]
>	];
>	res		(* return *)
>]
>
>D[ tstb[  0, uval], uval]
>==> returns: 0
>
>D[ tstb[  1, uval], uval]
>==> returns: 5 res\$4 uval^4         (?? what is res\$4 ?)

Change the definition of htest as follows:

htest[uval_] := If[uval < 0, uval^2, uval^5];

You can then take its derivative.

D[htest[uval], uval]

If[uval < 0, 2*uval, 5*uval^4]

or in a function definition

gtest[uval_] := Evaluate[D[htest[uval], uval]];

gtest[uval]

If[uval < 0, 2*uval, 5*uval^4]

tstb[0, uval_] := If[uval < 0, 0, uval^2];

tstb[pow_Integer, uval_] :=
uval^5 * tstb[pow-1, uval];

Table[tstb[k, uval], {k, 0, 3}]//ColumnForm

If[uval < 0, 0, uval^2]
uval^5*If[uval < 0, 0, uval^2]
uval^10*If[uval < 0, 0, uval^2]
uval^15*If[uval < 0, 0, uval^2]

Table[D[tstb[k, uval], uval], {k, 0, 3}] //
ColumnForm

If[uval < 0, 0, 2*uval]
uval^5*If[uval < 0, 0, 2*uval] +
5*uval^4*If[uval < 0, 0, uval^2]
uval^10*If[uval < 0, 0, 2*uval] +
10*uval^9*If[uval < 0, 0, uval^2]
uval^15*If[uval < 0, 0, 2*uval] +
15*uval^14*If[uval < 0, 0, uval^2]

If you cannot restructure a function definition in this fashion,  define
the derivative separately along the lines

Unprotect[Derivative];
Derivative[1][f][x_] :=
If[x<0, Evaluate[D[x^2, x]], Evaluate[D[x^5, x]]];
Protect[Derivative];

D[f[x], x]

If[x < 0, 2*x, 5*x^4]

Bob Hanlon

```

• Prev by Date: Re: Problems with Flat (Again)
• Next by Date: Re: a tricky limit
• Previous by thread: Re: Symbolic Derivative of Piecewise Contin Fcn
• Next by thread: ContourPlot problem