Chapter Eight of "Maxima By Example"




On Wed, 29 Jul 2009, Edwin Woollett wrote:

< 
< > (%i1) 
< > quad_qags(lambda([x],quad_qags(lambda([y],exp(x-y^2)),y,1,2+x)[1]),x,0,1);
< > (%o1)          [.2384683615524653, 2.647530656401834e-15, 21, 0]
< >
< >
< > I have an interest in determining knot energies, and double integrals
< > arise naturally in these calculations. My tests show that for double
< > integrals quad_qags usually provides a better estimate than romberg.
< >
< > Regards,
< > James M Rotenberry
< ----------------------------------
 
Nice. Here is a short function that fixes what I think are two holes in
maxima: first, one cannot easily switch from symbolic to numeric
integration; second, multiple integrals are a pain. 

(%i2) nintegrate(_method,_F,intervals) := block(
      [interval,f,method],
      f : _F,
      if _method#'integrate then method :
buildq([__method:_method],lambda([_f,_x,_a,_b],__method(_f,_x,_a,_b,'epsrel=1e-9)[1]))
      else method : _method,
      for interval in intervals do (
        f : 'apply(method,[f,interval[1],interval[2],interval[3]])
        ),
      ev(f,nouns))$
(%i3) nintegrate('integrate,x*z,[[x,0,y],[y,0,1],[z,0,1]]);
(%o3) 1/12
(%i4) nintegrate('quad_qags,x*z,[[x,0,y],[y,0,1],[z,0,1]]);
(%o4) .08333333333333334


Argument checking and error handling ought to be added.

Leo

-- 
The University of Edinburgh is a charitable body, registered in
Scotland, with registration number SC005336.