Two Maxima Functions for Riemann Sums

Two early attempts at programming Maxima functions.  I’d love to hear your comments about how to make these work better.  Many thanks to the online community from whom I learned how to get this far!

The script is given at the bottom of this post.  Copy and paste into a file (in your working directory) called Riemann.mac, and then load into Maxima using

load(“Riemann.mac”);

Riemann

/* Two Maxima functions for introductory integral calculus
(c) 2016, themaximalist.org  */

/* RiemannSum
fn is the function to be integrated on the interval [a,b]
n rectangles
opt specifies:
0: left endpoints
1: midpoints
2: right endpoints
*/

RiemannSum(fn,a,b,n,opt):=
block([xx,s],
xx(i,n):= a+(i+opt/2)*(b-a)/n,
s: sum(ev(fn,x=xx(i,n)),i,0,n-1)*(b-a)/n,
float(s)

)$

/* RiemannRectangles to Draw rectangles
fn is the function to be integrated:
on the interval [a,b]
n rectangles
opt specifies:
0: left endpoints
1: midpoints
2: right endpoints
*/

load(draw)%

RiemannRectangles(fn,a,b,n,opt):=
block([xi,xx,rects,wxd],
xi(i,n):= a+i*(b-a)/n,
xx(i,n):= a+(i+opt/2)*(b-a)/n,
rects(n):=makelist(rectangle([xi(k,n),0], [xi(k+1,n),ev(fn,x=xx(k,n))]),k,0,n-1),
wxd(n):=apply(wxdraw2d,
append([xrange=[a-(b-a)/4, b+(b-a)/4],color=blue],
rects(n),
[transparent=true, explicit(fn,x,a, b)]
)
),
wxd(n)
)$

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s