Re: Interpreting output

*To*: mathgroup at smc.vnet.net*Subject*: [mg56680] Re: Interpreting output*From*: Maxim <ab_def at prontomail.com>*Date*: Tue, 3 May 2005 05:26:31 -0400 (EDT)*References*: <200505010446.AAA13098@smc.vnet.net> <d51vpv$frp$1@smc.vnet.net>*Sender*: owner-wri-mathgroup at wolfram.com

On Sun, 1 May 2005 07:17:19 +0000 (UTC), Andrzej Kozlowski <akoz at mimuw.edu.pl> wrote: > > On 1 May 2005, at 13:46, MC wrote: > >> Hi everybody.... >> >> I'm having troubles in interpreting the output of Mathematica. >> >> My problem is to define a function F(x,y,z) such that: >> F = (whatever) if x>c >> F=(whatever) if x<=c >> >> I achievied this using /; >> >> When I derive F with respect to one of the variables, the result is a >> mess. >> Just an example with 1 variable: >> >> f=x/;x>0; >> f=-x^2/;x<0; >> >> D[f,x]=-2Condition(1,0)[x^2,x<0]+Condition(1,0)[x^2,x<0]Less(1,0)[x,0] >> >> Even in this very simple case, the result is a real mess: >> I really do not know how should I read that...and I'm wondering if >> I have defined the function in the right way. >> >> Anybody so kind to explain me what's goung on? >> >> Thank you very much for your patience >> >> > > What is going on is simply that you that Mathematica's notion of > derivative does not work with piecewise functions defined by means of > pattern matching. (Actually, there is nothing in the documentation that > suggests that it might.) > In order for differentiation to be possible you have to construct a > piecewise function using a construction that "understands" the notion > of a derivative. In Mathematica 5.1 this is easy: > > f[x_] := Piecewise[{{x^2, x < 2}, {x^3, x >= 2}}] > > f'[1] > 2 > > > f'[3] > 27 > > > f'[2] > Indeterminate > > > In earlier version you could try doing this by means of the UnitStep > function: > > g[x_] := x^2*UnitStep[2 - x] + x^3*UnitStep[x - 2] > > this will work almost the same except for the singular value > > > g'[1] > 2 > > > g'[3] > > 27 > > > > g'[1] > > 2 > > > g'[3] > > 27 > > > g'[2] > > > 4*DiracDelta[0] + 16 > > > Note also that both approaches work well when you construct a > differentiable everywhere piecewise function: > > > f[x_] := Piecewise[{{(x-1)^2, x < 1}, {(x-1)^3, x >= 1}}] > > > f'[1] > 0 > > > g[x_]:=(x-1)^2*UnitStep[1-x]+(x-1)^3*UnitStep[x-1] > > > g'[1] > > 0 > > However, this is achieved in a quite different way, which we can see > from > > > f'[x] > > Piecewise[{{2*(x - 1), x < 1}, {0, x == 1}}, 3*(x - 1)^2] > > > and > > > g'[x] > > DiracDelta[x - 2]*(x - 2)^3 - DiracDelta[x - 2]* > (x - 2)^2 + 3*UnitStep[x - 2]*(x - 2)^2 + > 2*UnitStep[2 - x]*(x - 2) > > > The second answer is given as a "generalized function" and although it > seems "correct" I would not rely on this type of use of generalised > functions (distributions) in anything but the most simple types of > situations. The Piecewise approach looks more promising. > > > > Andrzej Kozlowski > Chiba, Japan > http://www.akikoz.net/andrzej/index.html > http://www.mimuw.edu.pl/~akoz/ > But Mathematica wouldn't be Mathematica if there weren't some accompanying errors with this new feature: In[1]:= D[Piecewise[{{Sin[x]^2/x, x != 0}}], x] Out[1]= Piecewise[{{(2*Cos[x]*Sin[x])/x - Sin[x]^2/x^2, x != 0}}] In[2]:= D[Piecewise[{{0, x == 0}}, Sin[x]^2/x], x] Out[2]= Piecewise[{{0, x == 0}}, (2*Cos[x]*Sin[x])/x - Sin[x]^2/x^2] Both are incorrect because the derivative at x=0 is equal to 1. D[Piecewise[{{Sin[x]^2/x, x < 0 || x > 0}}], x] returns the correct answer. Sometimes working with generalized functions is the right thing to do, in particular when we're dealing with probabilities. For example, (UnitStep[x] + UnitStep[x - 1])/2 is the CDF for the random variable xi such that P(xi = 0) = P(xi = 1) = 1/2. This expression can be viewed either as an ordinary or a generalized function, but to obtain the correct expression for the PDF we have to differentiate it as a generalized function to get (DiracDelta[x] + DiracDelta[x - 1])/2 (Piecewise wouldn't do). Now this PDF can be used to evaluate the moment integrals. Incidentally, here we encounter another error: In[3]:= Integrate[(x - 1/2)^2*(DiracDelta[x] + DiracDelta[x - 1])/2, {x, -Infinity, Infinity}] Out[3]= 0 If we apply Expand to the integrand we get 1/4 as expected. Maxim Rytin m.r at inbox.ru

**References**:**[Newbie] Interpreting output***From:*"MC" <daemmerung@enttaeuschung.com>