MathGroup Archive 1998

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

Search the Archive

Differentiating Piecewise Functions

  • To: mathgroup at smc.vnet.net
  • Subject: [mg14752] Differentiating Piecewise Functions
  • From: Des Penny <penny at suu.edu>
  • Date: Thu, 12 Nov 1998 02:17:50 -0500
  • Organization: Southern Utah University
  • Sender: owner-wri-mathgroup at wolfram.com

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.924302,\

-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.924302,\

-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.924302,\

-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



  • Prev by Date: Re: Multiplying large polynomials
  • Next by Date: Damaged Mathematic Systems Files, and Deleting Mathematica Caches?
  • Previous by thread: Re: Mathematica Lightning Model?
  • Next by thread: Re: Differentiating Piecewise Functions