Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemath
GitHub Repository: sagemath/sagemath.github.io
Path: blob/master/eval/novoseltsev.json
2457 views
1
{
2
"Region of Integration Plotter" : {
3
"cat" : [ "Calculus" , "Multivariate" ],
4
"descr" : "Plotting regions of integration for iterated triple integrals in different coordinate systems.",
5
"code" : [
6
"# Limits of the left integral, first bottom, then top.",
7
"bot0d = \"1\"",
8
"top0d = \"3\"",
9
"# Limits of the middle integral.",
10
"bot1d = \"1\"",
11
"top1d = \"3\"",
12
"# Limits of the right integral.",
13
"bot2d = \"1\"",
14
"top2d = \"3\"",
15
"# Function to integrate.",
16
"fd = \"1\"",
17
"# Order of integration (must match one of the strings in the drop-down list).",
18
"dVd = \"dz dy dx\"",
19
"# Transparency control: 0 completely clear, 1 completely opaque.",
20
"opacity = 0.75",
21
"",
22
"#########################################",
23
"# Copyright (c) 2012 Andrey Novoseltsev #",
24
"#########################################",
25
"",
26
"html(r\"\"\"",
27
"<ul>",
28
"<li>Consider unchecking <b>Update plot</b> checkbox on the bottom while you are setting up the integral.</li>",
29
"<li>On the plot <span style=\"color:red\">red parts</span> correspond to points where limits of the <span style=\"color:red\">red integral</span> are reached, etc.</li>",
30
"<li>Try setting equal limits on the internal integral in cylindrical and spherical coordinates to see \"planes parallel to coordinate ones.\"</li>",
31
"</ul>",
32
"\"\"\")",
33
"",
34
"def dlist(vs):",
35
" vs = vs.split()",
36
" return [\" \".join(\"d%s\" % v for v in a) for a in Arrangements(vs, len(vs))]",
37
"",
38
"@interact(layout={",
39
"\"top\": [",
40
"[\"top0\", \"top1\", \"top2\"],",
41
"[\"int0\", \"int1\", \"int2\", \"f\", \"dV\"],",
42
"[\"bot0\", \"bot1\", \"bot2\"],",
43
"],",
44
"\"bottom\": [",
45
"[\"plot_axes\", \"plot_origin\", \"update_plot\", \"compute\"],",
46
"],",
47
"})",
48
"def _(",
49
" top0=input_box(label=\"\", default=top0d, type=str, width=5),",
50
" int0=text_control(r\"\"\"<div class=\"math\">\\color{red}{\\int}</div>\"\"\"),",
51
" bot0=input_box(label=\"\", default=bot0d, type=str, width=5),",
52
" top1=input_box(label=\"\", default=top1d, type=str, width=10),",
53
" int1=text_control(r\"\"\"<div class=\"math\">\\color{green}{\\int}</div>\"\"\"),",
54
" bot1=input_box(label=\"\", default=bot1d, type=str, width=10),",
55
" top2=input_box(label=\"\", default=top2d, type=str, width=15),",
56
" int2=text_control(r\"\"\"<div class=\"math\">\\color{blue}{\\int}</div>\"\"\"),",
57
" bot2=input_box(label=\"\", default=bot2d, type=str, width=15),",
58
" f=input_box(label=\"\", default=fd, type=str, width=40),",
59
" dV=selector(dlist(\"x y z\") + dlist(\"z r theta\") + dlist(\"rho phi theta\"), label=\"\", default=dVd),",
60
" plot_axes=checkbox(label=\"Axes:\", default=True),",
61
" plot_origin=checkbox(label=\" Origin:\", default=True),",
62
" update_plot=checkbox(label=\" Update plot:\", default=True),",
63
" compute=checkbox(label=\" Try to compute:\", default=False),",
64
" ):",
65
" vs = [d[1:] for d in dV.split()]",
66
" vs.reverse()",
67
" V = [SR(v) for v in vs]",
68
" v0, v1, v2 = (SR.var(v) for v in vs)",
69
" limits = [eval(\"[v%d, SR(bot%d), SR(top%d)]\" % (i, i, i)) for i in range(3)]",
70
" f(v0, v1, v2) = SR(f)",
71
"",
72
" def to_xyz(*V):",
73
" if \"x\" in vs:",
74
" return [V[vs.index(v)] for v in \"xyz\"]",
75
" elif \"r\" in vs:",
76
" r, theta, z = [V[vs.index(v)] for v in [\"r\", \"theta\", \"z\"]]",
77
" return [r*cos(theta), r*sin(theta), z]",
78
" else:",
79
" rho, phi, theta = [V[vs.index(v)] for v in [\"rho\", \"phi\", \"theta\"]]",
80
" return [rho*sin(phi)*cos(theta), rho*sin(phi)*sin(theta), rho*cos(phi)]",
81
"",
82
" u = SR.var(\"u\")",
83
" v = SR.var(\"v\")",
84
" p = Graphics()",
85
" V1 = (1-u) * limits[1][1] + u * limits[1][2]",
86
" for i in [1,2]:",
87
" V2 = limits[2][i].subs({v0: v0, v1: V1})",
88
" p += parametric_plot3d(to_xyz(v0, V1, V2), limits[0], (u, 0, 1), opacity=opacity, color=\"blue\")",
89
" for i in [1,2]:",
90
" V1 = limits[1][i]",
91
" V2 = (1-u) * limits[2][1].subs({v1: V1}) + u * limits[2][2].subs({v1: V1})",
92
" p += parametric_plot3d(to_xyz(v0, V1, V2), limits[0], (u, 0, 1), opacity=opacity, color=\"green\", boundary_style={\"color\": \"green\", \"thickness\": 5})",
93
" for i in [1,2]:",
94
" V0 = limits[0][i]",
95
" V1 = (1-u) * limits[1][1].subs({v0: V0}) + u * limits[1][2].subs({v0: V0})",
96
" V2 = (1-v) * limits[2][1].subs({v0: V0, v1: V1}) + v * limits[2][2].subs({v0: V0, v1: V1})",
97
" p += parametric_plot3d(to_xyz(V0, V1, V2), (u, 0, 1), (v, 0, 1), opacity=opacity, color=\"red\", boundary_style={\"color\": \"red\", \"thickness\": 10})",
98
" if plot_origin:",
99
" p += point3d((0, 0, 0), size=25, color=\"yellow\")",
100
" if plot_axes:",
101
" bb = p.bounding_box()",
102
" for i, v in enumerate(\"xyz\"):",
103
" start = min(bb[0][i], 0)",
104
" end = max(bb[1][i], 0)",
105
" length = end - start",
106
" p += line([[0]*i+[start-0.08*length]+[0]*(2-i), [0]*i+[end+0.08*length]+[0]*(2-i)], thickness=3, color=\"brown\")",
107
" p += text3d(v, [0]*i+[end+0.1*length]+[0]*(2-i))",
108
" if update_plot:",
109
" p.show(frame=False, aspect_ratio=1)",
110
" if compute:",
111
" try:",
112
" I2 = integral(f, *(limits[2]))",
113
" I1 = integral(I2, *(limits[1]))",
114
" I0 = integral(I1, *(limits[0]))",
115
" html(r\"\\[ \\iiint_D %s\\, dV = %s \\approx %s \\]\" % tuple(map(latex, [f(v0, v1, v2), I0(0,0,0), I0.n()])))",
116
" except:",
117
" html(r\"\\[ \\iiint_D %s\\, dV = \\dots ? \\]\" % latex(f(v0, v1, v2)))"
118
]
119
}
120
}
121
122
123