Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download

All published worksheets from http://sagenb.org

Views: 168738
Image: ubuntu2004

Vector fields arise naturally in the physical world, where most fall into two types, velocity fields and force fields.

Example of a 2D velocity field

Perhaps the following approximates two-dimensional fluid flow (an example of a velocity field) around an obstruction.  Sage plots the vectors (arrows) so that the length from tail to head is equal to the length of the vector.  Thus, longer arrows correspond to faster flow.

var('x,y,z,t') vf=plot_vector_field(( -y^2,x^2), (x,-3,3), (y,-3,3), aspect_ratio=1); C=parametric_plot([cos(t),sin(t)],[t,0,2*pi], rgbcolor = (1,0,0)); show(vf + C)
(x, y, z, t)

Example of a 2D gravitational (force) field

The previous cell suggests flow of any kind is associated with a velocity (vector) field.  Some examples of force fields include electric and gravitational fields.  Consider the gravitational field about a star in a 2D universe, with coordinate frame chosen so that the star is at the origin.  At each point in the plane there is a vector pointing at the origin with magnitude equal to the gravitational force felt at that point; the closer a point is to the origin the stronger the force.  The cell below draws such a field in red.  To avoid being drawn into the star, satellites might orbit at faster or slower speeds, depending on distance to the star.  If orbits are required to be everywhere orthogonal to the gravitational field (something that is not so in our solar system), the resulting velocity field will look like the one depicted in blue below.

x, y =var('x y') field1plot = plot_vector_field(vector([-y, x])/(-sqrt(x^2 + y^2)^(3/2)), (x,-2, 2), (y,-2, 2)) field2plot = plot_vector_field(vector([x, y])/(-sqrt(x^2 + y^2)^(3/2)), (x,-2, 2), (y,-2, 2)) show(field1plot + field2plot, aspect_ratio=1)

Gradient fields

If vector fields arise naturally in the physical world, they do so in multivariable calculus as well.  A scalar function of nn variables requires nn numeric inputs and produces a single numeric output.  Such a function, if it is differentiable, has partial derivatives, one for each input variable.  A very natural thing is to consider the gradient vector, the vector which has, as its components, each of the nn partial derivatives.  Since there is a gradient vector for each point of differentiability in nn-space, the original function induces a gradient (vector) field.  Such a field is portrayed in the next cell for a function ff of n=2n=2 variables, along with the surface z=f(x,y)z = f(x,y).

var('x y') f(x,y) = x^2 - y^2 # a scalar function of two variables, x and y print f.gradient() plot_vector_field(f.gradient(), (x,-3,3), (y,-3,3)).show() parametric_plot((x, y, f(x,y)), (x,-3,3), (y,-3,3))
((x, y) |--> 2*x, (x, y) |--> -2*y)

3D Vector Fields

There is really little difference in plotting 3-dimensional vector fields, beyond a slight change to the name of the function.

Below, we see a surprisingly interesting looking field given it has the simple formula

    F(x,y,z)=y(x,y,z) = yi x- xj +z+ zk.

If you don't like the color scheme used for the various arrows, you may dictate they be all of one color, say, red, by adding the switch: colors='red'.

x,y,z=var('x,y,z') plot_vector_field3d((y,-x,z), (x, -3,3), (y,-3,3), (z,-3,3))

 

The following cell draws the same field (in blue) as the previous cell, and then draws (in red) the trajectories of three separate free particles whose paths are everywhere tangent to the velocity field, one beginning at position (0, 1.42, 0.1), another at the nearby point (0, 1.42, 0), and a third beginning at (0, 2.84, -0.01).

x,y,z,t = var('x,y,z,t') p1 = plot_vector_field3d((y,-x,z), (x, -3,3), (y,-3,3), (z,-3,3), colors='blue') p2 = parametric_plot((1.42*sin(t), 1.42*cos(t), exp(t)/10), (t,0,3.5), color='red') p3 = parametric_plot((1.42*sin(t), 1.42*cos(t), 0), (t,0,6), color='red') p4 = parametric_plot((2.84*sin(t), 2.84*cos(t), -exp(t)/100), (t,0,6), color='red') show(p1 + p2 + p3 + p4)

Example of a 3D gradient field

Recall from MATH 162 that a gradient field is everywhere orthogonal to level sets of ff.  For instance, the function of three variables


    f(x,y,z)=x2+y2+z2f(x,y,z) = x^2 + y^2 + z^2


has level sets f(x,y,z)=cf(x,y,z) = c (whenever c>0c > 0) which are spheres.  Its gradient field is (2x,2y,2z)(2x, 2y, 2z).  The next cell plots several level sets of ff (all that is plotted are half-spheres instead of full spheres) along with this gradient field.  Notice that the arrows are orthogonal to the spheres.

var('x y z') f(x,y,z) = x^2+y^2+z^2 numSpheres = 5 spheres = sum([implicit_plot3d(f(x,y) == (4*(k+1)/numSpheres)^2, (x,0,4), \ (y,-4,4), (z,-4,4), opacity=(1-k/numSpheres), \ rgbcolor=(.5,.5*((k+1)/numSpheres),1)) for k in range(numSpheres)]) gradientField = plot_vector_field3d((2*x, 2*y, 2*z), (x,-3,3), (y,-3,3), (z,-3,3)) show(spheres + gradientField, aspect_ratio = [1,1,1])