Integrating (mathematica)

  • #1
gaby287
14
0
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.
 

Answers and Replies

  • #2
Bill Simpson
1,073
32
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
 
  • #3
33,666
11,236
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.
 
  • #4
gaby287
14
0
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.916496176313921`15.954589770191005*^55895 and 2.916496176313921`15.954589770191005*^55895 for the integral and error estimates. >>
 
  • #5
gaby287
14
0
Tablev, Tablepc, Tableerr are tables of numbers.
 
  • #6
33,666
11,236
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.
 
  • #7
gaby287
14
0
I have my graphic and i know which 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}}.
 
  • #8
DrClaude
Mentor
8,021
4,743
Look at the complete output for F[x, y] and make sure that it is all numerical (except for x and y).
 
  • #9
gaby287
14
0
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.
 
  • #10
DrClaude
Mentor
8,021
4,743
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.}}
 

Suggested for: Integrating (mathematica)

Replies
1
Views
411
Replies
1
Views
467
Replies
5
Views
848
Replies
6
Views
788
  • Last Post
Replies
1
Views
645
Replies
2
Views
480
Replies
3
Views
315
Replies
0
Views
358
Replies
0
Views
390
Replies
6
Views
272
Top