Group Abstract Group Abstract

Message Boards Message Boards

0
|
2K Views
|
5 Replies
|
3 Total Likes
View groups...
Share
Share this post:

Why does nested Integration works for a single function but not for a list of functions ?

Posted 1 year ago

I try to apply nested integration to lists of functions. The code works fine for a single function (not a list), but returns error when trying to calculate the same expression with a list of functions.

A function f[x,args...] is simplified preliminary by defining argument x (let say X={1,2,3,4}) and I obtain a list of functions for given x's. Next step, I define integral functions f2[args...] and f3[args...] and try to compute a nested integration with the list, desiring to obtain a list of result for all x's. May it be done without use of 'Indexed' or calling a part of the function list in general f[[i]] ? I would like to send a list into Nintegrates and obtain list of outputs.

Currently my solution is the following, but I like to avoid use of indexes:

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]

Out= {60.,65.,70.,75.,80.}
POSTED BY: Aka Kopejkin
5 Replies
POSTED BY: Michael Rogers
Posted 1 year ago

Michael, thank you for the comprehensive answer.

POSTED BY: Aka Kopejkin
Posted 1 year ago

I tried to boil your example down to simpler situation. Here is an input that does not work:

Clear[g2, g3];
g2[y_?NumericQ] := NIntegrate[{1}, {x, 0, 1}];
g3[K1_?NumericQ] := NIntegrate[g2[y], {y, 0, K1}];
g3[1]

The following variation does work:

Clear[g2, g3];
g2[y_] := NIntegrate[{1}, {x, 0, 1}];
g3[K1_?NumericQ] := NIntegrate[g2[y], {y, 0, K1}];
g3[1]

and also this

Clear[g3];
g3[K1_?NumericQ] :=
  NIntegrate[NIntegrate[{1}, {x, 0, 1}],
   {y, 0, K1}];
g3[1]

I have no explanation.

POSTED BY: Updating Name

This works:

ListExpressions = {x + y + z, 2  x + y + z, 3  x + y + z, 
   4  x + y + z, 5  x + y + z};
g[x_?NumericQ, y_?NumericQ, z_?NumericQ, KK_Integer] = 
  Quiet@ListExpressions[[KK]];
g2[K1_?NumericQ, y_?NumericQ, z_?NumericQ, KK_?NumericQ] := 
  NIntegrate[g[x, y, z, KK], {x, 0, K1}];
g3[K1_?NumericQ, K2_?NumericQ, K3_?NumericQ, KK_?NumericQ] := 
  NIntegrate[g2[K1, y, z, KK], {y, 0, K2}, {z, 0, K3}];
g3[1, 1, 10, #] & /@ Range[5]
POSTED BY: Gianluca Gorni
Posted 1 year ago

Thank you ! My fault, I only mentioned 'Indexed' but in fact I assume: "May it be done without use of 'Indexed' or calling a part of the function list in general f[[i]] ? I would like to send a list to Nintegrates and obtain list of outputs." The post was corrected correspondingly.

POSTED BY: Aka Kopejkin
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard