# Problem solving a function using FindRoot and then plotting it

When I run the following code:

xi = -20.; xf = 40.;

stepinter = 0.01;

m = 1/2;

vlogk0s = Re[ParallelTable[
vks :=
y /. FindRoot[x == y + 2 m Log[-2 m + y], {y, 2 m + 0.01},
WorkingPrecision -> 50, MaxIterations -> 10000];
{x, vks}, {x, xi, xf, stepinter}]]; // AbsoluteTiming
rts = Interpolation[vlogk0s];

Plot[rts[x], {x, xi, xf}]


I get the desired plot.

Now I will simply change the function inside the FindRoot as:

xi = -20.; xf = 40.;

stepinter = 0.01;

m = 1/2; \[CapitalLambda] = 0.6;

vlogk0sds = Re[ParallelTable[
vk :=
y /. FindRoot[
x == -RootSum[
6 m - 3 #1 + \[CapitalLambda] #1^3 &, (
Log[y - #1] #1)/(-1 + \[CapitalLambda] #1^2) &], {y,
2 m + 0.01}, WorkingPrecision -> 50, MaxIterations -> 10000];
{x, vk}, {x, xi, xf, stepinter}]]; // AbsoluteTiming
rtsds = Interpolation[vlogk0sds];

Plot[rtsds[x], {x, xi, xf}]


and the plot becomes messed.

I already tried different methods in both FindRoot and Interpolation, I tried different precision, iterations and everything. I know that the plot must be similar to the first one.

PS: In case it is needed, the function inside the FindRoot may be obtained via:

Integrate[(1 - (2 m)/r - \[CapitalLambda]/3 r^2)^-1, r]


As input, which yields:

-RootSum[6 m - 3 #1 + \[CapitalLambda] #1^3 &, (Log[r - #1] #1)/(-1 + \[CapitalLambda] #1^2) &]

• “ the plot becomes messed” - how exactly? Do you get errors? Warnings? Include an image if it’s not easy to describe. – MarcoB Jun 29 at 13:52
• I added some pictures, to both cases. – Edison Santos Jun 29 at 14:53
• You might want to ponder the RHS of the equation in FindRoot: Plot[-RootSum[ 6 m - 3 #1 + \[CapitalLambda] #1^3 &, (Log[ y - #1] #1)/(-1 + \[CapitalLambda] #1^2) &] // ReIm // Evaluate, {y, -10, 10}] – Michael E2 Jun 29 at 17:59
• What do you mean by reconsidering it? – Edison Santos Jun 30 at 11:52

What Michael mentioned in comments was the fact that, When calculating vlogk0sds, you are looking for values of $$y$$ for which your RootSum expression is equal to values in the $$(-20,40)$$ range. However, the following plot of the values of your RootSum expression shows that, where the expression is real, its value is never negative, and never greater than roughly 12:

Plot[
Evaluate@
ReIm@
-RootSum[6 m - 3 #1 + Λ #1^3 &, (Log[y - #1] #1)/(-1 + Λ #1^2) &],
{y, -10, 20},
PlotRange -> All, PlotLegends -> {"Re", "Im"},
Exclusions -> None
]


For instance, there are no values of $$y$$ for which this expression is equal to $$-20$$, so the results of FindRoot for that value won't be any good. In fact, they won't be much good for most of the $$x$$ values you considered!

If you restrict yourself to values for which solutions exist, then the following works quickly, even without parallel evaluation:

stepinter = 0.01;
m = 1/2; Λ = 0.6;

vlogk0sds = Chop@
Table[
{x, y} /.
FindRoot[
x == -RootSum[6 m - 3 #1 + Λ #1^3 &, (Log[y - #1] #1)/(-1 + Λ #1^2) &],
{y, 2 m + 0.01}, AccuracyGoal -> 5
],
{x, 0.01, 12, stepinter}
];

ListLogPlot[vlogk0sds, PlotRange -> All, Joined -> True]


Note that:

1. I use a log plot to emphasize the shape of the curve;
2. it is better to use Chop rather than Re to remove zero or near-zero imaginary parts resulting from numerical computation so, if the imaginary parts become significant, you are alerted to it;
3. you do not need to interpolate~, then plot: you can plot a list of data points directly using the ListPlot family of functions.
• It worked flawlessly, thank you. However, since I am not using interpolation anymore, how do I save it as a function with variable x? Something like func[x]? – Edison Santos Jul 3 at 11:48
• @EdisonSantos You can still use an interpolation for that, just like you did before, if you need it for other purposes. I only wanted to point out that you do not need it just for plotting. – MarcoB Jul 3 at 13:42