Can You Integrate a Function of Two Variables in Mathematica?

  • Context: Mathematica 
  • Thread starter Thread starter gaby287
  • Start date Start date
  • Tags Tags
    Integrating Mathematica
Click For Summary

Discussion Overview

The discussion revolves around integrating a function of two variables in Mathematica, specifically focusing on how to integrate over one variable to obtain a function of the other variable. Participants explore issues related to the numerical integration function NIntegrate and its requirements for successful execution.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant seeks to define a function G[x] by integrating F[x, y] over y but encounters issues with the implementation.
  • Another participant suggests that the success of NIntegrate may depend on the specific form of F and that all variables must be assigned constant numeric values.
  • A participant provides an example where integration works correctly, indicating that the issue may not be with the integration method itself.
  • One participant presents a complex function F[x, y] and describes difficulties in minimizing G[y] due to errors from NIntegrate, suggesting potential issues with the behavior of F in the integration limits.
  • Concerns are raised about the numerical stability and behavior of F[x, y] in the region of interest, particularly for large values of x.
  • Another participant emphasizes the need to ensure that F[x, y] is entirely numerical except for the variables x and y to avoid integration errors.
  • There is a suggestion that NIntegrate may not handle the integration as expected when used within NMinimize, leading to confusion about the expected numerical behavior.

Areas of Agreement / Disagreement

Participants express differing views on the nature of the issues encountered with NIntegrate, with some suggesting that the problem lies within the function F[x, y] while others believe the integration method itself is sound. The discussion remains unresolved regarding the specific cause of the integration failures.

Contextual Notes

Participants note limitations related to the numerical behavior of F[x, y], including potential singularities, oscillations, or loss of numerical accuracy in certain regions. There is also mention of the need for all components of F to be numerical for successful integration.

gaby287
Messages
14
Reaction score
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.
 
Physics news on Phys.org
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
 
gaby287 said:
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.916496176313921`15.954589770191005*^55895 and 2.916496176313921`15.954589770191005*^55895 for the integral and error estimates. >>
 
Tablev, Tablepc, Tableerr are tables of numbers.
 
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 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}}.
 
Look at the complete output for F[x, y] and make sure that it is all numerical (except for x and y).
 
DrClaude said:
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
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.}}
 

Similar threads

  • · Replies 13 ·
Replies
13
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 9 ·
Replies
9
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 4 ·
Replies
4
Views
4K
  • · Replies 1 ·
Replies
1
Views
2K