Mathematica®: performing a varying number of multiple integrals

Click For Summary

Discussion Overview

The discussion revolves around performing numerical integration in Mathematica® for a function with a varying number of variables. Participants explore methods to handle integration when the number of variables is not fixed, particularly focusing on the use of loops and specific Mathematica functions.

Discussion Character

  • Technical explanation
  • Exploratory
  • Mathematical reasoning

Main Points Raised

  • One participant seeks a method to numerically integrate a function of k variables in Mathematica®, noting that their integrand is complex and not analytically integrable.
  • Another participant suggests using NIntegrate with multiple variables in a single step, referencing the Mathematica documentation.
  • A participant expresses concern about the complexity of the original problem and the use of For loops, suggesting that such approaches rarely yield successful outcomes.
  • In response, the original poster shares that they found a solution using the Sequence and Apply commands, successfully implementing their integration for k variables.

Areas of Agreement / Disagreement

There is no explicit consensus on the best approach, but participants share different methods and experiences. The original poster ultimately finds a solution that works for them, while others express skepticism about the effectiveness of certain programming constructs.

Contextual Notes

The discussion includes varying levels of understanding regarding Mathematica® functions and programming practices, with some participants indicating that the problem's complexity may hinder clear communication.

pumpf
Messages
3
Reaction score
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
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?
 
My problem is that in the non-simplified problem the number of variables is k, with k varying in a for cycle.
 
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:
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.
 

Similar threads

  • · Replies 13 ·
Replies
13
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
3
Views
3K