Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mathematica code: Derivative of Abs[x]

Note to closers : This is a question about a Programming language (Mathematica), and not about a discipline/science (mathematics).

Why is

N[D[Sin[x], x] /. x -> Pi/4]
(*
Out -> 0.707107
*)

but

N[D[Abs[x], x] /. x -> Pi/4]
(*
Out -> Derivative[1][Abs][0.785398]
*)

?

And what is the better way to force a numerical result?

like image 349
Dr. belisarius Avatar asked Jun 13 '11 13:06

Dr. belisarius


People also ask

How do you write abs in Mathematica?

Abs is also known as modulus. Mathematical function, suitable for both symbolic and numerical manipulation. For complex numbers z, Abs[z] gives the modulus . Abs[z] is left unevaluated if z is not a numeric quantity.


2 Answers

Abs[z] is not a holomorphic function, so its derivative is not well defined on the complex plane (the default domain that Mathematica works with). This is in contradistinction to, e.g., Sin[z], whose complex derivative (i.e., with respect to its argument) is always defined.

More simply put, Abs[z] depends on both z and z*, so should really be thought as a two argument function. Sin[z] only depends on z, so makes sense with a single argument.

As pointed out by Leonid, once you restrict the domain to the reals, then the derivative is well defined (except maybe at x=0, where they've taken the average of the left and right derivatives)

In[1]:= FullSimplify[Abs'[x],x \[Element] Reals]
Out[1]= Sign[x]

As pointed out by Szabolcs (in a comment), FunctionExpand will simplify the numerical expressions, but "Some transformations used by FunctionExpand are only generically valid".

ComplexExpand also gives numeric results, but I don't trust it. It seems to take the derivative assuming the Abs is in the real domain, then substitutes in the numeric/complex arguments. That said, if you know that everything you're doing is in the reals, then ComplexExpand is your friend.

like image 149
Simon Avatar answered Nov 01 '22 20:11

Simon


I refer you to this thread as possibly relevant - this issue has been discussed before. To summarize my answer there, Abs is defined generally on complex numbers. Once you specify that your argument is real, it works:

 In[1]:= FullSimplify[Abs'[x], Assumptions -> {Element[x, Reals]}] 

 Out[1]= Sign[x] 
like image 37
Leonid Shifrin Avatar answered Nov 01 '22 21:11

Leonid Shifrin