tangents: Add Tangent Lines to a Plot

View source: R/tangents.R

tangentsR Documentation

Add Tangent Lines to a Plot

Description

Draw tangent lines to an existing plot.

Usage

tangents(x, y, slope, length = 1, ...)

Arguments

x, y

coordinate vectors of points x and function values y.

slope

vector of slopes at the points x.

length

desired length of tangent lines, see ‘Details’.

...

further graphical parameters to be passed to segments.

Details

The length of the tangent lines is scaled according to the current aspect ratio of the existing plot.

Author(s)

Carmen van Meegen

See Also

segments for drawing line segments between pairs of points.

Examples

# Define test function and its gradient from Oakley and O'Hagan (2002)
f <- function(x) 5 + x + cos(x)
fGrad <- function(x) 1 - sin(x)

# Generate coordinates and calculate slopes
x <- seq(-5, 5, length = 5)
y <- f(x)
dy <- fGrad(x)

# Draw curve and tangent lines
curve(f(x), from = -6, to = 6)
tangents(x, y, dy, length = 2, lwd = 2, col = 2:6)
points(x, y, pch = 16)

gek documentation built on April 4, 2025, 12:35 a.m.

Related to tangents in gek...