MathGroup Archive 2000

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

Search the Archive

Re: Piecewise functions definition and usage

You can't expect Mathematica to work if you feed it a command which vilates
the rules of the Mathematica language (your second example). What you wrote
is not a Mathematica program. The correct form is:

f[x_] := If[Abs[x - 3] < 10, Sqrt[1 - ( (x - 3)/10)^2] , 0]

However, I do not understand why you claim that Mathematica "goes nuts" in
the first case, which is more or less correct. (Actually  a bit weird: why
do you use contradictory conditions over an overlaping range of x?
Presumably you meant the first to be Abs[x-3]<10 and the second
Abs[x-3]>=10. In any case,  it does not metter for your second condition is
ignored where it contradicts the first, e.g. when x=7.)  What I get, even
with your original code, is this:


f[x_] := Sqrt[ 1 - ( (x - 3)/10)^2  ] /;
Abs[x - 3] < 10

f[x_] := 0 /;
Abs[x - 3] > 3 ; 

NIntegrate[f[x], {x, -1, 17}]

NIntegrate::"ncvb": "NIntegrate failed to converge to prescribed accuracy
after 7 recursive bisections in x near x = 12.9921875`."


All this says is that Mathematica had trouble near the singularity.  The
answer it gets is correct up to the first three digits after the decimal
point, as you can see from:

NIntegrate[f[x], {x, -1, 13, 17}]

This latter form of NIntegrate tells Mathematica about the singularity at 13
and avoids the trouble.
 In any case it is not at all surprising that a computer program finds it
difficult to cope with singularites, so do people.  If you call this "going
nuts" I wonder what your notion of sanity is :).

Andrzej Kozlowski
Toyama International University, JAPAN

For Mathematica related links and resources try:

on 6/27/00 1:51 PM, Viorel Ontalus at vio2 at wrote:

> 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: Re: Conditionals with multiple tests?
  • Next by Date: RE: Mathematica for High School Students
  • Previous by thread: RE: Piecewise functions definition and usage
  • Next by thread: Re: Piecewise functions definition and usage