2
$\begingroup$

I have a similar problem as posted in Difference in Plot when using Evaluate vs when not using Evaluate, however due to a numerical evaluation I cannot use Evaluated->True as a Plot[] option. Her a small example which gives errors:

f[y_, a_] := NIntegrate[y^2 + x, {x, 0, a}];
Plot[Table[f[y, a], {a, 1, 3}], {y, 0, 3}, Evaluated -> True]

The desired output should be equal to

Plot[{f[y, 1],f[y, 2],f[y, 3]}, {y, 0, 3}, Evaluated -> True]

How can I fix this example ?

$\endgroup$

1 Answer 1

7
$\begingroup$

Note that the following works, but generates warning messages.

f[y_, a_] := NIntegrate[y^2 + x, {x, 0, a}];
Plot[Table[f[y, a], {a, 1, 3}], {y, 0, 3}, Evaluated -> True]

enter image description here

The issue is that Plot (which usually holds it's first argument until specific values of $x$ are plugged in) tries to evaluate the first argument symbolically (due to the Evaluated option) and that causes an error since the numerical integral fails with symbolic arguments. The standard way to get around this is to ensure that f does not evaluate until numerical values are plugged in, as follows:

Clear[f];
f[y_?NumericQ, a_?NumericQ] := NIntegrate[y^2 + x, {x, 0, a}];
Plot[Table[f[y, a], {a, 1, 3}], {y, 0, 3}, Evaluated -> True]

enter image description here

I guess I'm assuming that you know the integral can be evaluated in closed form using Integrate and this is all just an example?

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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