A Python tool for visualizing tangent lines to mathematical functions using SymPy and Matplotlib.
This tool graphs tangent lines for two types of functions:
- Scalar functions:
y = f(x)- standard single-variable functions - Parametric curves:
r(t) = (x(t), y(t))- curves defined by parametric equations
The visualization helps understand the behavior of derivatives and tangent lines at multiple points along a curve, making it useful for educational purposes or mathematical exploration.
This tool requires the following Python packages:
numpy- for numerical computationssympy- for symbolic mathematics and automatic differentiationmatplotlib- for plotting and visualization
- Clone this repository:
git clone https://github.com/Lunaaaalj/tangents.git
cd tangents- Install the required dependencies:
pip install numpy sympy matplotlibThe tool supports two modes: scalar for scalar functions and param for parametric curves.
Plot tangent lines for a scalar function y = f(x):
python tangents.py scalar --f "sin(x) + x**2/8" --xmin -4 --xmax 4 --n 9 --segment 1.2Arguments:
--f: The function expression (required)--xmin: Minimum x value (required)--xmax: Maximum x value (required)--n: Number of tangent lines to draw (default: 7)--segment: Length of each tangent segment in x-units (default: 1.0)--samples: Number of points to sample for the curve (default: 800)--points: How to choose tangent points -evenorrandom(default: even)
More Examples:
# Simple parabola
python tangents.py scalar --f "x**2" --xmin -3 --xmax 3 --n 5
# Trigonometric function
python tangents.py scalar --f "cos(x)" --xmin 0 --xmax 6.28318 --n 8 --segment 0.8
# Complex function with random tangent points
python tangents.py scalar --f "exp(-x**2/4)*sin(2*x)" --xmin -4 --xmax 4 --n 10 --points randomPlot tangent lines for a parametric curve r(t) = (x(t), y(t)):
python tangents.py param --x "cos(t)" --y "sin(t) + 0.25*sin(3*t)" --tmin 0 --tmax 6.28318 --n 12 --segment 0.6Arguments:
--x: The x(t) expression (required)--y: The y(t) expression (required)--tmin: Minimum t value (required)--tmax: Maximum t value (required)--n: Number of tangent lines to draw (default: 10)--segment: Length of each tangent segment in curve units (default: 0.8)--samples: Number of points to sample for the curve (default: 1200)--points: How to choose tangent points -evenorrandom(default: even)
More Examples:
# Circle
python tangents.py param --x "cos(t)" --y "sin(t)" --tmin 0 --tmax 6.28318 --n 8
# Ellipse
python tangents.py param --x "2*cos(t)" --y "sin(t)" --tmin 0 --tmax 6.28318 --n 12
# Lissajous curve
python tangents.py param --x "sin(3*t)" --y "cos(4*t)" --tmin 0 --tmax 6.28318 --n 16 --segment 0.4The tool supports the following mathematical functions and constants in expressions:
Constants:
pi- πE- Euler's number (e)
Basic Functions:
sqrt(x)- Square rootexp(x)- Exponential (e^x)log(x)- Natural logarithmAbs(x)- Absolute value
Trigonometric Functions:
sin(x),cos(x),tan(x)- Standard trig functionsasin(x),acos(x),atan(x)- Inverse trig functionssinh(x),cosh(x),tanh(x)- Hyperbolic functions
Other Functions:
Min(x, y),Max(x, y)- Minimum and maximumPiecewise(...)- Piecewise functions
- Expression Parsing: User-provided mathematical expressions are parsed using SymPy's symbolic mathematics engine with a restricted namespace for security
- Automatic Differentiation: SymPy automatically computes the derivative of the function
- Tangent Point Selection: Points are chosen either evenly spaced or randomly within the specified range
- Tangent Line Calculation:
- For scalar functions: Uses the derivative to compute slope
mat each point, then draws the liney - y₀ = m(x - x₀) - For parametric curves: Uses the tangent vector
(dx/dt, dy/dt)to determine the direction at each point
- For scalar functions: Uses the derivative to compute slope
- Visualization: Matplotlib renders the curve and tangent lines
- The tangent segments are normalized for parametric curves to ensure consistent visual length
- If a parametric curve has a degenerate point (where both derivatives are ~0), it's marked with an 'x' instead of a tangent line
- All expressions are evaluated in a safe, restricted namespace to prevent arbitrary code execution
- The aspect ratio for parametric plots is set to 'equal' when the data is finite to preserve the curve's shape
This project is provided as-is for educational and visualization purposes.