0

Can I get some help with this:

 P[f_, {x_, xmin_, xmax_}] := 
     Block[{ x1 = xmin, x2 = xmax}, f = Sin[2x];
      NIntegrate[f[x], {x,x1,x2}]]
    Quiet[Table[P[x], {x1, 0.02, 0.06, .01},{x2, 0.02, 0.06, .01}]]

I want to solve the above integral using Block. This is a single integral after knowing the right syntax, I want to generalize the function so that it can evaluate double and triple integrals. Any help please

  • 2
    Why are you doing f = Sin[2x] in the Block, when f is already an argument of the function P ? It would make more sense to do: P[f_, {xmin_, xmax_}] := NIntegrate[f[x], {x, xmin, xmax}]; g[x_]:=Sin[2 x]; Quiet[Table[ P[g, {x1, x2}], {x1, 0.02, 0.06, .01}, {x2, 0.02, 0.06, .01}]] – flinty Sep 30 '20 at 15:57
  • @flinty How can I generalize your method to solve triple integral? – user74940 Sep 30 '20 at 16:37
  • 1
    NIntegrate already supports higher dimensional integration. For example g[x_, y_, z_] := Cos[x*y*z]; NIntegrate[g[x, y, z], {x, y, z} \[Element] Ball[]] so I don't know why you need to generalize it. You should probably have a look at this question: https://mathematica.stackexchange.com/questions/21622/syntax-for-integrating-over-limits-specified-by-a-table?rq=1 – flinty Sep 30 '20 at 16:54

1 Answers1

1

It is not clear what are you trying to do. Here is a modified version of your code that works:

Clear[P];
P[f_, {x_, xmin_, xmax_}] :=
  Block[{x1 = xmin, x2 = xmax, f2},
   NIntegrate[f, {x, x1, x2}]
  ];

Table[ P[Sin[2 x], {x, x1, x2}], {x1, 0.02, 0.06, .01}, {x2, 0.02, 0.06, .01}]

(* {{0., 0.000499783, 0.0011992, 0.00209797, 0.00319574}, {-0.000499783, 0., 0.000699417, 0.00159819, 0.00269595}, {-0.0011992, -0.000699417, 0., 0.000898771, 0.00199654}, {-0.00209797, -0.00159819, -0.000898771, 0., 0.00109776}, {-0.00319574, -0.00269595, -0.00199654, -0.00109776, 0.}} *)

(Voting to close.)

Anton Antonov
  • 37,787
  • 3
  • 100
  • 178