The purpose of this answer is to give simple, clear answers to the simple component questions,
- How to draw an infinite tangent line?
- How to draw an infinite secant line?
I will use the V10+ InfiniteLine
, which Mr.Wizard has already pointed out as a way to draw an infinite line. See also the Note below.
How to draw a tangent line
Round about the eighth example under "Applications" in the documentation for InfiniteLine
, one finds how to draw tangent lines to parametric plots. The example can be adapted to Plot[f[x], {x, a, b}]
. The tangent line at x == x0
is given by
InfiniteLine[{x0, f[x0]}, {1, f'[x0]}]
This is the two-argument, point-vector form of InfiniteLine
, where {x0, f[x0]}
is a point on the line and {1, f'[x0]}
is the direction vector of the line. To draw it on the graph of f
, one can use either
Plot[f[x], {x, a, b}, Epilog -> {InfiniteLine[{x0, f[x0]}, {1, f'[x0]}]}]
or
Show[
Plot[f[x], {x, a, b}],
Graphics[{InfiniteLine[{x0, f[x0]}, {1, f'[x0]}]}]
]
How to draw a secant line
To draw the secant line through the points {x0, f[x0]}
and {x1, f[x1]}
, use the single-argument, point-point form of InfiniteLine
:
InfiniteLine[{{x0, f[x0]}, {x1, f[x1]}}]
Note the braces comprising the two points making them into a single argument. We can draw it on the plot of f
in the same way as we did the tangent line, either
Plot[f[x], {x, a, b}, Epilog -> {InfiniteLine[{{x0, f[x0]}, {x1, f[x1]}}]}]
or
Show[
Plot[f[x], {x, a, b}],
Graphics[{InfiniteLine[{{x0, f[x0]}, {x1, f[x1]}}]}]
]
Example
In the OP's example, for the function, we can use the expression f = 15 - 2 x^2
and we can find its derivative with D[f, x]
instead of the prime notation. We can also evaluate it at a point x == x0
, using ReplaceAll
to substitute x0
for x
with f /. x -> x0
.
f = 15 - 2 x^2;
x0 = 1;
x1 = 1.8;
Show[
Plot[f, {x, -1, 3}, PlotStyle -> Red],
Graphics[{
Blue, InfiniteLine[{x0, f /. x -> x0}, {1, D[f, x] /. x -> x0}], (* tangent *)
Green, InfiniteLine[{{x0, f /. x -> x0}, {x1, f /. x -> x1}}], (* secant *)
Black, PointSize[Large], Point[{{x0, f /. x -> x0}, {x1, f /. x -> x1}}]
}]]
Animation
The animation does not seem central to the question. Since the more sensible ways have been done, e.g., using Table
or Animate
to change x1
, here's a different way that wraps the plot above in DynamicModule
and animates the figure with Dynamic
; the Button
bumps x1
a little, which will then approach the point of tangency at x0
with exponentially decaying speed.
DynamicModule[{x0, x1, f},
f = 15 - 2 x^2;
x0 = 1;
x1 = 1.8;
Column[{
Button["bump", x1 = x1 + RandomReal[{-1.5, 1.5}]],
Show[
Plot[f, {x, -1, 3}, PlotStyle -> Red],
Graphics[
Dynamic@{If[Abs[x0 - x1] > 10^-6, x1 = {0.05, 0.95}.{x0, x1}];
Blue, InfiniteLine[{x0, f /. x -> x0}, {1, D[f, x] /. x -> x0}],
Green, InfiniteLine[{{x0, f /. x -> x0}, {x1, f /. x -> x1}}],
Black, PointSize[Large],
Point[{{x0, f /. x -> x0}, {x1, f /. x -> x1}}]
}]]
}]
]
Note: The ulterior purpose is that this question is used sometimes as a duplicate of the tangent line question. (For a finite tangent line, sliding a tangent line along a curve is sometimes used as the duplicate.) Most of the questions about tangent lines are complicated by other requirements. This is the oldest question I could find that seemed relevant, and since it has already been linked as a duplicate, I felt this was the best place for this answer.
Line[]
object always has two ends, by its very nature (so it is properly a line segment as opposed to a line)... what you can do is to have yourLine[]
extend past thePlotRange
of your plot... $\endgroup$