Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

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 ?

share|improve this question

1 Answer 1

up vote 7 down vote accepted

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?

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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