Calculating Integral in Mathematica

  • Context: Mathematica 
  • Thread starter Thread starter jimmy1
  • Start date Start date
  • Tags Tags
    Integral Mathematica
Click For Summary

Discussion Overview

The discussion revolves around the numerical evaluation of a double integral involving a multivariate Gaussian function in Mathematica. Participants explore methods to calculate the integral of a function defined by a nested integral, addressing issues related to integration limits and the lack of a closed form for the function.

Discussion Character

  • Technical explanation
  • Mathematical reasoning
  • Homework-related

Main Points Raised

  • One participant defines a function f(x) as a double integral of a multivariate Gaussian, noting the absence of a closed form solution.
  • Another suggests using NIntegrate to evaluate the integral numerically, specifying the limits as numerical constants.
  • A participant points out that due to the lack of a closed form for f(x), using NIntegrate directly results in an error indicating the integrand is not numerical.
  • There is a request for clarification on the definition of f(x1, x2) and an example of the Mathematica code to illustrate the problem.
  • A participant provides an example of f(x) using the PDF of a multivariate normal distribution and discusses the need to evaluate this for a continuous range of x values.
  • Another participant suggests a different order of integration in NIntegrate, claiming it provides a valid result.
  • One participant reiterates the definition of f(x1, x2) and shares code to define f as a function in Mathematica, indicating how to evaluate it over a range of x values using a table.
  • A final participant expresses gratitude for the suggestions and indicates progress in resolving their issue.

Areas of Agreement / Disagreement

Participants do not reach a consensus on a single method for evaluating the integral, as multiple approaches are discussed, and some methods are noted to produce errors while others appear to work.

Contextual Notes

Participants mention specific integration limits and the need for numerical evaluation, but there are unresolved issues regarding the correct setup of the integrals and the handling of variables in Mathematica.

Who May Find This Useful

This discussion may be useful for individuals working with numerical integration in Mathematica, particularly those dealing with multivariate Gaussian functions and nested integrals.

jimmy1
Messages
60
Reaction score
0
I have a function f(x) which is defined as

[tex]f(x) = \int_{x}^{c} \int_{x}^{c} f(x_1,x_2) dx_1 dx_2[/tex]

where c is a known constant and f(x1,x2) is a multivariate Gaussian. Unfortunetaly there is no closed form solution for f(x).
My problem is I want to numerically calculate

[tex]\int_{c_1}^{c} f(x) dx[/tex]

where again c_1, and c are known constants.
How do I numerically evaluate such an integral in Mathematica? I get errors every time saying "x is not a valid limit of integration".
Any ideas, how I would input the above into Mathematica to get a numerical solution?
 
Physics news on Phys.org
NIntegrate[ f[x], {x, c1, c} ]
where c1 and c are numerical values or constants (like Pi, 2*E, 33.401 etc).
 
Unfortunately f(x) has no closed form solution (so the expression for f(x) still has the symbols x1, x2) and thus evaluating "NIntegrate[ f[x], {x, c1, c} ]" just gives the error the "Integrand ... is not numerical at...".

I've also tried "NIntegrate[ f[x], {x1, x, c}, {x2, x, c}, {x, c1, c} ]", but this gives the error "x1 = x is not a valid limit of integration. "

I'm sure there's some way to do it, but just can't figure it out?
 
Can you post some code, in particular be a little more specific about f(x1, x2) ?
 
Basically, f(x1,x2) is a http://en.wikipedia.org/wiki/Multivariate_normal_distribution" . An example in Mathematica code would be:

f(x) = "Integrate[PDF[MultinormalDistribution[{5,6}, {{1,1}, {1,2}}], {x1,x2}], {x1,x,c},{x2,x,c}]"

For any particular value of x and c (c is a known constant) I can numerically evaluate the above expression, however, I want to evaluate the above expression for all possible values of x, where x is a countinous number, and hence I need to integrate the above expression for all possible values of x (say, x ranges from 0-10 in the above example)
 
Last edited by a moderator:
You are right that
Code:
NIntegrate[ f[x], {x1, x, c}, {x2, x, c}, {x, c1, c} ]
doesn't work, but I think that
Code:
NIntegrate[ f[x], {x, c1, c}, {x1, x, c}, {x2, x, c} ]
does. At least it gives an answer... apparently the integration is in a different order than you'd expect.

E.g.
Code:
NIntegrate[PDF[MultinormalDistribution[{5, 6}, {{1, 1}, {1, 2}}], {x1, x2}], {x, 5, 10}, {x1, x, 10}, {x2, x, 10}]

0.365427
 
jimmy1 said:
Basically, f(x1,x2) is a http://en.wikipedia.org/wiki/Multivariate_normal_distribution" . An example in Mathematica code would be:

f(x) = "Integrate[PDF[MultinormalDistribution[{5,6}, {{1,1}, {1,2}}], {x1,x2}], {x1,x,c},{x2,x,c}]"

For any particular value of x and c (c is a known constant) I can numerically evaluate the above expression, however, I want to evaluate the above expression for all possible values of x, where x is a countinous number, and hence I need to integrate the above expression for all possible values of x (say, x ranges from 0-10 in the above example)

Of course, make sure you load the package with the MultinormalDistribution:
Code:
Needs["MultivariateStatistics`"]

Then turn your code into a function:

Code:
f[x_, c_] :=  NIntegrate[PDF[MultinormalDistribution[{5, 6}, {{1, 1}, {1, 2}}], {x1, 
    x2}], {x1, x, c}, {x2, x, c}]

Now you say that you want to evaluate this repeatedly for different values of x, you could use a table:

Code:
Table[f[x,20],{x,1,10,0.1}]
 
Last edited by a moderator:
Cool, thanks guys! I've used your suggestions and I think I've got it now, just need to test the result a bit more. Anyway, cheers for the help!
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
4K
  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 13 ·
Replies
13
Views
3K
  • · Replies 1 ·
Replies
1
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
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K