Re: Re: Problems with NIntegrate
- To: mathgroup at christensen.cybernetics.net
- Subject: [mg1805] Re: [mg1692] Re: [mg1645] Problems with NIntegrate
- From: Allan Hayes <hay at haystack.demon.co.uk>
- Date: Mon, 31 Jul 1995 23:08:14 -0400
(Two other ways) In [mg1645] Problems with NIntegrate Ian Barringer, Ian.Barringer at Brunel.ac.uk Wrote > HI, > I have come up against a problem in Mathematica I hope > someone in this group may be able to explain to me. > I have defined a function as follows; > fp[p_]=Sqrt[(x^2-1)/(p^2-x^2)] > which I wish to integrate numerically. This I can do with > integer values of p: > NIntegrate[fp[6],{x,1,6}] > = 5.69279 > However, if I set p as a real value, such as: > NIntegrate[fp[6.2],{x,1,6.2}] > I get an inexact arithmetic error, and no answer. In [mg1692] John Fulz <jfultz at wri.com> replied > It *would* affect both integer and real values of p identically, if > it weren't for those tiny little errors computers are always prone > to making in the last digit of machine precision calculations. I > was able to get your answer to calculate on my machine with: > > In[24]:= NIntegrate[fp[6.2], {x, 1, 6.2 - 2 $MachineEpsilon}] > > Out[24]= 5.90009 Here are two variants that enable us to stand back a little more. I unformat the answers to show more digits First John's form: NIntegrate[fp[6.2],{x,1,6.2- 2 $MachineEpsilon}] 5.900087277504107 Block[{NIntegrate}, Rationalize[ N[ NIntegrate[fp[6.2], {x,1,6.2}] ], $MachineEpsilon ] ] 5.900087277601828 Using Unevaluated: Rationalize[ Unevaluated[NIntegrate[fp[6.2], {x,1,6.2}]], $MachineEpsilon ] 5.900087317994875 The form using Unevaluated is likely to be the most accurate because the rationalization is done on the atomic parts of the integral before evaluation (eg before 6.2^2 becomes 38.44), but it cannot automatically deal with symbolic constants like Pi or other parts that evaluate to numbers. However we could try rewriting the integral to get round this dificulty. Here is how the block form copes with Pi: Block[{NIntegrate}, Rationalize[ N[ NIntegrate[fp[Pi], {x,1,Pi}] ], $MachineEpsilon ] -11 2.65548 + 5.57794 10 I Chop[%] 2.65548 Whereas with the unevaluated form we get Rationalize[Unevaluated[NIntegrate[fp[Pi],{x,1,Pi}]],$MachineEpsilon] 1 Power::infy: Infinite expression -- encountered. 0. 1 Power::infy: Infinite expression -- encountered. 0. NIntegrate::rnderr: Inexact arithmetic has caused {x} to take the value {3.14159} where the integrand is singular. NIntegrate[fp[Pi], {x, 1, Pi}] Allan Hayes, De Montfort University Leicester hay at haystack.demon.co.uk