222 - Topic 16 - Midpoint Method in 3D

1725 days ago by Professor222

Right now, this worksheet does not work. Ignore till I can find out what has been depricated.

Copied from http://wiki.sagemath.org/interact/calculus

%auto # from http://wiki.sagemath.org/interact/calculus # by Marshall Hampton 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) sin,cos = math.sin,math.cos html("<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))) html("$$\int_{"+str(R16(y_start))+"}^{"+str(R16(y_end))+"} "+ "\int_{"+str(R16(x_start))+"}^{"+str(R16(x_end))+"} "+func+"\ dx \ dy$$") html('<p style="text-align: center;">Numerical approximation: ' + str(num_appro 
       
Traceback (most recent call last): Cuboid defined by corner points v1 and v2. File "", line 1, in <module> File "/tmp/tmpYNHlxu/___code___.py", line 41 ^ SyntaxError: invalid syntax
Traceback (most recent call last):        Cuboid defined by corner points v1 and v2.
  File "", line 1, in <module>
    
  File "/tmp/tmpYNHlxu/___code___.py", line 41
    
                                                                                       ^
SyntaxError: invalid syntax
x)+'</p>') p1 = plot3d(f,(x,x_start,x_end),(y,y_start,y_end)) (p1+sum(cubs)).show(fig_size=(12,18)) 
       
Traceback (click to the left of this block for traceback)
...
SyntaxError: invalid syntax
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "_sage_input_5.py", line 10, in <module>
    exec compile(u'open("___code___.py","w").write("# -*- coding: utf-8 -*-\\n" + _support_.preparse_worksheet_cell(base64.b64decode("eCkrJzwvcD4nKQogICAgcDEgPSBwbG90M2QoZiwoeCx4X3N0YXJ0LHhfZW5kKSwoeSx5X3N0YXJ0LHlfZW5kKSkKICAgIChwMStzdW0oY3VicykpLnNob3coZmlnX3NpemU9KDEyLDE4KSk="),globals())+"\\n"); execfile(os.path.abspath("___code___.py"))
  File "", line 1, in <module>
    
  File "/tmp/tmpFyzGeq/___code___.py", line 3
    x)+'</p>')
     ^
SyntaxError: invalid syntax
# Working on some changes to the user interface 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) sin,cos = math.sin,math.cos html("<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 = input_box(0,width=10,label='$x_0$'), x_end = input_box(10.1,width=10,label='$x_1$'), y_start = input_box(0.1,width=10,label='$y_0$'), y_end = input_box(10.1,width=10,label='$y_1$')): x_start=RR(x_start) x_end=RR(x_end) y_start=RR(y_start) y_end=RR(y_end) 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))) html("$$\int_{"+str(R16(y_start))+"}^{"+str(R16(y_end))+"} "+ "\int_{"+str(R16(x_start))+"}^{"+str(R16(x_end))+"} "+func+"\ dx \ dy$$") html('<p style="text-align: center;">Numerical approximation: ' + str(num_approx)+'</p>') p1 = plot3d(f,(x,x_start,x_end),(y,y_start,y_end)) show(p1+sum(cubs)) 
       
function of x and y 
x subdivisions 
y subdivisions 
$x_0$ 
$x_1$ 
$y_0$ 
$y_1$ 

Click to the left again to hide and once more to show the dynamic interactive window