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.
rStar[r_?NumericQ] := (* stuff *)to mitigate your problem, but it seems to me you could do better by usingNDSolve[]withWhenEvent[]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?NumericQ, see this answer. – Michael E2 Jun 19 '16 at 16:59NDSolveto compute the inverse of a function. – Michael E2 Jun 19 '16 at 17:07NDSolve, 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:34NDSolve, 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