If I have a multivariable integration like
NIntegrate[x^2 + y^2, {x, 1, 5}, {y, 6, 10}]
But I need to plot its result in terms of x
. Then how to do it ?
Mathematica Stack Exchange is a question and answer site for users of Wolfram Mathematica. It only takes a minute to sign up.
Sign up to join this communityI think this would be your answer :
data = Integrate[x^2 + y^2, {x, 0, t}, {y, 6, 10}];
Plot[data, {t, 0, 6}]
and this would be your Graphic :
DSaad's answer above is correct. You can use symbolic integration to get the formula. The solution when using numeric integration (NIntegrate
instead of Integrate
) can be more complicated. You have to be careful about the order of evaluation. In earlier versions of Mathematica, you had to use ?NumericQ
to delay the evaluation of the numeric integral:
myFunction[t_?NumericQ]:= NIntegrate[x^2 + y^2, {x, 0, t}, {y, 6, 10}];
Plot[myFunction[t], {t, 0, 6}]
To see why ?NumericQ
is needed here, please see this (original version). Using ?NumericQ
is a still a good idea. It helps your function to behave consistently and makes your code more predictable.
NIntegrate
, not Integrate
in the definition of myFunction
?
$\endgroup$
NumericQ
is no longer necessary, that you update your answer and I delete mine. I think it would make this Q&A more coherent.
$\endgroup$
Aug 5, 2015 at 11:53
[Background: I added this answer because this question was cited in What are the most common pitfalls awaiting new users? as an example of when NumericQ
was required. I took the integrand to be a simple example of a more general problem with NIntegrate
, but on reading the comments, it seems to be narrowly the exact integrand of interest to the OP. I recall a time when plotting functions would complain about numeric issues, but I could not test this specific case. It was also intended to offset Searke's answer, which I take was necessary back in '12. But perhaps this so well known that this answer is of little value, and I should remove the part of the pitfalls answer that implies NumericQ
is useful in such cases as this and delete this answer.]
As of at least V9 and after, you do not need to protect the function with NumericQ
when using Plot
:
Plot[NIntegrate[x^2 + y^2, {x, 0, t}, {y, 6, 10}], {t, 0, 6}]
Aside: This is true for ParametricPlot
in V9, too, but not in V10.2.
In V10.2, you get a single warning message:
ParametricPlot[{t, NIntegrate[x^2 + y^2, {x, 0, t}, {y, 6, 10}]}, {t, 0, 6}, AspectRatio -> 0.6]
NIntegrate::nlim: x = t is not a valid limit of integration. >>
But the plot is generated correctly:
x,y
? $\endgroup$With[{int = Integrate[x^2 + y^2, {y, 6, 10}, {x, 0, t}]}, Plot[int, {t, 0, 10}]]
? $\endgroup$