Midpoint Rule Numerically Integrating Function 2 Variab

## Description

This interact illustrates numerical integration for a function of two variables using the midpoint rule. The user may change the function to be integrated, the number of x and y subdivisions, and the ranges of x and y to integrate over.

## Sage Cell

#### Code

```
from sage.plot.plot3d.platonic import index_face_set
def cuboid(v1,v2,**kwds):
"""
Cuboid defined by corner points v1 and v2.
"""
ptlist = []
for vi in (v1,v2):
for vj in (v1,v2):
for vk in (v1,v2):
ptlist.append([vi[0],vj[1],vk[2]])
f_incs = [[0, 2, 6, 4], [0, 1, 3, 2], [0, 1, 5, 4], [1, 3, 7, 5], [2, 3, 7, 6], [4, 5, 7, 6]]
if 'aspect_ratio' not in kwds:
kwds['aspect_ratio'] = [1,1,1]
return index_face_set(f_incs,ptlist,enclosed = True, **kwds)
var('x,y')
R16 = RealField(16)
npi = RDF(pi)
pretty_print(html(r"<h1>The midpoint rule for a function of two variables</h1>"))
@interact
def midpoint2d(func = input_box('y*sin(x)/x+sin(y)',type=str,label='function of x and y'), nx = slider(2,20,1,3,label='x subdivisions'), ny = slider(2,20,1,3,label='y subdivisions'), x_start = slider(-10,10,.1,0), x_end = slider(-10,10,.1,3*npi), y_start= slider(-10,10,.1,0), y_end= slider(-10,10,.1,3*npi)):
f = sage_eval('lambda x,y: ' + func)
delx = (x_end - x_start)/nx
dely = (y_end - y_start)/ny
xvals = [RDF(x_start + (i+1.0/2)*delx) for i in range(nx)]
yvals = [RDF(y_start + (i+1.0/2)*dely) for i in range(ny)]
num_approx = 0
cubs = []
darea = delx*dely
for xv in xvals:
for yv in yvals:
num_approx += f(xv,yv)*darea
cubs.append(cuboid([xv-delx/2,yv-dely/2,0],[xv+delx/2,yv+dely/2,f(xv,yv)], opacity = .5, rgbcolor = (1,0,0)))
pretty_print(html(r"$\int_{"+str(R16(y_start))+r"}^{"+str(R16(y_end))+r"} "+ r"\int_{"+str(R16(x_start))+r"}^{"+str(R16(x_end))+r"} "+latex(SR(func))+r"\ dx \ dy$"))
pretty_print(html(r'<p style="text-align: center;">Numerical approximation: ' + str(num_approx)+r'</p>'))
p1 = plot3d(f,(x,x_start,x_end),(y,y_start,y_end))
show(p1+sum(cubs))
```

## Options

none

## Tags

Primary Tags:

Secondary Tags:

A list of possible tags can be found at The WeBWorK Open Problem Library. For linear algebra tags see the Curated Courses Project.

## Related Cells

Any related cells go here. Provide a link to the page containing the information about the cell.

## Attribute

Permalink:

Author:

Date: 23 Jul 2020 21:03

Submitted by: Zane Corbiere