Mathematica®: performing a varying number of multiple integrals

In summary, you can use NIntegrate to numerically integrate a function of k variables with respect to all of them.
  • #1
pumpf
3
0
Hello everyone.
In Mathematica® I want to numerically integrate a function of k variables (k varies) with respect to all of them. Does anyone of you know a way to do that? I tried the following simplified example.

k = 5;
int[x_] := x[[1]] + x[[2]] + x[[3]] + x[[4]] + x[[5]] ; (* My integrand. Of course, together with a and b below, the true version will be defined in terms of k. *)
a = {1, 2, 3, 4, 5}; (* lower limits of integration *)
b = {2, 3, 4, 5, 6}; (* upper limits of integration *)
For[i = k, i >= 1, i--,
y = Table[x[j], {j, 1, i}];
int[x_] = Integrate[int[y], {x, a[], b[]}] /. {x[p_] -> x[[p]]};
]

int becomes now a constant function, which is what I wanted. My problem is that my initial integrand int[x_] is more complicated than the one written in the example and is not analytically integrable, so that I have to use numerical integration NIntegrate instead of Integrate. However, I cannot NIntegrate one variable at a time. Also using the definition with := (int[x_] := Integrate[...]) trying to perform just one numerical estimation at the end is not working. Any ideas to help me with this problem?

Lucio
 
Physics news on Phys.org
  • #2
It is very difficult to be certain I understand the problem you have tried to simplify.

This
http://reference.wolfram.com/mathematica/ref/NIntegrate.html
shows you can
NIntegrate[f,{x,x0,x1},{y,y0,y1},{z,z0,z1}...]

That will let you numerically integrate over all your variables in a single step.

Will that solve your problem?
 
  • #3
My problem is that in the non-simplified problem the number of variables is k, with k varying in a for cycle.
 
  • #4
Again and again people show up here with slight variations of "My problem is too complicated to understand or explain, I absolutely positively MUST use For to do it, but it doesn't work, what do I do?" That almost never turns out well.

v={{x,x0,x1},{y,y0,y1},{z,z0,z1}};
For[i=1,i<=3,i++,
Print[NIntegrate[f,Evaluate[Sequence@@Take[v,i]]]]
]

There is more going on inside that than can be briefly explained to a new user.

I don't believe this is going to solve your problem, but good luck.
 
Last edited:
  • #5
It did solve my problem, thanks! I didn't know the commands Sequence and @@ (Apply), which turned out to do what I wanted. I programmed the k-th step the following way:

y = Table[x, {i, 1, k}]; (* my k variables *)
a = Table[i, {i, 1, k}]; (* lower limits of integration *)
b = Table[i + 1, {i, 1, k}]; (* upper limits of integration *)
v = Table[{x, a[], b[]}, {i, 1, k}];
f[x_] := E^(Sum[-x[]^2, {i, 2, k - 1}] - 0.5 (x[[1]]^2 + x[[k]]^2)); (* my integrand *)
Print[NIntegrate[f[y], Evaluate[Sequence @@ v]]];

Thanks for your time and your suggestions.
 

1. How can I perform multiple integrals with varying numbers of variables in Mathematica®?

Mathematica® has a built-in function called Integrate that allows you to perform integrals with one or more variables. To perform a multiple integral with varying numbers of variables, you can use the syntax Integrate[expr, {x1, a1, b1}, {x2, a2, b2}, ...], where expr is the function you want to integrate and x1, x2, etc. are the variables. You can specify the lower and upper limits of integration with a1, b1, a2, b2, etc.

2. How do I handle nested integrals in Mathematica®?

To handle nested integrals in Mathematica®, you can use the same syntax as above, but include the inner integrals as functions in the expression. For example, if you have a nested integral of f(x,y) = x * y over the region 0 < x < 1 and 0 < y < x, you can use Integrate[x * Integrate[y, {y, 0, x}], {x, 0, 1}] in Mathematica®.

3. Can I perform a varying number of integrals over a region in Mathematica®?

Yes, you can use the function NIntegrate in Mathematica® to perform numerical integration over a region with a varying number of variables. The syntax is similar to Integrate, but you need to specify the method of integration and the number of integration points. For example, NIntegrate[x * y, {x, 0, 1}, {y, 0, x}, Method -> "AdaptiveMonteCarlo", MaxPoints -> 10000] will perform a numerical integration of f(x,y) = x * y over the region 0 < x < 1 and 0 < y < x using the Adaptive Monte Carlo method with a maximum of 10000 integration points.

4. Is there a way to simplify a multiple integral in Mathematica®?

Yes, Mathematica® has a function called Simplify that can be used to simplify expressions, including multiple integrals. You can use it after performing the integral to simplify the result. For example, Simplify[Integrate[x * y, {x, 0, 1}, {y, 0, x}]] will simplify the result of the integral f(x,y) = x * y over the region 0 < x < 1 and 0 < y < x.

5. What are some common mistakes to avoid when performing multiple integrals in Mathematica®?

Some common mistakes to avoid when performing multiple integrals in Mathematica® include not specifying the variables and their limits of integration correctly, not using proper syntax, and not simplifying the result. It is also important to check for any missing parentheses or brackets, as they can greatly affect the result of the integral. Additionally, it is important to double check that the function being integrated is spelled correctly and that all necessary parentheses and brackets are included in the function.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
13
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
127
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
7
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
3
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
Back
Top