-1
$\begingroup$

Given a function of two variables, how can I define a 2nd function that is the integral of the 1st w.r.t. one of the variables?

I have a function $f(x,y)$ and I want to do the integration like $\int^1_{-1} dy f(x,y)$ to obtain a 2nd function, say, $g(x)$. But when I try to do this with NIntegrate, the mathematica reports that the "the integrand has evaluated to non-numerical values...". So what commander should I use to take such a form integration?

Let's take an example. Let $f(x,y)=x^2y+xy^2$, and I want to do the integration $\int^1_0 x^2y+xy^2$. I try to do this by

NIntegrate[x^2y + xy^2,{x, 0, 1}]

Mathematica now reports that

the integrand x^2y+xy^2 has evaluated to non-numerical values ...

Indeed, when x is taking a number, say, 0.5, $x^2y+xy^2=0.5y^2+0.25y$ and it is not a numerical values. But my question is, how to do the integration of this kind?

$\endgroup$
  • $\begingroup$ Nintegrate[] will not work if there are any hanging symbols in your integrand (it is an N*[] function, after all). You could define a function like fint[x_] := (* stuff *), tho. $\endgroup$ – J. M.'s discontentment Jul 14 '16 at 15:18
  • $\begingroup$ @J.M. I really thank you for your comment. But can you put it more clearly? I am sorry that I don't understand it very much. $\endgroup$ – Wein Eld Jul 14 '16 at 15:20
  • $\begingroup$ NIntegrate[Sin[x + y], {y, -1, 1}] will not work because of the y. Is that clearer? $\endgroup$ – J. M.'s discontentment Jul 14 '16 at 15:25
  • $\begingroup$ @J.M. Yes, I realized it. But I am not clear that then how to do the integration. What do you mean by defining a function fint[x_] := (* stuff *)? $\endgroup$ – Wein Eld Jul 14 '16 at 15:28
  • $\begingroup$ It will be useful if you give a concrete example of what you are doing. Otherwise people can only guess about what is going wrong. $\endgroup$ – Szabolcs Jul 14 '16 at 15:29
3
$\begingroup$

I think you want something like this. I am assuming in your actual application f will require numerical integration, even though in this example it doesn't.

f[x_, y_] := x^2 y + x y^2
g[y_?NumericQ] := NIntegrate[f[x, y], {x, 0, 1}]
Plot[g[y], {y, 0, 1}, AxesLabel -> {y, g[x]}]

plot

| improve this answer | |
$\endgroup$

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