Loading editor...

Plotting functions and tangents

Loading commands, almost done…

This example demonstrates how to plot a function and draw a tangent line at a point. We'll work with the function y = x · sin(x) and explore how to visualize both the function and its tangent at any point.

Define the function

First, let's define our function y = x · sin(x). We'll use the variable x as our input:

  • : x = \scalar 4

Now calculate y using the sine function:

  • : sin-x = \sin x
  • : y = \mul x sin-x

Define colors:

Hide the origin point p0:

Set animation speed:


Plot the entire function

To see the full function curve, we can use the plot command directly with our x and y variables:

  • : \plot x y

You could give a name to the plot (eg my-plot = \plot x y). However a name is auto-assigned when not provided. To know the auto-assigned name, just hover the mouse anywhere over the plot. In this case, the auto-assigned name must be plot0.

Create points and draw the function line

Let's create a point at (x, 0) on the x-axis and a point at (x, y) on the function:

  • : \line p-x-axis p-func

Draw the tangent line

The tangent command automatically calculates the derivative and draws the tangent line at the given x coordinate on the plot:

  • :

Animate the point

Let's animate x from -4 to 4 to see how the tangent line changes as we move along the function:


Calculating gradients with broadcasting

One can also use an array of points as input to tangent function.