Learn R Programming

gek (version 1.2.0)

tangents: 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.

Author

Carmen van Meegen

Details

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

References

Oakley, J. and O'Hagan, A. (2002). Bayesian Inference for the Uncertainty Distribution of Computer Model Outputs. Biometrika, 89(4):769--784. tools:::Rd_expr_doi("10.1093/biomet/89.4.769").

See Also

segments for drawing line segments between pairs of points.

Examples

Run this code
# 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)

Run the code above in your browser using DataLab