222 - Topic 15.2 - Triple Integral Interact

1491 days ago by Professor222

# Triple Integral Interact for VOLUME only. Draws the nice region in cartesian, # cylindrical, and spherical with differential terms in various orders. def dlist(vs): vs = vs.split() return [" ".join("d%s" % v for v in a) for a in Arrangements(vs, len(vs))] var('x,y,z') @interact(layout={ "top": [ ["top0", "top1", "top2", "spacer1"], ["int0", "int1", "int2", "dV", ], ["bot0", "bot1", "bot2","auto_update"], ], "bottom": [ ["plot_axes", "plot_origin", "opacity"], ], }) def _( top0=input_box(label="", default=3, width=3), int0=text_control(r"""<div class="math">\color{red}{\int}</div>"""), bot0=input_box(label="", default=1, width=3), top1=input_box(label="", default=3, width=5), int1=text_control(r"""<div class="math">\color{green}{\int}</div>"""), bot1=input_box(label="", default=1, width=5), top2=input_box(label="", default=3, width=7), spacer1 = text_control(" "), int2=text_control(r"""<div class="math">\color{blue}{\int}</div>"""), bot2=input_box(label="", default=1, width=7), dV=selector(flatten(map(dlist, ["x y z", "r theta z", "rho theta phi"])), label="", default="dz dy dx"), opacity=slider(range(101), label="Opacity:", default=75), plot_axes=checkbox(label=" Axes:", default=True), plot_origin=checkbox(label=" Origin:", default=True), auto_update=False ): f = 1+0*x opacity /= 100 vs = [d[1:] for d in dV.split()] vs.reverse() V = map(SR, vs) v0, v1, v2 = (SR.var(v) for v in vs) # While ln = log, symbolically they are treated differently limits = [eval("[v%d, SR(str(bot%d).replace('ln', 'log')), SR(str(top%d).replace('ln', 'log'))]" % (i, i, i)) for i in range(3)] f = SR(str(f).replace('ln', 'log')) if true: integrals = [] differentials = [] for v, a, b in limits: integrals.append(r"\int_{%s}^{%s} " % (latex(a), latex(b))) differentials.insert(0, r"\, \mathrm{d} %s" % latex(v)) try: assume(a < v) assume(v < b) except ValueError: pass expression = [r"$"] #expression = [r"\("] integrand = f try: for i in [3, 2, 1]: expression.extend(integrals[:i]) if integrand != 1+0*x: expression.append(latex(integrand)) expression.extend(differentials[-i:]) expression.append("\\\\\hspace{1cm}=") v, a, b = limits[i - 1] try: integrand = integral(integrand, v, a, b) except: integrand = integral(integrand, v) integrand = (integrand.subs({v: b}) - integrand.subs({v: a})).simplify_full() # Don't add these at once in case of exceptions from n(). expression.extend([latex(integrand), r"\\\\\hspace{1cm}\approx"]) expression.append(latex(integrand.n())) except: expression.append(r"\dots ?") forget() expression.append(r"$") pretty_print(html("<p><h3>The integral is evaluated as follows:</h3>")) pretty_print(html(" ".join(expression))) pretty_print(html("<br/></p>")) if "x" in vs: def to_xyz(*V): return [V[vs.index(v)] for v in "xyz"] elif "r" in vs: def to_xyz(*V): r, theta, z = [V[vs.index(v)] for v in ["r", "theta", "z"]] return [r*cos(theta), r*sin(theta), z] else: def to_xyz(*V): rho, phi, theta = [V[vs.index(v)] for v in ["rho", "phi", "theta"]] return [rho*sin(phi)*cos(theta), rho*sin(phi)*sin(theta), rho*cos(phi)] try: u = SR.var("u") v = SR.var("v") p = Graphics() V1 = (1-u) * limits[1][1] + u * limits[1][2] for i in [1,2]: V2 = limits[2][i].subs({v0: v0, v1: V1}) p += parametric_plot3d(to_xyz(v0, V1, V2), limits[0], (u, 0, 1), opacity=opacity, color="blue") for i in [1,2]: V1 = limits[1][i] V2 = (1-u) * limits[2][1].subs({v1: V1}) + u * limits[2][2].subs({v1: V1}) p += parametric_plot3d(to_xyz(v0, V1, V2), limits[0], (u, 0, 1), opacity=opacity, color="green", boundary_style={"color": "green", "thickness": 5}) for i in [1,2]: V0 = limits[0][i] V1 = (1-u) * limits[1][1].subs({v0: V0}) + u * limits[1][2].subs({v0: V0}) V2 = (1-v) * limits[2][1].subs({v0: V0, v1: V1}) + v * limits[2][2].subs({v0: V0, v1: V1}) p += parametric_plot3d(to_xyz(V0, V1, V2), (u, 0, 1), (v, 0, 1), opacity=opacity, color="red", boundary_style={"color": "red", "thickness": 10}) if plot_origin: p += point3d((0, 0, 0), size=25, color="yellow") if plot_axes: bb = p.bounding_box() for i, v in enumerate("XYZ"): start = min(bb[0][i], 0) end = max(bb[1][i], 0) length = end - start p += line([[0]*i+[start-0.08*length]+[0]*(2-i), [0]*i+[end+0.08*length]+[0]*(2-i)], thickness=3, color="brown") p += text3d(v, [0]*i+[end+0.1*length]+[0]*(2-i)) p.show(frame=False, aspect_ratio=1) except ValueError: pretty_print(html(r"""<span style="color:red">Cannot draw the plot, is the integral valid?..</span>""")) 
       

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

# Triple Integral Interact for general w=f(x,y,z). Draws the nice region in cartesian, # cylindrical, and spherical with differential terms in various orders. Note that if # f(x,y,z) = 1, then this is simply the volume of the region R. If not, this has no nice pic. def dlist(vs): vs = vs.split() return [" ".join("d%s" % v for v in a) for a in Arrangements(vs, len(vs))] var('x,y,z') @interact(layout={ "top": [ ["top0", "top1", "top2"], ["int0", "int1", "int2", "f", "dV", "auto_update"], ["bot0", "bot1", "bot2"], ], "bottom": [ ["plot_axes", "plot_origin"],[ "compute", "opacity"], ], }) def _( top0=input_box(label="", default=3, width=4), int0=text_control(r"""<div class="math">\color{red}{\int}</div>"""), bot0=input_box(label="", default=1, width=4), top1=input_box(label="", default=3, width=8), int1=text_control(r"""<div class="math">\color{green}{\int}</div>"""), bot1=input_box(label="", default=1, width=8), top2=input_box(label="", default=3, width=12), int2=text_control(r"""<div class="math">\color{blue}{\int}</div>"""), bot2=input_box(label="", default=1, width=12), f=input_box(label="", default="1", width=30), dV=selector(flatten(map(dlist, ["x y z", "r theta z", "rho theta phi"])), label="", default="dz dy dx"), opacity=slider(range(101), label="Opacity:", default=75), plot_axes=checkbox(label=" Axes:", default=True), plot_origin=checkbox(label=" Origin:", default=True), compute=checkbox(label=" Try to compute:", default=False), auto_update=False ): opacity /= 100 vs = [d[1:] for d in dV.split()] vs.reverse() V = map(SR, vs) v0, v1, v2 = (SR.var(v) for v in vs) # While ln = log, symbolically they are treated differently limits = [eval("[v%d, SR(str(bot%d).replace('ln', 'log')), SR(str(top%d).replace('ln', 'log'))]" % (i, i, i)) for i in range(3)] f = SR(str(f).replace('ln', 'log')) if compute: integrals = [] differentials = [] for v, a, b in limits: integrals.append(r"\int_{%s}^{%s} " % (latex(a), latex(b))) differentials.insert(0, r"\, d %s" % latex(v)) try: assume(a < v) assume(v < b) except ValueError: pass expression = [r"\["] integrand = f try: for i in [3, 2, 1]: expression.extend(integrals[:i]) expression.append(latex(integrand)) expression.extend(differentials[-i:]) expression.append("=") v, a, b = limits[i - 1] try: integrand = integral(integrand, v, a, b) except: integrand = integral(integrand, v) integrand = (integrand.subs({v: b}) - integrand.subs({v: a})).simplify_full() # Don't add these at once in case of exceptions from n(). expression.extend([latex(integrand), r"\approx"]) expression.append(latex(integrand.n())) except: expression.append(r"\dots ?") forget() expression.append(r"\]") pretty_print(html(" ".join(expression))) if "x" in vs: def to_xyz(*V): return [V[vs.index(v)] for v in "xyz"] elif "r" in vs: def to_xyz(*V): r, theta, z = [V[vs.index(v)] for v in ["r", "theta", "z"]] return [r*cos(theta), r*sin(theta), z] else: def to_xyz(*V): rho, phi, theta = [V[vs.index(v)] for v in ["rho", "phi", "theta"]] return [rho*sin(phi)*cos(theta), rho*sin(phi)*sin(theta), rho*cos(phi)] try: u = SR.var("u") v = SR.var("v") p = Graphics() V1 = (1-u) * limits[1][1] + u * limits[1][2] for i in [1,2]: V2 = limits[2][i].subs({v0: v0, v1: V1}) p += parametric_plot3d(to_xyz(v0, V1, V2), limits[0], (u, 0, 1), opacity=opacity, color="blue") for i in [1,2]: V1 = limits[1][i] V2 = (1-u) * limits[2][1].subs({v1: V1}) + u * limits[2][2].subs({v1: V1}) p += parametric_plot3d(to_xyz(v0, V1, V2), limits[0], (u, 0, 1), opacity=opacity, color="green", boundary_style={"color": "green", "thickness": 5}) for i in [1,2]: V0 = limits[0][i] V1 = (1-u) * limits[1][1].subs({v0: V0}) + u * limits[1][2].subs({v0: V0}) V2 = (1-v) * limits[2][1].subs({v0: V0, v1: V1}) + v * limits[2][2].subs({v0: V0, v1: V1}) p += parametric_plot3d(to_xyz(V0, V1, V2), (u, 0, 1), (v, 0, 1), opacity=opacity, color="red", boundary_style={"color": "red", "thickness": 10}) if plot_origin: p += point3d((0, 0, 0), size=25, color="yellow") if plot_axes: bb = p.bounding_box() for i, v in enumerate("XYZ"): start = min(bb[0][i], 0) end = max(bb[1][i], 0) length = end - start p += line([[0]*i+[start-0.08*length]+[0]*(2-i), [0]*i+[end+0.08*length]+[0]*(2-i)], thickness=3, color="brown") p += text3d(v, [0]*i+[end+0.1*length]+[0]*(2-i)) p.show(frame=False, aspect_ratio=1) except ValueError: html(r"""<span style="color:red">Cannot draw the plot, is the integral valid?..</span>""") 
       

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

# Don't know why I have this one in here twice. Wonder what I was up to? def dlist(vs): vs = vs.split() return [" ".join("d%s" % v for v in a) for a in Arrangements(vs, len(vs))] var('x,y,z') @interact(layout={ "top": [ ["top0", "top1", "top2", "spacer1"], ["int0", "int1", "int2","f", "dV", ], ["bot0", "bot1", "bot2","auto_update"], ], "bottom": [ ["plot_axes", "plot_origin", "opacity"], ], }) def _( top0=input_box(label="", default=3, width=3), int0=text_control(r"""<div class="math">\color{red}{\int}</div>"""), bot0=input_box(label="", default=1, width=3), top1=input_box(label="", default=3, width=5), int1=text_control(r"""<div class="math">\color{green}{\int}</div>"""), bot1=input_box(label="", default=1, width=5), top2=input_box(label="", default=3, width=7), f=input_box(label="", default=3, width=7), spacer1 = text_control(" "), int2=text_control(r"""<div class="math">\color{blue}{\int}</div>"""), bot2=input_box(label="", default=1, width=7), dV=selector(flatten(map(dlist, ["x y z", "r theta z", "rho theta phi"])), label="", default="dz dy dx"), opacity=slider(range(101), label="Opacity:", default=75), plot_axes=checkbox(label=" Axes:", default=True), plot_origin=checkbox(label=" Origin:", default=True), auto_update=False ): f += 0*x opacity /= 100 vs = [d[1:] for d in dV.split()] vs.reverse() V = map(SR, vs) v0, v1, v2 = (SR.var(v) for v in vs) # While ln = log, symbolically they are treated differently limits = [eval("[v%d, SR(str(bot%d).replace('ln', 'log')), SR(str(top%d).replace('ln', 'log'))]" % (i, i, i)) for i in range(3)] f = SR(str(f).replace('ln', 'log')) if true: integrals = [] differentials = [] for v, a, b in limits: integrals.append(r"\int_{%s}^{%s} " % (latex(a), latex(b))) differentials.insert(0, r"\, \mathrm{d} %s" % latex(v)) try: assume(a < v) assume(v < b) except ValueError: pass expression = [r"$"] #expression = [r"\("] integrand = f try: for i in [3, 2, 1]: expression.extend(integrals[:i]) if integrand != 1+0*x: expression.append(latex(integrand)) expression.extend(differentials[-i:]) expression.append("\\\\\hspace{1cm}=") v, a, b = limits[i - 1] try: integrand = integral(integrand, v, a, b) except: integrand = integral(integrand, v) integrand = (integrand.subs({v: b}) - integrand.subs({v: a})).simplify_full() # Don't add these at once in case of exceptions from n(). expression.extend([latex(integrand), r"\\\\\hspace{1cm}\approx"]) expression.append(latex(integrand.n())) except: expression.append(r"\dots ?") forget() expression.append(r"$") html("<p><h3>The integral is evaluated as follows:</h3>") html(" ".join(expression)) html("<br/></p>") if "x" in vs: def to_xyz(*V): return [V[vs.index(v)] for v in "xyz"] elif "r" in vs: def to_xyz(*V): r, theta, z = [V[vs.index(v)] for v in ["r", "theta", "z"]] return [r*cos(theta), r*sin(theta), z] else: def to_xyz(*V): rho, phi, theta = [V[vs.index(v)] for v in ["rho", "phi", "theta"]] return [rho*sin(phi)*cos(theta), rho*sin(phi)*sin(theta), rho*cos(phi)] try: u = SR.var("u") v = SR.var("v") p = Graphics() V1 = (1-u) * limits[1][1] + u * limits[1][2] for i in [1,2]: V2 = limits[2][i].subs({v0: v0, v1: V1}) p += parametric_plot3d(to_xyz(v0, V1, V2), limits[0], (u, 0, 1), opacity=opacity, color="blue") for i in [1,2]: V1 = limits[1][i] V2 = (1-u) * limits[2][1].subs({v1: V1}) + u * limits[2][2].subs({v1: V1}) p += parametric_plot3d(to_xyz(v0, V1, V2), limits[0], (u, 0, 1), opacity=opacity, color="green", boundary_style={"color": "green", "thickness": 5}) for i in [1,2]: V0 = limits[0][i] V1 = (1-u) * limits[1][1].subs({v0: V0}) + u * limits[1][2].subs({v0: V0}) V2 = (1-v) * limits[2][1].subs({v0: V0, v1: V1}) + v * limits[2][2].subs({v0: V0, v1: V1}) p += parametric_plot3d(to_xyz(V0, V1, V2), (u, 0, 1), (v, 0, 1), opacity=opacity, color="red", boundary_style={"color": "red", "thickness": 10}) if plot_origin: p += point3d((0, 0, 0), size=25, color="yellow") if plot_axes: bb = p.bounding_box() for i, v in enumerate("XYZ"): start = min(bb[0][i], 0) end = max(bb[1][i], 0) length = end - start p += line([[0]*i+[start-0.08*length]+[0]*(2-i), [0]*i+[end+0.08*length]+[0]*(2-i)], thickness=3, color="brown") p += text3d(v, [0]*i+[end+0.1*length]+[0]*(2-i)) p.show(frame=False, aspect_ratio=1) except ValueError: html(r"""<span style="color:red">Cannot draw the plot, is the integral valid?..</span>""") 
       

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