simpson's rule area and volume integrals using buildq and define
Subject: simpson's rule area and volume integrals using buildq and define
From: Edwin Woollett
Date: Wed, 7 Dec 2011 14:21:23 -0800
On Dec. 7, 2011, I wrote
----------------------------
>Thanks to the examples of Robert Dodier and
>Barton Willis, I have written Simpson's one
>third rule code for area and volume integrals
>as a first step in improving the software associated
>with ch. 8, Maxima by Example: Numerical Integration.
-------------------------------------------
Looking again at the volume code, I realize that
although it seems to work, I don't understand one
aspect.
The symbols %%u, %%v, %%w should be bound
to the names of the three variables in the
furnished expression, and the %%v integral integrand is
G(%%u,%%v) =
integrate(ex(%%u,%%v,%%w),%%w,w1(%%u,%%v),w2(%%u,%%v)),
and the define statement uses dummy variables (us,vs) to pick out
a particular value of this quantity.
By mistake, I used the syntax
subst([u=us,v=vs] ,ex)
instead of
subst([%%u=us, %%v=vs],ex)
and the mistaken code seems to work.
------------------------------------------
first the code version that doesn't work
(why not??)
(%i2) volume(ex,uL,vL,wL,nL) :=
block([%%u,u1,u2,%%v,v1,v2,%%w,w1,w2,
nu,nv,nw,u0,us,vs,hh,gg],
local(hh,gg),
[%%u,u1,u2] : uL,
[%%v,v1,v2] : vL,
[%%w,w1,w2] : wL,
[nu,nv,nw] : nL,
define(gg(us,vs),
buildq([%%expr:subst([%%u=us,%%v=vs],ex),
%w1:subst([%%u=us,%%v=vs],w1),
%w2:subst([%%u=us,%%v=vs],w2) ],
simp1(%%expr,%%w,%w1,%w2,nw))),
define(hh(u0),
buildq([%gg:subst(u0,%%u,gg(u,v)),
%v1:subst(u0,%%u,v1),
%v2:subst(u0,%%u,v2)],
simp1(%gg,%%v,%v1,%v2,nv))),
simp1(hh(%uu),%uu,u1,u2,nu))$
(%i3) volume(x*y^2*z^3,[x,0,1],[y,0,1],[z,0,1],[4,4,4]);
(%o3) 0.25*u*v^2
------------------------------
and the not understood version that works:
(%i4) volume(ex,uL,vL,wL,nL) :=
block([%%u,u1,u2,%%v,v1,v2,%%w,w1,w2,
nu,nv,nw,u0,us,vs,hh,gg],
local(hh,gg),
[%%u,u1,u2] : uL,
[%%v,v1,v2] : vL,
[%%w,w1,w2] : wL,
[nu,nv,nw] : nL,
display(%%u,%%v,%%w),
define(gg(us,vs),
buildq([%%expr:subst([u=us,v=vs],ex),
%w1:subst([u=us,v=vs],w1),
%w2:subst([u=us,v=vs],w2) ],
simp1(%%expr,%%w,%w1,%w2,nw))),
define(hh(u0),
buildq([%gg:subst(u0,%%u,gg(u,v)),
%v1:subst(u0,%%u,v1),
%v2:subst(u0,%%u,v2)],
simp1(%gg,%%v,%v1,%v2,nv))),
simp1(hh(%uu),%uu,u1,u2,nu))$
(%i5) volume(x*y^2*z^3,[x,0,1],[y,0,1],[z,0,1],[4,4,4]);
%%u = x
%%v = y
%%w = z
(%o5) 0.041666666666667
--------------------------------------------
Ted Woollett