0

I am new to mathematica. I am trying to use a numerical indefinite integral as an input function of a module.

I have a function $$r^* \equiv r^*(r)=\int_0^{1/r} B(z)dz$$ with $B(z)$ being a analytic but sophisticated function. The above function is defined in mathematica as a numerical indefinite integral as follows

rStar[r_] := 
 NIntegrate[B[z1],{z1, 0, 1/r}, WorkingPrecision -> MyPrecision, MaxRecursion -> MyRecursion]

The reason to use ":=", as I understand is because that the NIntegrate asks all input to be numerical, so the expression should only be replaced when the function rStar[] is evoked with a numerical value of r.

Now what I need is the inverse function $r(r^*)$, for given value of $r^*$. So I make use of a module which uses bisection method to find the root of $r*(r)=r*$ as follows

NRootFinder[Function_, {var_, neg_, pos_}, precision_] := 
 Module[{oldm = neg, n = neg, p = pos, m = (neg + pos)/2, f, g = 10^(-precision)},
  While[Abs[n - p] > g && oldm != m,
   oldm = m;
   f = Re[Function /. var -> m];
   If[f > 0, p = m, If[f < 0, n = m, Break[]]];
   m = (n + p)/2;
  ];
  {var -> m}
 ]; 

and defines

Tortuga[x_, aa_, bb_, MyPrecision_] := 
 NRootFinder[ SetPrecision[rStar[r] - x, MyPrecision], 
  {r, SetPrecision[aa, MyPrecision], SetPrecision[bb, MyPrecision]}, MyPrecision]

NRootFinder and Tortuga work fine for any given function other than rStar[], and rStar[] works just fine for any numerical input such as rStar[0.1]. The combination, however, does not work. As I understand, mathematica requires some numerical input instead of symbolic r in NIntegrate even when the module is define. The error message was

NIntegrate::nlim: "z1 = 1/r is not a valid limit of integration. " 

Is it possible to achieve the above goal in a correct and elegant way? Many thanks in advance.

gamebm
  • 215
  • 1
  • 7
  • You certainly can do rStar[r_?NumericQ] := (* stuff *) to mitigate your problem, but it seems to me you could do better by using NDSolve[] with WhenEvent[] if it's really the root you are interested in and not the function itself. – J. M.'s missing motivation Jun 19 '16 at 16:27
  • Welcome to Mathematica.SE! I suggest the following: 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2) Take the tour! 3) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! – Michael E2 Jun 19 '16 at 16:57
  • For more on ?NumericQ, see this answer. – Michael E2 Jun 19 '16 at 16:59
  • This answer shows a variety of approaches, including the use of NDSolve to compute the inverse of a function. – Michael E2 Jun 19 '16 at 17:07
  • Many thanks. ?NumericQ seems to work! Concerning NDSolve, I read the post and thought about it, still I don't get it. How to get the inverse function by solving a differential equation? I tried google but did not get much information. Are there other post with a simple example? – gamebm Jun 19 '16 at 17:34
  • I see, NDSolve the derivative of rStar equals to the inverse of the derivative of inverse function. In my case, probably it wont work, since the original function rStar contains up to 16th higher order polinomial as well as operations such as square root, so I guess the computational time is not affordable. I will give it a try anyway. – gamebm Jun 19 '16 at 17:54
  • I tried to use NDSolve, but then encountered the problem of infinity. After some tentative guess, am not able to get through. I think it is a quite different situation now, so I started a new question. – gamebm Jun 20 '16 at 01:22

0 Answers0