Skip to content

Lunaaaalj/tangents

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 

Repository files navigation

Tangents

A Python tool for visualizing tangent lines to mathematical functions using SymPy and Matplotlib.

Purpose

This tool graphs tangent lines for two types of functions:

  1. Scalar functions: y = f(x) - standard single-variable functions
  2. 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.

Dependencies

This tool requires the following Python packages:

  • numpy - for numerical computations
  • sympy - for symbolic mathematics and automatic differentiation
  • matplotlib - for plotting and visualization

Installation

  1. Clone this repository:
git clone https://github.com/Lunaaaalj/tangents.git
cd tangents
  1. Install the required dependencies:
pip install numpy sympy matplotlib

Usage

The tool supports two modes: scalar for scalar functions and param for parametric curves.

Scalar Functions

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.2

Arguments:

  • --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 - even or random (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 random

Parametric Curves

Plot 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.6

Arguments:

  • --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 - even or random (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.4

Supported Mathematical Functions

The tool supports the following mathematical functions and constants in expressions:

Constants:

  • pi - π
  • E - Euler's number (e)

Basic Functions:

  • sqrt(x) - Square root
  • exp(x) - Exponential (e^x)
  • log(x) - Natural logarithm
  • Abs(x) - Absolute value

Trigonometric Functions:

  • sin(x), cos(x), tan(x) - Standard trig functions
  • asin(x), acos(x), atan(x) - Inverse trig functions
  • sinh(x), cosh(x), tanh(x) - Hyperbolic functions

Other Functions:

  • Min(x, y), Max(x, y) - Minimum and maximum
  • Piecewise(...) - Piecewise functions

How It Works

  1. Expression Parsing: User-provided mathematical expressions are parsed using SymPy's symbolic mathematics engine with a restricted namespace for security
  2. Automatic Differentiation: SymPy automatically computes the derivative of the function
  3. Tangent Point Selection: Points are chosen either evenly spaced or randomly within the specified range
  4. Tangent Line Calculation:
    • For scalar functions: Uses the derivative to compute slope m at each point, then draws the line y - y₀ = m(x - x₀)
    • For parametric curves: Uses the tangent vector (dx/dt, dy/dt) to determine the direction at each point
  5. Visualization: Matplotlib renders the curve and tangent lines

Notes

  • 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

License

This project is provided as-is for educational and visualization purposes.

About

A Python tool for visualizing tangent lines to mathematical functions using SymPy and Matplotlib.

Topics

Resources

Stars

Watchers

Forks

Contributors

Languages