MathGroup Archive 2000

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

Search the Archive

Re: Piecewise functions definition and usage

  • To: mathgroup at smc.vnet.net
  • Subject: [mg24258] Re: [mg24098] Piecewise functions definition and usage
  • From: Viorel Ontalus <vio2 at lehigh.edu>
  • Date: Wed, 5 Jul 2000 23:10:39 -0400 (EDT)
  • Organization: Lehigh University
  • References: <NDBBJGNHKLMPLILOIPPOOEIBCBAA.djmp@earthlink.net>
  • Sender: owner-wri-mathgroup at wolfram.com

Dear David,
Thank you for pointing out this method. However my calculations are very complex
and I still have some problems.
Here is an example of using  your method. As you will see, the answer is a
complex number when no complex number solutions are possible. Imagine what a
mess I get when I run this in a much bigger program that is using these results.

Clear[PeakUp, DOSup, NumberDens]
  PeakUp[n_] := n + 10;
G = 10;
DOSup[n_, Energy_] :=   UnitStep[1 -(  (Energy - PeakUp[n]) /G )^2 ) ] *Sqrt[
Abs[1 -(  (Energy - PeakUp[n]) /G )^2 )]
  NumberDens[n_, Evar_] :=    Integrate[DOSup[n, Energy], {Energy, 0, Evar}]
  NumberDens[0, 20]

You get the complex answer !!


If you do

  Plot[Evaluate[NumberDens[0, Evar]], {Evar, 0, 100}]


the computer is running  very long time and I am not sure you get a plot.



David Park wrote:

> Viorel,
>
> Whenever you plan to integrate or differentiate piecewise functions use the
> UnitStep function. In fact, UnitStep is so useful, and the multiple
> conditional definition method is so limited, that it is a bit of a shame
> that the Mathematica Book often leads people down the conditional definition
> path. UnitStep is a regular part of Version 4, but a standard package in
> earlier versions. So, here is how to do your problem using UnitStep. (I
> think that you made an error in the second conditional using 3 instead of
> 10. An alternative method of expressing the condition is that the formula
> applies for the range -7 < x < 13.) Using UnitStep we just turn the formula
> on at x == -7 and off at x == 13.
>
> f[x_] := Sqrt[ 1 - ( (x - 3)/10)^2  ](UnitStep[x + 7] - UnitStep[x - 13])
>
> The following gives a warning message because NIntegrate has trouble with
> the kink in the curve, but gives an approximately correct answer.
>
> NIntegrate[f[x], {x, -1, 17}]
> 11.7447
>
> This works better:
> NIntegrate[f[x], {x, -1, 13}]
> 11.7446
>
> You can actually obtain an exact answer using Mathematica.
>
> Integrate[f[x], {x, -1, 17}]
> N[%]
> (2*Sqrt[21])/5 + (5*Pi)/2 + 5*ArcSin[2/5]
> 11.7446
>
> Piecewise functions? Always think UnitStep.
>
> David Park
> djmp at earthlink.net
> http://home.earthlink.net/~djmp/
>
> > -----Original Message-----
> > From: Viorel Ontalus [mailto:vio2 at mail.lehigh.edu]
To: mathgroup at smc.vnet.net
>
> >
> > It seems I got into an area where Mathematica has some problems, and I
> > hope somebody can give me a hint on how to go around these problems.
> >
> > 1.  I am trying to define a piecewise function and do some computations
> > with it. When I integrate mathematica does not behave. Here is an simple
> > example you can run and see what I am talking about
> >
> > Clear[f,x}
> > f[x_] := Sqrt[ 1- ( (x-3)/10)^2  ] /;
> > Abs[x-3]<10
> > f[x_] := 0 /;
> > Abs[x-3]>3 ;          (*this is a very simple piecewise function but one
> > must be sure the Sqrt is from a positive # )
> >
> > NIntegrate[f[x],{x,-1,17}]   (* Here Mathematica goes nuts !!!*)
> >
> > Of course it gives an answer but if your program is more complex, then
> > you never get an answer !!
> > ( I tried to make the upper limit a variable  etc !!)
> > Does anybody know how to avoid the error , or non convergence messages I
> > get !!
> >
> >
> >
> > 2. For fun I tried the only reference from the book on piecewise
> > functions:
> > If[Abs[x-3]<10, f[x_]:=Sqrt[1- ( (x-3)/10)^2  ] , f[x_]:=0 ]
> >
> > This definition does not work !!
> >
> >
> >
> >
> >
> >
> >
> >
> >
> >



  • Prev by Date: LogPlot != Plot[Log]
  • Next by Date: RE: Using Block instead of Module?
  • Previous by thread: RE: LogPlot != Plot[Log]
  • Next by thread: Re: Piecewise functions definition and usage