0
$\begingroup$

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 ?

$\endgroup$
4
  • 1
    $\begingroup$ The result of the integration is a constant independent of x,y ? $\endgroup$ Aug 26, 2012 at 17:25
  • 1
    $\begingroup$ What you have there is a constant... $\endgroup$ Aug 26, 2012 at 17:25
  • $\begingroup$ Do you mean something like With[{int = Integrate[x^2 + y^2, {y, 6, 10}, {x, 0, t}]}, Plot[int, {t, 0, 10}]]? $\endgroup$
    – rm -rf
    Aug 26, 2012 at 17:27
  • $\begingroup$ Exactly !!! Thanks man! Trying that @R.M $\endgroup$
    – Mush
    Aug 26, 2012 at 18:11

3 Answers 3

3
$\begingroup$

I 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 :

enter image description here

$\endgroup$
8
$\begingroup$

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.

$\endgroup$
4
  • $\begingroup$ I assume you meant NIntegrate, not Integrate in the definition of myFunction? $\endgroup$
    – rcollyer
    Jun 6, 2013 at 18:47
  • $\begingroup$ yes. Sorry about that. $\endgroup$
    – Searke
    Jun 7, 2013 at 19:12
  • $\begingroup$ Please see my answer. Perhaps it is better, if you agree that 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$
    – Michael E2
    Aug 5, 2015 at 11:53
  • $\begingroup$ I think NumericQ still really should be used in this case. The function could easily be used in other contexts and it's behavior should be consistent. I've updated the answer. $\endgroup$
    – Searke
    Aug 5, 2015 at 15:17
2
$\begingroup$

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

Mathematica graphics

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:

Mathematica graphics

$\endgroup$

Your Answer

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

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