Re: Differentiating Piecewise Functions
- To: mathgroup at smc.vnet.net
- Subject: [mg14757] Re: Differentiating Piecewise Functions
- From: "Allan Hayes" <hay at haystack.demon.co.uk>
- Date: Sat, 14 Nov 1998 03:07:50 -0500
- References: <72e2q0$oi3@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
Des,
There seems to be a problem the numerical differentiation routine.
With
In[1]:=
Clear[f, g, x]
f[x_ /; (x < 0)] := x;
f[x_ /; (x >= 0 && x < 3)] := Sin[x]; f[x_ /; (x >= 3)] := x - 4;
We get
In[5]:=
g[x_] = D[f[x], x]
Out[5]=
f'[x]
since f[x] meets non of the conditions for the rules defined above.
Now with
In[6]:=
Table[{Cos[x], g[x]}, {x, 2.6, 2.99, 0.05}]
Out[6]=
{{-0.856889, -0.856686}, {-0.881582, -0.885125},
{-0.904072, -0.874532}, {-0.924302, -1.08174},
{-0.942222, -0.333364}, {-0.957787, -2.82107},
{-0.970958, 4.01021}, {-0.981702, -15.6121}}
To get first entry in this list Table sets x=2.6 and then the calcuation
proceeds
{Cos[x], g[x]} ->
{Cos[2.6], g[x]}->
{-0.856889, g[x]} ->
{-0.856889, g[2.6]} ->
{-0.856889, f'[2.6]} ,
at this point it seems that a numerical differentiaton routine is
executed and here, and subsequently, gives an incorrect answer.
With
In[7]:=
Clear[f, g]
f[x_Real] := Which[x < 0, x, (x >= 0 && x < 3), Sin[x], (x >= 3), x - 4]
we get
In[9]:=
g[x_] = D[f[x], x]
Out[9]=
f'[x]
(since x is not real)
So the table evaluates in the same way
But with
In[10]:=
Clear[f, g]
f[x_] := Which[x < 0, x, (x >= 0 && x < 3), Sin[x], (x >= 3), x - 4]
In[12]:=
g[x_] = D[f[x], x]
Out[12]=
Which[x < 0, 1, x >= 0 && x < 3, Cos[x], x >= 3, 1] since there is no
restriction on x.
Now the steps in evaluating the first entry in the table (with x =
2.6)are
{Cos[x], g[x]} ->
{Cos[2.6], g[x]}->
{-0.856889, Which[x < 0, 1, x >= 0 && x < 3, Cos[x], x >= 3, 1]}->
{-0.856889, Cos[2.6]} -> {-0.856889, -0.856889}
Allan
---------------------
Allan Hayes
Mathematica Training and Consulting
www.haystack.demon.co.uk
hay at haystack.demon.co.uk
Voice: +44 (0)116 271 4198
Fax: +44 (0)870 164 0565
-
**********************
Des Penny wrote in message <72e2q0$oi3 at smc.vnet.net>...
>Hi Folks:
>I've got a problem understanding how Mathematica finds the derivative of
>a piecewise function.
>
>Consider the following:
>In[1]:=
>Clear[f]
>f[x_ /; (x<0)]:=x;
>f[x_ /; (x>=0 && x<3)]:=Sin[x];
>f[x_ /; (x>=3)]:=x-4;
>
>In[3]:=
>Clear[g]
>g[x_]=D[f[x],x]
>Out[4]=
>\!\(\*
> RowBox[{
> SuperscriptBox["f", "\[Prime]",
> MultilineFunction->None], "[", "x", "]"}]\)
>
>In[5]:=
>Table[{Cos[x],g[x]},{x,2.6,2.99,0.05}] Out[5]=
>{{-0.856889,-0.856686},{-0.881582,-0.885125},{-0.904072,-0.874532},{-0.9243
02,\
>
>-1.08174},{-0.942222,-0.333364},{-0.957787,-2.82107},{-0.970958,
> 4.01021},{-0.981702,-15.6121}}
>In[6]:=
>Table[{1,g[x]},{x,3.01,3.5,0.05}]
>Out[6]=
>{{1,-14.1159},{1,6.1614},{1,-0.955307},{1,1.64929},{1,1.59634},{1,0.842939}
,{
>
> 1,1.03004},{1,0.996326},{1,1.00021},{1,1.}}
>
>The first element of each sublist above is the theoretical value, the
>last element is the output of g.
>It appears that the derivative is wrong in the domain {2.6,3.45}.
>
>The same behavior seems to occur with the Which function: In[7]:=
>Clear[f]
>f[x_Real]:=Which[x<0,x, (x>=0 && x<3),Sin[x],(x>=3),x-4]
>
>In[9]:=
>Clear[g]
>g[x_]=D[f[x],x]
>Out[10]=
>\!\(\*
> RowBox[{
> SuperscriptBox["f", "\[Prime]",
> MultilineFunction->None], "[", "x", "]"}]\) In[11]:=
>Table[{Cos[x],g[x]},{x,2.6,2.99,0.05}] Out[11]=
>{{-0.856889,-0.856686},{-0.881582,-0.885125},{-0.904072,-0.874532},{-0.9243
02,\
>
>-1.08174},{-0.942222,-0.333364},{-0.957787,-2.82107},{-0.970958,
> 4.01021},{-0.981702,-15.6121}}
>In[12]:=
>Table[{1,g[x]},{x,3.01,3.5,0.05}]
>Out[12]=
>{{1,-14.1159},{1,6.1614},{1,-0.955307},{1,1.64929},{1,1.59634},{1,0.842939}
,{
>
> 1,1.03004},{1,0.996326},{1,1.00021},{1,1.}}
>
>Again things are wrong in the domain {2.6,3.45}, however the answers
>seem to be the same as in the first definition.
>
> Now if I change the definition of the Which slightly things are much
>better:
>In[13]:=
>Clear[f]
>f[x_]:=Which[x<0,x, (x>=0 && x<3),Sin[x],(x>=3),x-4]
>
>In[15]:=
>Clear[g]
>g[x_]=D[f[x],x]
>
>Out[16]=
>Which[x<0,1,x\[GreaterEqual]0&&x<3,1 Cos[x],x\[GreaterEqual]3,1]
>
>In[17]:=
>Table[{Cos[x],g[x]},{x,2.6,2.99,0.05}] Out[17]=
>{{-0.856889,-0.856889},{-0.881582,-0.881582},{-0.904072,-0.904072},{-0.9243
02,\
>
>-0.924302},{-0.942222,-0.942222},{-0.957787,-0.957787},{-0.970958,-0.970958
},{\
>
>-0.981702,-0.981702}}
>In[18]:=
>Table[{1,g[x]},{x,3.01,3.5,0.05}]
>Out[18]=
>{{1,1},{1,1},{1,1},{1,1},{1,1},{1,1},{1,1},{1,1},{1,1},{1,1}}
>
>Can anyone throw some light on what Mathematica is doing internally to
>get these results?
>
>Cheers,
>
>Des Penny
>Physical Science Dept
>Southern Utah University
>Cedar City, Utah 84720
>
>Voice: (435) 586-7708
>FAX: (435) 865-8051
>Email: penny at suu.edu
>
>