Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download
Project: Summer A ODEs
Views: 65
Visibility: Unlisted (only visible to those who know the link)
Image: ubuntu2004
Kernel: SageMath 9.2

Plotting vector fields

Similar to the previous notebook, which plotted slope fields, this notebook contains an interactive element which plots a vector field. Into the box input a vector function of the variables xx and yy of the form v(x,y)=(v1(x,y),v2(x,y))v(x,y) = (v_1(x,y), v_2(x,y))

x,y = var('x,y') @interact def show_vector_field(xrange = slider(1, 10, .5, default = 3), yrange = slider(1,10, .5, default = 3), v = input_box((sin(x), cos(y)), label = '$v(x,y)$'), streamline = checkbox(False, label = "streamline plot")): if streamline: show(streamline_plot(v, (x,-xrange, xrange), (y,-yrange, yrange))) else: show(plot_vector_field(v, (x,-xrange,xrange), (y,-yrange,yrange)))

Plotting vector field with flow lines

If you see above, checking the "steamline" button produced a "streamline plot" with solutions curves. We can select a few initial conditions and plot them with the vector field below. Now instead of specifying only y(0)y(0) as in the Slope_Fields notebook, we can specify a starting point. (Feel free to modify the other page to work in this more general situation!)

x,y = var('x,y') @interact def show_vector_field_curves(xrange = slider(1, 10, .5, default = 5), yrange = slider(1,10, .5, default = 5), v = input_box((x + y , x ^2 + 1), label = '$v(x,y)$'), ics = input_box([(1,1), (2,3), (-1,1)], label = "$(x(0), y(0))$")): g = plot_vector_field(v, (x,-xrange,xrange), (y,-yrange,yrange)) g += streamline_plot(v, (x,-xrange,yrange), (y,-yrange,yrange), start_points=ics) g += point(ics, color='red', size = 20) show(g)

3D Vector Fields

We can also fairly straightforwardly generalize this to the next dimension we can visualize and plot 3d vector fields. Now

v(x,y,z)=(v1(x,y,z),v2(x,y,z),v3(x,y,z))v(x,y,z) = (v_1(x,y,z), v_2(x,y,z), v_3(x,y,z))

has three components.

x,y,z=var('x y z') @interact def show_vector_field(xrange = slider(1, 10, .5, default = 3), yrange = slider(1,10, .5, default = 3), zrange = slider(1,10, .5, default = 3), v = input_box((-y,x, z), label = '$v(x,y,z)$')): show(plot_vector_field3d(v, (x,-xrange,xrange), (y,-yrange,yrange), (z, -zrange, zrange)))
show(latex(x^2))
x2\renewcommand{\Bold}[1]{\mathbf{#1}}x^{2}