# A function created by Integrate which can be plotted only with certain argument names; NIntegrate works better, but

Here's the function:

ps = Exp[-#] &;
int[s_?NumericQ, z_?NumericQ] := (s (z - s + Log[s]))^(-1);
j[z_?NumericQ] := Integrate[int[x, z], {x, ps[z], z}];
Plot[Evaluate[j[z]], {z, 1/3, 1/2}]


Evaluating does not work

j[.5]


Plotting three more times with different argument names leads to two failures and a success on Version 11.3 (it seems to work only with an argument which has not been used before)

Plot[Evaluate[j[x]], {x, 1/3, 1/2}]
Plot[Evaluate[j[s]], {s, 1/3, 1/2}]
Plot[Evaluate[j[y]], {y, 1/3, 1/2}]

• This is an issue coming from using Integrate when you wanted NIntegrate. The former tries to do symbolic integration and can sometimes fallback to numerical stuff. Using NIntegrate fixes your issue. As is, for me, when I use the version with Integrate I only run into issues when using the x version. The issue there is that x in the symbolic integral gets replaced by a numerical value and Mathematica thinks there are no variables over which to integrate. Mar 12, 2021 at 20:54
• You can see that Integrate replaces the integration variable x with a numerical value by specifying: j[z_?NumericQ] := (Print[{z, ps[z]}]; Integrate[int[x, z], {x, ps[z], z}]); I would call this a bug that should be reported. Mar 12, 2021 at 21:16

There are two issues: (1) j[] depends on a global variable x, which makes it vulnerable to x having a numeric value instead of being a symbol; (2) Integrate is an exact solver, which means it won't evaluate to a number it cannot compute exactly.

The first issue is solved by localizing the variable x (e.g. Module[{x},...]).

The second issue can be solved by using N[] when appropriate, which I show; or by being satisfied with an approximation by NIntegrate, which @m_goldberg shows in his answer.

ps = Exp[-#] &;
int[s_?NumericQ, z_?NumericQ] := (s (z - s + Log[s]))^(-1);
j[z_?NumericQ] :=
Module[{x}, N[Integrate[int[x, z], {x, ps[z], z}], Precision[z]]];
Plot[Evaluate[j[z]], {z, 1/3, 1/2}]

• Thanks @Michael E2. I had not realized that x in "Integrate[int[x, z]" is a global variable in need to be encapsulated. An important lesson :) Now for "N[Integrate[int[x, z], {x, ps[z], z}], Precision[z]]]" , is this equivalent to NIntegrate? Mar 14, 2021 at 15:53
• @florin No, N[Integrate[..], prec] is not exactly equivalent to NIntegrate, although it will use NIntegrate. It will attempt to return a result that is accurate to prec digits. For example, z1 = 250/5; j[z1] aims to have at least 50 digits because Precision[z1] is 50. N[] does this (as I understand it) by adapting the precision of NIntegrate to achieve the goal indicated by prec. (The docs say "attempt," so it might fail to achieve the goal in troublesome cases.) Use NIntegrate as in the other answers if speed and only moderate accuracy are needed. Mar 14, 2021 at 16:57
• I chose N[Integrate[..]] to show an alternative to NIntegrate, since only NIntegrate is shown in the other answers. Mar 14, 2021 at 16:58

Try this:

Plot[Evaluate[NIntegrate[x/(z - x + Log[x]), {x, Exp[-z], z}]], {z,
1/3, 1/2}] // Quiet


with the effect of

Have fun!

• I think you have the integrand wrong. I think it should be 1/x/(z - x + Log[x]). Mar 13, 2021 at 2:48

Using NItegrate in place of Itegrate seems to work.

ps = Exp[-#] &;
int[s_?NumericQ, z_?NumericQ] := 1/(s (z - s + Log[s]))
j[z_?NumericQ] := NIntegrate[int[x, z], {x, ps[z], z}]
Plot[j[x], {x, 1/3, 1/2}]
`