Re: piecewice pdf, problems with cdf
- To: mathgroup at smc.vnet.net
- Subject: [mg105413] Re: [mg105365] piecewice pdf, problems with cdf
- From: michael partensky <partensky at gmail.com>
- Date: Tue, 1 Dec 2009 04:16:52 -0500 (EST)
- References: <200911291012.FAA16385@smc.vnet.net>
On Sun, Nov 29, 2009 at 12:03 PM, <danl at wolfram.com> wrote: > > Hi! Teaching the continuous distributions, I needed to introduce the > > piecewise functions. > > Here is the example that did not work well: > > > > In[56]:= f1[x_] /; 0 < x <= 3 := 1/9 x ^2; > > f1[x_] := 0; > > > > Plot[f1[x],{x,-1,4}] works fine. However, the results for cdf are > > ambiguous > > In[57]:= cdf[x_] := Integrate[f1[v], {v, -\[Infinity], x}] > > > > In[59]:= cdf[1] > > Out[59]= 0 > > > > I thought that may be the second definition (for some reason) overwrote > > the > > first, but apparently this was not the case. > > > > Then I tried using Which, > > > > f1[x_] := Which[0 < x <= 3, x^2/9, x <= 0 || x > 3, 0]; > > > > Plot[f2[x], {x, -1, 4}] worked fine. > > > > However, Plotting CDF is very slow. > > > > What is the reason for the first error and how to accelerate (compile?) > > the > > second? > > > > Thanks > > Michael > > > > PS: I was aware about the issues with the derivatives of Piecewise > > functions, but expected integration to be safe. What did i do wrong? > > [...,] > > You did not use Piecewise. Those other methods do not play well with > Integrate. Actually this has been discussed in this forum a couple of > times in the past four months (though those discussions had a way of going > off-line, and taking me with them). > > Anyway, use Piecewise; this is the sort of thing it is designed to handle. > > f1[x_] := Piecewise[{{x^2/9, 0 < x <= 3}}, 0] > > This will work fine. To make the cdf much faster, use Set rather than > SetDelayed, and let Integrate know x is real valued. > > In[100]:= cdf[x_] = Integrate[f1[v], {v, -Infinity, x}, > Assumptions -> Element[x, Reals]] > > Out[100]= Piecewise[{{1, x > 3}, {x^3/27, > Inequality[0, Less, x, LessEqual, > 3]}}, 0] > > Daniel Lichtblau > Wolfram Research > Thanks, Daniel. Using 'Set' works great indeed! Best Michael