# Integrating (mathematica)

• Mathematica
I have a function of two variables F[x_,y_] and I Would like to integrate over one variable only and get a function G[x] for example and work with it.
I want something like:
G[x_]:=NIntegrate[F[x,y],{y,0,\infty}]

But it doesn't work.

It probably depends on what your F is. NIntegrate is also going to demand that all variables have been assigned constant numeric values.

In[1]:= f[x_, y_] := 3 x^2/(y + 2)^2;
g[x_] := Integrate[f[x, y], {y, 0, Infinity}];
g[x]

Out[3]= (3 x^2)/2

Dale
Mentor
2020 Award
But it doesn't work.
Can you be more specific about what doesn't work. It seems to work for me.

f[x_, y_] := 3 x^2/(y + 2)^2;
G[x_] := NIntegrate[f[x, y], {y, 0, \[Infinity]}];
G[1]

This gives 1.5 as expected.

Well, my function is something like:
F[x_, y_] := Sum[(Tablev[[ i]] - Sqrt[(4 Pi*(43.040600502)*x* y^(3)/(Tablepc[[i ]]))*(-Tablepc[[i ]] / (Tablepc[[i ]] +y) + Log[((Tablepc[[ i ]] +y)/y)])])^2/(Tableerr[[
i]])^2, {i, 1, 61}]

And I need minimize a function:
G[y_] :=NIntegrate[F[x, y], {x, 0, \[Infinity]}]

NMinimize[{G[y], y>0},y]

Out:
NIntegrate::inumr: The integrand 1.36156 (3.9 -63.1823 Sqrt[Power[<<2>>] x Plus[<<2>>]])^2+0.260308 (9.47 -44.6766 Sqrt[Power[<<2>>] x Plus[<<2>>]])^2+0.179091 (13.62 -36.4783 Sqrt[Power[<<2>>] x Plus[<<2>>]])^2+0.21553 (16.31 -31.5911 Sqrt[Power[<<2>>] x Plus[<<2>>]])^2+<<44>>+0.16391 (46.36 -9.02604 Sqrt[Power[<<2>>] x Plus[<<2>>]])^2+0.158601 (46.89 -8.93532 Sqrt[Power[<<2>>] x Plus[<<2>>]])^2+<<11>>
has evaluated to non-numerical values for all sampling points in the region with boundaries {{\[Infinity],0.}}. >>

General stop: Further output of NIntegrate::inumr will be suppressed during this calculation. >>

NIntegrate slwcon: Numerical integration converging too slowly; suspect one of the following: singularity, value of the integration is 0, highly oscillatory integrand, or WorkingPrecision too small. >>

NIntegrate::ncvb: NIntegrate failed to converge to prescribed accuracy after 9 recursive bisections in x near {x} = {8.16907*10^224}. NIntegrate obtained 2.91649617631392115.954589770191005*^55895 and 2.91649617631392115.954589770191005*^55895 for the integral and error estimates. >>

Tablev, Tablepc, Tableerr are tables of numbers.

Dale
Mentor
2020 Award
It looks like the problem is F[x,y], not G[y] per se. Have you plotted F[x,y] for points in your region of interest? Is it well-behaved throughout? Does it oscillate or go to zero in some limit? Is it numerically stable in that region?

Note, if you integrate to infinity then it must be well-behaved for extremely large numbers. In particular, your F[x,y] seems to be losing numerical accuracy in the x > 10^224 range.

I have my graphic and i know wich is the region of interest but when i put this interest region i have the next problem:
G[y_] :=NIntegrate[F[x, y], {x, 0.05, 25 }]
Out:
NIntegrate::inumr: The integrand 43.39404528 E^(1/2 (<<99>>+<<12>>)) has evaluated to non-numerical values for all sampling points in the region with boundaries {{0.05000000000,0.05085937500}}. >>

I've tried changing the interval of integration but doing that i only change the interval {{0.05000000000,0.05085937500}}.

DrClaude
Mentor
Look at the complete output for F[x, y] and make sure that it is all numerical (except for x and y).

Look at the complete output for F[x, y] and make sure that it is all numerical (except for x and y).
I Checked and I'm sure that it's all numerical.

DrClaude
Mentor
I've looked in more details, and what you are trying to do is impossible with NIntegrate, which can only work with something completely numerical, and for some reason when you ask for
Code:
NMinimize[{G[y], y>0},y]
you would expect that it passes a numerical value y to G, which then can numerically integrate F and return the result, but it appears that this is not what Mathematica does. That said, I found that, at leat for a simple case, the correct result is obtained anyway:
Code:
In[10] = NMinimize[{NIntegrate[Exp[-x^2] y, {x, 0, \[Infinity]}], y > 0}, y]

NIntegrate::inumr: The integrand E^-x^2 y has evaluated to non-numerical values for all sampling points in the region with boundaries {{\[Infinity],0.}}. >>
NIntegrate::inumr: The integrand E^-x^2 y has evaluated to non-numerical values for all sampling points in the region with boundaries {{\[Infinity],0.}}. >>
NIntegrate::inumr: The integrand E^-x^2 y has evaluated to non-numerical values for all sampling points in the region with boundaries {{\[Infinity],0.}}. >>
General::stop: Further output of NIntegrate::inumr will be suppressed during this calculation. >>

Out[10] = {0., {y -> 0.}}