Re: An Argument Problem
- To: mathgroup at smc.vnet.net
- Subject: [mg91728] Re: An Argument Problem
- From: David Bailey <dave at Remove_Thisdbailey.co.uk>
- Date: Sun, 7 Sep 2008 05:31:31 -0400 (EDT)
- References: <g9t6pp$jbm$1@smc.vnet.net>
Mr Ajit Sen wrote: > 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 > As written, your code accepts a function (probably typically a pure function) as its first argument. I think it would be easier to re-code Simpson to accept an expression as its first argument, and supply an extra argument to specify the integration variable. (Note that this variable should not have a value!) Simpson[F_,a_,b_,n_?EvenQ,x_Symbol]:=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 In[4]:= Simpson[Sin[u]Cos[t],0,2,2,t] Out[4]= 0.915021 Sin[u] Note that the above code could be simplified considerably, but I have made minimal changes to your function. David Bailey http://www.dbaileyconsultancy.co.uk