       Re: Piecewise functions definition and usage

• To: mathgroup at smc.vnet.net
• Subject: [mg24273] Re: [mg24098] Piecewise functions definition and usage
• From: BobHanlon at aol.com
• Date: Wed, 5 Jul 2000 23:10:57 -0400 (EDT)
• Sender: owner-wri-mathgroup at wolfram.com

```In a message dated 7/4/2000 10:16:23 PM, vio2 at Lehigh.EDU writes:

>Thanks,
>Nice example. My calculations are very big and if I use your suggestion
>in the following:
>
>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]
>
>I get a complex number when I shouldn't.
>
>If I wnat to plot        NumberDens[0,Evar]   I get no result.
>
>My real program is much more complex and of course I can't get it working
>with piecewise functions.
>
>
>BobHanlon at aol.com wrote:
>
>> In a message dated 6/27/2000 1:06:55 AM, vio2 at mail.lehigh.edu writes:
>>
>> >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 !!
>> >
>>
>> Needs["Algebra`InequalitySolve`"]
>>
>> InequalitySolve[Abs[x - 3] < 10, x]
>>
>> -7 < x < 13
>>
>> f[x_] := Sqrt[
>>       1 - ( (x - 3)/10)^2  ] * (UnitStep[(x + 7)] - UnitStep[(x - 13)])
>>
>> Plot[f[x], {x, -10, 15},
>>     PlotStyle -> {AbsoluteThickness, RGBColor[1, 0, 0]}];
>>
>> Integrate[f[x], {x, -1, 17}]
>>
>> (2*Sqrt)/5 + (5*Pi)/2 + 5*ArcSin[2/5]
>>
>> % // N
>>
>> 11.74459614229426
>>
>> Bob Hanlon
>

PeakUp[n_] := n + 10;

DOSup[n_, Energy_, G_:10] :=
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]

5*I*Log - 10*I*Log[2*I*Sqrt]

Although this may appear to be complex, it is readily shown to be real by
simplifying the expression

% // Simplify

5*Pi

Looking at the results for other values of n

examples = Table[FullSimplify[NumberDens[n, 20]], {n, 0, 5}]

Examination of the pattern of these results leads to the supposition that for
0<= n <= 20,
NumberDens[n, 20] is given by

nd20[n_] := (1 - n/10)*Sqrt[n(20 - n)]/2 + 5ArcCos[n/10 - 1];

Thread[Equal[examples, Table[nd20[n], {n, 0, 5}]]] // FullSimplify

{True, True, True, True, True, True}

However, n need not necessarily be an integer, for example

FullSimplify[NumberDens[#, 20] == nd20[#]] & /@ {Pi, E, 2.25, 20}

{True, True, True, True}

A proof is left for a mathematician.

plot20 = Plot[nd20[n], {n, 0, 20}, PlotStyle -> RGBColor[0, 0, 1]];

Similarly for Evar = 10

NumberDens[0, 10]

5*I*(2*Log[1 + I] + Log) - 10*I*Log[2*I*Sqrt]

% // FullSimplify

(5*Pi)/2

examples = Table[FullSimplify[NumberDens[n, 10]], {n, 0, 5}]

For 0<=n <= 10,

nd10[n_] := -n*Sqrt[100 - n^2]/20 + 5*ArcCos[n/10];

Thread[Equal[examples, Table[nd10[n], {n, 0, 5}]]] // FullSimplify

{True, True, True, True, True, True}

FullSimplify[NumberDens[#, 10] == nd10[#]] & /@ {Pi, E, 2.25, 10}

{True, True, True, True}

plot10 = Plot[nd10[n], {n, 0, 10}, PlotStyle -> RGBColor[1, 0, 0]];

Since the plots appear to be pretty smooth we can use MultipleListPlot to
plot the function

plotData =
Table[{n, FullSimplify[NumberDens[n, Evar]]}, {Evar, 5, 20, 5}, {n, 0,
Evar, Evar/10}];

Needs["Graphics`MultipleListPlot`"]

mlp = MultipleListPlot[Sequence @@ plotData, DisplayFunction -> Identity];

Show[{plot10, plot20, mlp}, DisplayFunction -> \$DisplayFunction,
ImageSize -> {500, 310}];

Note that since we know the results are real, you could speed up the
generation of plotData by using Chop[N[_]] rather than FullSimplify[_]

Bob Hanlon

```

• Prev by Date: Re: Drawing polytopes
• Next by Date: Re: Drawing polytopes
• Previous by thread: Re: Piecewise functions definition and usage
• Next by thread: Re: A few questions about RowReduce - bis