Using Matplotlib, a python plotting library, I figured out how to
graph both 2d and 3d vector fields along with their associated
flow lines. Intuitively, flow lines are curves which you get
by starting at a point and tracing in the direction of the vector
field. This is the path a particle would take in a vector field.
The formal definition of a flow line is this: Let \(\bf{F}\) be a
vector field and \(\bf{x(t)}\) be the flow line. Then \(\bf{x'(t) =
F(x(t))}\). In other words, at every point on the path \(\bf{x}\),
\(\bf{x}\) is tangent to the vector field \(\bf{F}\).
Graphing in 2d
Graphing a simple vector field
First, we must import all of our dependencies, which are matplotlib
and numpy.
import matplotlib.pyplot as plt
import numpy as np
Now we have to define the x and y components of our vector field as
a function of the point (x,y). In this case, \(\textbf{F}(x, y) =
(y, -x)\). We also define the x and y bounds as well as our step
size (which defines our 'grid' of vectors) and scale (which scales
the vectors in the graph).
We create a grid using np.meshgrid, where each point in the grid is
given by (X[i, j], Y[i, j]), where i and j are indices. We then
assign the x component of every vector in the grid to U and the y
component to V using our vector field function defined earlier.
Matplotlib's quiver function creates the vector field.
X, Y = np.meshgrid(np.arange(x_lim[0], x_lim[1], step), np.arange(y_lim[0], y_lim[1], step))
U = np.zeros(X.shape)
V = np.zeros(Y.shape)
for i in range(X.shape[0]):
for j in range(Y.shape[0]):
U[i,j] = vf_x(X[i, j], Y[i, j])
V[i,j] = vf_y(X[i, j], Y[i, j])
fig, ax = plt.subplots(figsize=(20, 20))
_ = ax.quiver(X, Y, U, V, units='xy', scale=scale, color='red')
plt.xlim(x_lim)
plt.ylim(y_lim)
Finally, we can plot the vector field and make it look at bit nicer.
This is the final result!
Graphing Functions
We can also graph functions using plt.plot, including flow lines.
func_x is an array of x values, and func_y is an array of y values.
plt.plot(func_x, func_y, 'g')
For example, this plots the flowline through (0, 0) for the vector
field \(\textbf{F}(x, y) = (1, x+y)\).
Graphing Flow Lines using Euler's Method
What if we can't find the equation to the flow line directly? We can
use
Euler's method, and the premise behind Euler's method is this:
Start at point (x, y)
Find the derivative, or slope, at (x, y). The vector field already
gives us the x and y components of the derivative!
Take a step in the direction of the derivative, and get a new
point (x, y)
Repeat steps 1-3 until either x or y are outside the desired
bounds
In python, it looks like this:
Here's the result—it looks identical to the graph we generated by
manually finding the flow line! As long as the step size is
sufficiently large, the numerically generated flow line should be
close to the actual flow line.
Graphing in 3d
Well, can we graph vector fields in 3d? Yes! To graph in 3d, we have
to add a special import which we will use to create a 3d axis. Other
than that, all we have to do is add an extra variable.
We can also use Euler's method in 3d as well. It's the same as the
2d way, but we're adding a z variable.