4
$\begingroup$

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}]
$\endgroup$
2
  • 3
    $\begingroup$ 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. $\endgroup$ – b3m2a1 Mar 12 at 20:54
  • 1
    $\begingroup$ 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. $\endgroup$ – Daniel Huber Mar 12 at 21:16
2
$\begingroup$

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}]
$\endgroup$
3
  • $\begingroup$ 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? $\endgroup$ – florin Mar 14 at 15:53
  • 1
    $\begingroup$ @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 = 2`50/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. $\endgroup$ – Michael E2 Mar 14 at 16:57
  • 1
    $\begingroup$ I chose N[Integrate[..]] to show an alternative to NIntegrate, since only NIntegrate is shown in the other answers. $\endgroup$ – Michael E2 Mar 14 at 16:58
2
$\begingroup$

Try this:

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

with the effect of

enter image description here

Have fun!

$\endgroup$
1
  • 2
    $\begingroup$ I think you have the integrand wrong. I think it should be 1/x/(z - x + Log[x]). $\endgroup$ – m_goldberg Mar 13 at 2:48
2
$\begingroup$

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}]

plot

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.