Loading editor...

Vector field I: gradient of a scalar function

Loading commands, almost done…

A vector field can be derived from a scalar function by computing its gradient. In this example, we define a scalar function:

The gradient of this function gives us a vector field where each vector points in the direction of steepest ascent. The gradient is:

Let's visualize this vector field and see how changing the parameter a affects it.

First, we define the parameter a:

  • : a = \scalar 1

Now we need a point to evaluate the function and its gradient. Let's start with a point p:

  • : p = \point 0 0

To compute , we need to extract the x and y coordinates:

  • : px = \x-coord p
  • : py = \y-coord p

Now we calculate the squared terms:

  • : px-squared = \mul px px
  • : py-squared = \mul py py

Multiply a by :

  • : a-px-squared = \mul a px-squared

Finally, compute :

  • : out = \sub a-px-squared py-squared

Visualizing the vector field

Now we can visualize the vector field using the \vector-field command, which automatically computes the gradient at each point:

  • : \vector-field p out

Reactivity

Try changing the value of a to see how the vector field transforms.

  • First we need to create

Notice how the vector field changes shape as you modify a. When a is positive, the vectors point outward in the x-direction, and when a is negative, they point inward.