# 222 - Topic 23 - Line Integral 3D Vector Field

## 1016 days ago by admin

Computing the line integral of a vector field in 3 dimensions.

John Travis

Mississippi College

%hide %auto # This routine is a copy of the built-in function "plot_vector_field3d" but where # the autoscaling feature has been disabled so that vectors are displayed using their # actual length def plot_vector_field3d_unscaled(vec, xrange, yrange, zrange, plot_points=[5,5,5], colors='jet', center_arrows=False,**kwds): from matplotlib.cm import get_cmap from matplotlib.colors import ListedColormap from sage.plot.misc import setup_for_eval_on_grid (ff,gg,hh), ranges = setup_for_eval_on_grid(vec, [xrange, yrange, zrange], plot_points) xpoints, ypoints, zpoints = [srange(*r, include_endpoint=True) for r in ranges] points = [vector((i,j,k)) for i in xpoints for j in ypoints for k in zpoints] vectors = [vector((ff(*point), gg(*point), hh(*point))) for point in points] try: cm=get_cmap(colors) assert(cm is not None) except: if not isinstance(colors, (list, tuple)): colors=[colors] cm=ListedColormap(colors) max_len = max(v.norm() for v in vectors) max_len = 1 scaled_vectors = [v/max_len for v in vectors] if center_arrows: return sum([plot(v,color=cm(v.norm()),**kwds).translate(p-v/2) for v,p in zip(scaled_vectors, points)]) else: return sum([plot(v,color=cm(v.norm()),**kwds).translate(p) for v,p in zip(scaled_vectors, points)])
%auto ## This worksheet interactively computes and displays the line integral of a 3D vector field ## over a given smooth curve C ## ## John Travis ## Mississippi College ## 06/16/11 ## var('x,y,z,t,s') @interact(layout=dict(top=[['M','u'],['N','v'],['P','w'],['tt']], bottom=[['in_3d','spinning'],['xx'],['yy'],['zz']])) def _(M=input_box(default=x*y*z,label="$M(x,y,z)$",width=30), N=input_box(default=-y*z,label="$N(x,y,z)$",width=30), P=input_box(default=z*y,label="$P(x,y,z)$",width=30), u=input_box(default=cos(t),label="$x=u(t)$",width=30), v=input_box(default=2*sin(t),label="$y=v(t)$",width=30), w=input_box(default=t*(t-2*pi)/pi,label="$z=w(t)$",width=30), tt = range_slider(-4*pi, 4*pi, pi/6, default=(0,2*pi), label='t Range'), xx = range_slider(-5, 5, 1, default=(-1,1), label='x Range'), yy = range_slider(-5, 5, 1, default=(-2,2), label='y Range'), zz = range_slider(-5, 5, 1, default=(-3,1), label='z Range'), in_3d=checkbox(true,label='3D'), spinning=checkbox(true,label='Spin On')): # setup the parts and then compute the line integral dr = [derivative(u,t),derivative(v,t),derivative(w,t)] A = (M(x=u,y=v,z=w)*dr[0] +N(x=u,y=v,z=w)*dr[1] +P(x=u,y=v,z=w)*dr[2]) global line_integral line_integral = integral(A(t=t),t,tt[0],tt[1]) pretty_print(html('$\int_{C} < %s'%str(latex(M))+',%s'%str(latex(N))+',%s'%str(latex(P))+' > dr =')) print pretty_print(html('$ \int_{%s}'%str(latex(tt[0]))+'^{%s}'%str(latex(tt[1]))+' [%s'%str(latex(A))+']dt = %s $'%latex(line_integral))) G = plot_vector_field3d((M,N,P),(x,xx[0],xx[1]),(y,yy[0],yy[1]),(z,zz[0],zz[1]),plot_points=6) G += parametric_plot3d([u,v,w],(t,tt[0],tt[1]),thickness='5',color='yellow') if spinning: do_spin=true else: do_spin=false if in_3d: show(G,stereo='redcyan',spin=do_spin) else: show(G,perspective_depth=true,spin=do_spin) $\int_{C} < x y z,-y z,y z > dr =$ $\int_{0}^{2 \, \pi} [\frac{2 \, {\left(2 \, \pi - t\right)} t \cos\left(t\right) \sin\left(t\right)^{2}}{\pi} + \frac{2 \, {\left(2 \, \pi - t\right)} t {\left(\frac{2 \, \pi - t}{\pi} - \frac{t}{\pi}\right)} \sin\left(t\right)}{\pi} + \frac{4 \, {\left(2 \, \pi - t\right)} t \cos\left(t\right) \sin\left(t\right)}{\pi}]dt = -\frac{16 \, {\left(\pi - 27\right)}}{9 \, \pi}$ ## Click to the left again to hide and once more to show the dynamic interactive window # Evaluate this cell to get a numerical approximation to the line integral evaluated above... line_integral.n() %auto ## This worksheet interactively computes and displays the line integral of a 3D vector field ## over a given smooth curve C ## ## John Travis ## Mississippi College ## 06/16/11 ## var('x,y,z,t,s') @interact(layout=dict(top=[['M','u'],['N','v'],['P','w'],['tt']], bottom=[['in_3d','spinning'],['xx'],['yy'],['zz']])) def _(M=input_box(default=x*y*z,label="$M(x,y,z)$",width=30), N=input_box(default=-y*z,label="$N(x,y,z)$",width=30), P=input_box(default=z*y,label="$P(x,y,z)$",width=30), u=input_box(default=cos(t),label="$x=u(t)$",width=30), v=input_box(default=2*sin(t),label="$y=v(t)$",width=30), w=input_box(default=t*(t-2*pi)/pi,label="$z=w(t)$",width=30), tt = range_slider(-4*pi, 4*pi, pi/6, default=(0,2*pi), label='t Range'), xx = range_slider(-5, 5, 1, default=(-1,1), label='x Range'), yy = range_slider(-5, 5, 1, default=(-2,2), label='y Range'), zz = range_slider(-5, 5, 1, default=(-3,1), label='z Range'), in_3d=checkbox(true,label='3D'), spinning=checkbox(true,label='Spin On')): # setup the parts and then compute the line integral dr = [derivative(u,t),derivative(v,t),derivative(w,t)] A = (M(x=u,y=v,z=w)*dr[0] +N(x=u,y=v,z=w)*dr[1] +P(x=u,y=v,z=w)*dr[2]) global line_integral line_integral = integral(A(t=t),t,tt[0],tt[1]) pretty_print(html('$ \int_{C} < %s'%str(latex(M))+',%s'%str(latex(N))+',%s'%str(latex(P))+' > dr =')) print pretty_print(html('$\int_{%s}'%str(latex(tt[0]))+'^{%s}'%str(latex(tt[1]))+' [%s'%str(latex(A))+']dt = %s$'%latex(line_integral))) G = plot_vector_field3d_unscaled((M,N,P),(x,xx[0],xx[1]),(y,yy[0],yy[1]),(z,zz[0],zz[1]),plot_points=6) G += parametric_plot3d([u,v,w],(t,tt[0],tt[1]),thickness='5',color='yellow') if spinning: do_spin=true else: do_spin=false if in_3d: show(G,stereo='redcyan',spin=do_spin) else: show(G,perspective_depth=true,spin=do_spin)

$M(x,y,z)$  $x=u(t)$
$N(x,y,z)$  $y=v(t)$
$P(x,y,z)$  $z=w(t)$
t Range
3D  Spin On
x Range
y Range
z Range

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