3
$\begingroup$

I have a list of expressions which I would like to integrate by nested integration. For any element from the list it works without issues (see end of the post), however, when I apply nested integration to the whole list I get an error. Can someone help me to understand what is matter and how to fix it ? I would like to continue to work with the list of functions as with the solid object.

Code:

ListExpressions = {x + y + z, 2 x + y + z, 3 x + y + z, 4 x + y + z, 5 x + y + z};
f[x_?NumericQ, y_?NumericQ, z_?NumericQ] := Evaluate[ListExpressions];
f2[K1_?NumericQ, y_?NumericQ, z_?NumericQ] := NIntegrate[f[x, y, z], {x, 0, K1}];
f3[K1_?NumericQ, K2_?NumericQ, K3_?NumericQ] := NIntegrate[f2[K1, y, z], {y, 0, K2}, {z, 0, K3}];

f2[1, 1, 10]
f3[1, 1, 10]

Output:

During evaluation of In[19]:= NIntegrate::inum: Integrand f[x,1,10] is not numerical at {x} = {0.00795732}.
During evaluation of In[19]:= NIntegrate::inum: Integrand f[x,1,10] is not numerical at {x} = {0.00795732}.
Out[23]= NIntegrate[f[x, 1, 10], {x, 0, 1}]
During evaluation of In[19]:= NIntegrate::inum: Integrand f[x,1,10] is not numerical at {x} = {0.00795732}.
During evaluation of In[19]:= NIntegrate::inum: Integrand f[x,1,10] is not numerical at {x} = {0.00795732}.
During evaluation of In[19]:= NIntegrate::inum: Integrand f[x,1,10] is not numerical at {x} = {0.00795732}.
During evaluation of In[19]:= General::stop: Further output of NIntegrate::inum will be suppressed during this calculation.
During evaluation of In[19]:= NIntegrate::inumr: The integrand f2[1,y,z] has evaluated to non-numerical values for all sampling points in the region with boundaries {{0,1},{0,10}}.
Out[24]= NIntegrate[f2[1, y, z], {y, 0, 1}, {z, 0, 10}]

Everything is working fine when calculating a single element from a list of functions. One can use one of the lines, both return correct result for a given element after substitution to the code above:

IndexInList=3; (*can be 1, 2, 4, 5 as well*)
f[x_?NumericQ, y_?NumericQ, z_?NumericQ] := Evaluate[ListExpressions[[IndexInList]]];
(*OR*)
f2[K1_?NumericQ, y_?NumericQ, z_?NumericQ] := NIntegrate[f[x, y, z][[IndexInList]], {x, 0, K1}];

*UPD 13.05.24 I tried to follow @LouisB's suggestion and I can conclude that the output for his approach gives vague values for f3. The results do not coincide with the one obtained for a single function from the list. Therefore, I would aware those who might resort to it, since the sequence of pure function application is not clear. Below, is the code for single functions:

ClearAll["Global`*"]
ListExpressions = 1 x + y + z; (*OR N x + y + z*)
f[x_?NumericQ, y_?NumericQ, z_?NumericQ] := Evaluate[ListExpressions]
f2[K1_?NumericQ, y_?NumericQ, z_?NumericQ] := NIntegrate[f[x, y, z], {x, 0, K1}];
f3[K1_?NumericQ, K2_?NumericQ, K3_?NumericQ] := NIntegrate[f2[K1, y, z], {y, 0, K2}, {z, 0, K3}];

f2[1, 1, 10]
f3[1, 1, 10]

(*11.5
60.*)
$\endgroup$
4
  • $\begingroup$ These are newbie errors, such as using upper-case letters to name functions or lists. Also Evaluate[x + y + z] makes no sense. $\endgroup$ Commented May 12 at 20:46
  • $\begingroup$ I see, thank you. Do you have a solution to my issue ? I will be glad. $\endgroup$
    – seva011
    Commented May 12 at 20:50
  • $\begingroup$ They are not always numeric. This works f[x_, y_, z_] := {x + y + z, 2 x + y + z, 3 x + y + z, 4 x + y + z, 5 x + y + z}; f2[K1_?NumericQ, y_?NumericQ, z_?NumericQ] := NIntegrate[f[x, y, z], {x, 0, K1}]; f2[1, 1, 10] $\endgroup$
    – yarchik
    Commented May 12 at 21:09
  • $\begingroup$ @yarchik Thank you. Your suggestion works only for f2 for f3 it returns NIntegrate::inumr: The integrand x+y+z has evaluated to non-numerical values for all sampling points in the region with boundaries {{0,1}}. Even when rid off ?NumericQ in f2 declaration, i.e. f2[K1_, y_, z_] . I cannot accept the proposal. "They are not always numeric" I do not agree, there is no symbolic calculations, and hence inputs have to be numeric only. $\endgroup$
    – seva011
    Commented May 12 at 21:25

1 Answer 1

1
$\begingroup$

The intermediate solution I use is based on indexing, I did not find another option:

ClearAll["Global`*"]
ListExpressions = {x + y + z, 2 x + y + z, 3 x + y + z, 4 x + y + z, 5 x + y + z};
f[x_?NumericQ, y_?NumericQ, z_?NumericQ, KK_?NumericQ] := Evaluate[Indexed[ListExpressions, KK]]
f2[K1_?NumericQ, y_?NumericQ, z_?NumericQ, KK_?NumericQ] := NIntegrate[f[x, y, z, KK], {x, 0, K1}];
f3[K1_?NumericQ, K2_?NumericQ, K3_?NumericQ, KK_?NumericQ] := NIntegrate[f2[K1, y, z, KK], {y, 0, K2}, {z, 0, K3}];
f3[1,1,10,#]&/@Range[5]

(*{60.,65.,70.,75.,80.}*)
```
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.