Re: An Argument Problem

*To*: mathgroup at smc.vnet.net*Subject*: [mg91770] Re: An Argument Problem*From*: Peter Breitfeld <phbrf at t-online.de>*Date*: Sun, 7 Sep 2008 05:39:32 -0400 (EDT)*Organization*: SFZ Bad Saulgau*References*: <g9t6pp$jbm$1@smc.vnet.net>

Mr Ajit Sen schrieb: > Dear Mathgroup, > > I am trying to numerically integrate a function of several variables w.r.t. its last variable. As NIntegrate won't work, I decided to use Simpson's 1/3 rule. Below is my attempt for a function of 2 variables (using Mathematica 6.0): > > Simpson[F_, a_, b_, n_?EvenQ] := Module[{h, evens, odds}, > h = (b - a)/n; > evens = Table[a + i h, {i, 2, n - 2, 2}]; > odds = Table[a + i h, {i, 1, n - 1, 2}]; > h/3 {F[x, a], F[x, b], 4 F[x, #] & /@ odds, > 2 F[x, #] & /@ evens} // Flatten // Total] // Simplify // N > > This works OK & returns a function of x. My query is how to make Simpson give a function of whatever the fist argument of F is. Thus, if F= F[r,t]= > , > Simpson would integrate w.r.t. t & give a function of r (without my having to change the x's into r's). Basically, I can't suss out how to pass the first argument of F inside the module. > > Thanks for any help. > > Regards. > Ajit > Maybe you could write: Simpson[F_, p_List, a_, b_, n_?EvenQ] := Module[{h, evens, odds, f}, f = F[Sequence @@ Most[p], #] &; h = (b - a)/n; evens = a + h Range[2, n - 2, 2]; odds = a + h Range[1, n - 1, 2]; h/3 {f[a], f[b], 4 (f[#] & /@ odds), 2 (f[#] & /@ evens)} // Flatten // Total ] // Simplify // N F should be given as a named or pure function, p is the List of parameters f expects Example: f[a_, b_, x_] := a (x - 5)^2 + 3 b x^3 Simpson[f, {a, b, x}, -2, 3, 6] Out=111.667 a + 48.75 b This should be exact, beeing a cubic: In[292]:= Integrate[f[a, b, x], {x, -2, 3.}] Out[292]= 111.667 a + 48.75 b Gruss Peter -- ==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-==-== Peter Breitfeld, Bad Saulgau, Germany -- http://www.pBreitfeld.de