lots of cleanup

This commit is contained in:
jverzani
2026-08-11 17:17:08 -04:00
parent ae461659e0
commit 253295ff6e
91 changed files with 18284 additions and 7872 deletions

View File

@@ -20,7 +20,7 @@ using DualNumbers
---
The derivative of $f(x)$ has the interpretation as the slope of the tangent line. The tangent line is the line that best approximates the function at the point.
The derivative of $f(x)$, when it exists, is the slope of the tangent line; the tangent line being the line that best approximates the function at the point.
Using the point-slope form of a line, we see that the tangent line to the graph of $f(x)$ at $(c,f(c))$ is given by:
@@ -48,17 +48,20 @@ tangent(sin, pi/4)
```
We make some graphs with tangent lines:
In @fig-plot-x-squared-two-tangent-lines-at-minus-1-2 we make some graphs with tangent lines.
::: {#fig-plot-x-squared-two-tangent-lines-at-minus-1-2}
```{julia}
#| hold: true
f(x) = x^2
plot(f, -3, 3)
plot!(tangent(f, -1))
plot!(tangent(f, 2))
plot(f, -3, 3; label="f", ylims=(-10, 10))
plot!(tangent(f, -1); label="tangent line at -1")
plot!(tangent(f, 2); label="tangent line at 2")
```
Plot of $f(x) = x^2$ over $[-3,3]$ with tangent lines drawn at $x=2$ and $x=-1$
:::
The graph shows that near the point, the line and function are close, but this need not be the case away from the point. We can express this informally as
@@ -69,10 +72,13 @@ $$
with the understanding this applies for $x$ "close" to $c$.
Usually for the applications herein, instead of $x$ and $c$ the two points are $x+\Delta_x$ and $x$. This gives:
Usually for the applications herein, instead of $x$ and $c$ the two points are labeled $x+\Delta_x$ and $x$. This gives:
::: {.definition title="Linearization"}
> *Linearization*: $\Delta_y = f(x +\Delta_x) - f(x) \approx f'(x) \Delta_x$, for small $\Delta_x$.
$\Delta_y = f(x +\Delta_x) - f(x) \approx f'(x) \Delta_x$, for small $\Delta_x$.
:::
@@ -85,15 +91,18 @@ This section gives some implications of this fact and quantifies what "close" ca
There are several approximations that are well known in physics, due to their widespread usage:
* That $\sin(x) \approx x$ around $x=0$:
* That $\sin(x) \approx x$ around $x=0$:
::: {#fig-plot-sin-tangent-line-at-0}
```{julia}
#| hold: true
plot(sin, -pi/2, pi/2)
plot!(tangent(sin, 0))
plot(sin, -pi/2, pi/2; label="sin")
plot!(tangent(sin, 0); label="tangent line at 0")
```
Plot of $\sin(x)$ and its tangent line at $0$
:::
Symbolically:
@@ -105,16 +114,19 @@ f(x) = sin(x)
f(c) + diff(f(x),x)(c) * (x - c)
```
* That $\log(1 + x) \approx x$ around $x=0$:
* That $\log(1 + x) \approx x$ around $x=0$:
::: {#fig-plot-log1p-x-and-tangent-at-0}
```{julia}
#| hold: true
f(x) = log(1 + x)
plot(f, -1/2, 1/2)
plot!(tangent(f, 0))
plot(f, -1/2, 1/2; label="sin")
plot!(tangent(f, 0); label="tangent line at 0")
```
Plot of $f(x) = log(1 + x)$ and its tangent line at $x=0$
:::
Symbolically:
@@ -129,16 +141,19 @@ f(c) + diff(f(x),x)(c) * (x - c)
(The `log1p` function implements a more accurate version of this function when numeric values are needed.)
* That $1/(1-x) \approx 1+x$ around $x=0$:
* That $1/(1-x) \approx 1+x$ around $x=0$:
::: {#fig-plot-1-over-1-minus-x-over-minus-one-half-one-half}
```{julia}
#| hold: true
f(x) = 1/(1-x)
plot(f, -1/2, 1/2)
plot!(tangent(f, 0))
plot(f, -1/2, 1/2; label="f")
plot!(tangent(f, 0); label="tangent line at 0")
```
Plot of $1/(1-x)$ and its tangent line at $x=0$
:::
Symbolically:
@@ -150,17 +165,21 @@ f(x) = 1 / (1 - x)
f(c) + diff(f(x),x)(c) * (x - c)
```
* That $(1+x)^n \approx 1 + nx$ around $x = 0$. For example, with $n=5$
* That $(1+x)^n \approx 1 + nx$ around $x = 0$. For example, with $n=5$
::: {#fig-plot-1-plus-x-to-the-n-and-tangent-line}
```{julia}
#| hold: true
n = 5
f(x) = (1+x)^n # f'(0) = n = n(1+x)^(n-1) at x=0
plot(f, -1/2, 1/2)
plot!(tangent(f, 0))
plot(f, -1/2, 1/2; label="f")
plot!(tangent(f, 0); label="tangent line at 0")
```
Plot of $f(x) = (1 + x)^5$ and its tangent line at $0$
:::
Symbolically:
@@ -227,7 +246,7 @@ plotly()
nothing
```
The plot in @fig-tangent-dy-dx shows a tangent line with slope $dy/dx$ and the actual change in $y$, $\Delta y$, for some specified $\Delta x$ at a point $(c,f(c))$. The small gap above the sine curve is the error were the value of the sine approximated using the drawn tangent line. We can see that approximating the value of $\Delta y = \sin(c+\Delta x) - \sin(c)$ with the often easier to compute $(dy/dx) \cdot \Delta x = f'(c)\Delta x$ - for small enough values of $\Delta x$ - is not going to be too far off provided $\Delta x$ is not too large.
The plot in @fig-tangent-dy-dx shows a tangent line with slope $dy/dx$ and the actual change in $y$, $\Delta y$, for some specified $\Delta x$ at a point $(c,f(c))$. The small gap above the sine curve is the error were the value of the sine approximated using the drawn tangent line. We can see that approximating the value of $\Delta y = \sin(c+\Delta x) - \sin(c)$ with the often easier to compute $(dy/dx) \cdot \Delta x = f'(c)\Delta x$---for small enough values of $\Delta x$---is not going to be too far off provided $\Delta x$ is not too large.
This approximation is known as linearization. It can be used both in theoretical computations and in practical applications. To see how effective it is, we look at some examples.
@@ -389,7 +408,7 @@ Vₛ(r) = 4/3 * pi * r^3
A *simple* pendulum is comprised of a massless "bob" on a rigid "rod" of length $l$. The rod swings back and forth making an angle $\theta$ with the perpendicular. At rest $\theta=0$, here we have $\theta$ swinging with $\lvert\theta\rvert \leq \theta_0$ for some $\theta_0$.
According to [Wikipedia](http://tinyurl.com/yz5sz7e) - and many introductory physics book - while swinging, the angle $\theta$ varies with time following this equation:
According to [Wikipedia](http://tinyurl.com/yz5sz7e)---and many introductory physics book---while swinging, the angle $\theta$ varies with time following this equation:
$$
@@ -405,7 +424,7 @@ This would be much easier if the second derivative were proportional to the angl
[Huygens](http://en.wikipedia.org/wiki/Christiaan_Huygens) used the approximation of $\sin(x) \approx x$, noted above, to say that when the angle is not too big, we have the pendulum's swing obeying $\theta''(t) = -g/l \cdot \theta(t)$. Without getting too involved in why, we can verify by taking two derivatives that $\theta_0\sin(\sqrt{g/l}\cdot t)$ will be a solution to this modified equation.
With this solution, the motion is periodic with constant amplitude (assuming frictionless behaviour), as the sine function is. More surprisingly, the period is found from $T = 2\pi/(\sqrt{g/l}) = 2\pi \sqrt{l/g}$. It depends on $l$ - longer "rods" take more time to swing back and forth - but does not depend on the how wide the pendulum is swinging between (provided $\theta_0$ is not so big the approximation of $\sin(x) \approx x$ fails). This latter fact may be surprising, though not to Galileo who discovered it.
With this solution, the motion is periodic with constant amplitude (assuming frictionless behaviour), as the sine function is. More surprisingly, the period is found from $T = 2\pi/(\sqrt{g/l}) = 2\pi \sqrt{l/g}$. It depends on $l$---longer "rods" take more time to swing back and forth---but does not depend on the how wide the pendulum is swinging between (provided $\theta_0$ is not so big the approximation of $\sin(x) \approx x$ fails). This latter fact may be surprising, though not to Galileo who discovered it.
## Differentials
@@ -414,11 +433,11 @@ With this solution, the motion is periodic with constant amplitude (assuming fri
The Leibniz notation for a derivative is $dy/dx$ indicating the change in $y$ as $x$ changes. It proves convenient to decouple this using *differentials* $dx$ and $dy$. What do these notations mean? They measure change along the tangent line in same way $\Delta_x$ and $\Delta_y$ measure change for the function. The differential $dy$ depends on both $x$ and $dx$, it being defined by $dy=f'(x)dx$. As tangent lines locally represent a function, $dy$ and $dx$ are often associated with an *infinitesimal* difference.
Taking $dx = \Delta_x$, as in the previous graphic, we can compare $dy$ the change along the tangent line given by $dy/dx \cdot dx$ and $\Delta_y$ the change along the function given by $f(x + \Delta_x) - f(x)$. The linear approximation, $f(x + \Delta_x) - f(x)\approx f'(x)dx$, says that
Taking $dx = \Delta_x$, as in the previous graphic, we can compare $dy$---the change along the tangent line given by $dy/dx \cdot dx$---and $\Delta_y$---the change along the function given by $f(x + \Delta_x) - f(x)$. The linear approximation, $f(x + \Delta_x) - f(x)\approx f'(x)dx$, says that
$$
\Delta_y \approx dy; \quad \text{ when } \Delta_x = dx
\Delta_y \approx dy; \quad \text{ when } \Delta_x = dx.
$$
## The error in approximation
@@ -426,8 +445,15 @@ $$
How good is the approximation? Graphically we can see it is pretty good for the graphs we choose, but are there graphs out there for which the approximation is not so good? Of course. However, we can say this (the [Lagrange](http://en.wikipedia.org/wiki/Taylor%27s_theorem) form of a more general Taylor remainder theorem):
::: {.definition title="Lagrange remainder form"}
> Let $f(x)$ be twice differentiable on $I=(a,b)$, $f$ is continuous on $[a,b]$, and $a < c < b$. Then for any $x$ in $I$, there exists some value $\xi$ between $c$ and $x$ such that $f(x) = f(c) + f'(c)(x-c) + (f''(\xi)/2)\cdot(x-c)^2$.
Let $f(x)$ be twice differentiable on $I=(a,b)$, $f$ is continuous on $[a,b]$, and $a < c < b$. Then for any $x$ in $I$, there exists some value $\xi$ between $c$ and $x$ such that
$$
f(x) = f(c) + f'(c)(x-c) + \frac{f''(\xi)}{2}\cdot(x-c)^2.
$$
:::
@@ -437,9 +463,9 @@ That is, the error is basically a constant depending on the concavity of $f$ tim
For $\sin(x)$ at $c=0$ we get $\lvert\sin(x) - x\rvert = \lvert-\sin(\xi)\cdot x^2/2\rvert$. Since $\lvert\sin(\xi)\rvert \leq 1$, we must have this bound: $\lvert\sin(x) - x\rvert \leq x^2/2$.
Can we verify? Let's do so graphically:
Can we verify? We do so graphically in @fig-graph-abs-sin-x-minus-x-and-x-squared-over-2.
::: {#fig-graph-abs-sin-x-minus-x-and-x-squared-over-2}
```{julia}
#| hold: true
h(x) = abs(sin(x) - x)
@@ -448,6 +474,9 @@ plot(h, -2, 2, label="h")
plot!(g, -2, 2, label="f")
```
Plot of $h(x) = \lvert \sin(x) - x \vert$ and $g(x) = x^2/2$ over $[-2, 2]$
:::
The graph shows a tight bound near $0$ and then a bound over this viewing window.
@@ -479,9 +508,9 @@ $$
\lvert f(x) - x\rvert \leq \lvert f''(0)\rvert \cdot \frac{x^2}{2} = x^2/2.
$$
Plotting we verify the bound on $|\log(1+x)-x|$:
In @fig-abs-log1p-minus-x-and-bound we verify the bound on $|\log(1+x)-x|$.
::: {#fig-abs-log1p-minus-x-and-bound}
```{julia}
#| hold: true
h(x) = abs(log(1+x) - x)
@@ -490,6 +519,9 @@ plot(h, -0.5, 2, label="h")
plot!(g, -0.5, 2, label="g")
```
Plot of $f(x) = \lvert \log(1 + x) - x \rvert$ and a derived upper bound
:::
Again, we see the very close bound near $0$, which widens at the edges of the viewing window.
@@ -500,15 +532,17 @@ To see formally why the remainder is as it is, we recall the mean value theorem
$$
\text{error} = h(x) - h(0) = (g(x) - g(0)) \frac{h'(e)}{g'(e)} =
(x^2 - 0) \cdot \frac{f'(e) - f'(0)}{2e} =
x^2 \cdot \frac{1}{2} \cdot f''(\xi).
\begin{align*}
\text{error} &= h(x) - h(0) = (g(x) - g(0)) \frac{h'(e)}{g'(e)} \\
&= (x^2 - 0) \cdot \frac{f'(e) - f'(0)}{2e} \\
&= x^2 \cdot \frac{1}{2} \cdot f''(\xi).
\end{align*}
$$
The value of $\xi$, from the mean value theorem applied to $f'(x)$, satisfies $0 < \xi < e < x$, so is in $[0,x].$
### The big (and small) "oh"
### The big---and small--""oh"
`SymPy` can find the tangent line expression as a special case of its `series` function (which implements [Taylor series](../taylor_series_polynomials.html)). The `series` function needs an expression to approximate; a variable specified, as there may be parameters in the expression; a value $c$ for *where* the expansion is taken, with default $0$; and a number of terms, for this example $2$ for a constant and linear term. (There is also an optional `dir` argument for one-sided expansions.)
@@ -681,165 +715,6 @@ x = Dual(1, 1)
We again see `log(x)` being evaluated in line `%6`. The derivative evaluated at `x` is done in line `%11` and this is multiplied by `xp` in line `%12`.
## Curvature
The curvature of a function will be a topic in a later section on differentiable vector calculus, but the concept of linearization can be used to give an earlier introduction.
The tangent line linearizes the function, it being the best linear approximation to the graph of the function at the point. The slope of the tangent line is the limit of the slopes of different secant lines. Consider now, the orthogonal concept, the *normal line* at a point. This is a line perpendicular to the tangent line that goes through the point on the curve.
At a point $(c,f(c))$ the slope of the normal line is $-1/f'(c)$.
Following [Kirby C. Smith](https://doi.org/10.2307/2687102), consider two nearby points on the curve of $f$ and suppose we take the two normal lines at $x=c$ and $x=c+h$. These two curves will intersect if the lines are not parallel. To ensure this, assueme that in some neighborhood of $c$, $f'(c)$ is increasing.
The two normal lines are:
$$
\begin{align*}
y &= f(c) - \frac{1}{f'(c)}(x-c)\\
y &= f(c+h) - \frac{1}{f'(c+h)}(x-(c+h))\\
\end{align*}
$$
Rearranging, we have
$$
\begin{align*}
-f'(c)(y-f(c)) &= x-c\\
-f'(c+h)(y-f(c+h)) &= x-(c+h)
\end{align*}
$$
Call $R$ the intersection point of the two normal lines:
```{julia}
#| echo: false
using Roots
let
gr()
f(x) = x^4
fp(x) = 4x^3
c = 1/4
h = 1/4
nlc(x) = f(c) - 1/fp(c) * (x - c)
nlch(x) = f(c+h) - 1/fp(c+h) * (x-(c+h))
canvas() = plot(axis=([],false), legend=false, aspect_ratio=:equal)
canvas()
plot!(f, 0, 3/4; line=(3,))
plot!(nlc; ylim=(-1/4, 1))
plot!(nlch; ylim=(-1/4, 1))
Rx = find_zero(x -> nlc(x) - nlch(x), (-10, 10))
scatter!([c,c+h], f.([c, c+h]))
scatter!([Rx], [nlc(Rx)])
annotate!([(c, f(c), L"(c,f(c))",:top),
(c+h, f(c+h), L"(c+h, f(c+h))",:bottom),
(Rx, nlc(Rx), L"R",:left)])
end
```
```{julia}
#| echo: false
plotly()
nothing
```
What happens to $R$ as $h \rightarrow 0$?
We can symbolically solve to see:
```{julia}
@syms 𝑓() 𝑓p() 𝑓pp() x y c
n1 = -𝑓p(c)*(y-𝑓(c)) ~ x - c
n2 = -𝑓p(c+)*(y-𝑓(c+)) ~ x - (c+)
R = solve((n1, n2), (x, y))
```
Taking limits of each term as $h$ goes to zero we have after some notation-simplifying substitution:
```{julia}
R = Dict(k => limit(R[k], =>0) for k in (x,y))
Rx = R[x](limit((𝑓(c+)-𝑓(c))/, =>0) => 𝑓p(c),
limit((𝑓p(c+)-𝑓p(c))/, =>0) => 𝑓pp(c))
```
and
```{julia}
Ry = R[y](limit((𝑓(c+)-𝑓(c))/, =>0) => 𝑓p(c),
limit((𝑓p(c+)-𝑓p(c))/, =>0) => 𝑓pp(c))
```
The squared distance, $r^2$, of $R$ to $(c,f(c))$ is then:
```{julia}
simplify((Rx-c)^2 + (Ry-𝑓(c))^2)
```
Or
$$
r^2 = \frac{(f'(c)^2 + 1)^3}{f''(c)^2}.
$$
This formula for $r$ is known as the radius of curvature of $f$ -- the radius of the *circle* that best approximates the function at the point. That is, this value reflects the curvature of $f$ supplementing the tangent line or best *linear* approximation to the graph of $f$ at the point.
```{julia}
#| echo: false
let
gr()
f(x) = x^4
fp(x) = 4x^3
fpp(x) = 12x^2
c = 1/4
h = 1/4
nlc(x) = f(c) - 1/fp(c) * (x - c)
nlch(x) = f(c+h) - 1/fp(c+h) * (x-(c+h))
canvas() = plot(axis=([],false), legend=false, aspect_ratio=:equal)
canvas()
plot!(f, -1/4, 3/4; line=(3,))
tl(x) = f(c) + f'(c)*(x-c)
plot!(tl, ylim=(-1/4, 3/2); line=(2, :dot))
Rx, Ry = c - fp(c)^3 / fpp(c) - fp(c)/fpp(c), f(c) + (fp(c)^2+1)/fpp(c)
r = (fp(c)^2 + 1)^(3/2) / abs(fpp(c))
scatter!([c], f.([c]))
scatter!([Rx], [nlc(Rx)])
annotate!([(c, f(c), L"(c,f(c))",:top),
(Rx, nlc(Rx), L"R",:left)])
Delta = pi/10
theta = range(3pi/2 - Delta, 2pi - 3Delta, length=100)
xs, ys = cos.(theta), sin.(theta)
plot!(Rx .+ r.*xs, Ry .+ r.*ys)
x0s, y0s = [Rx,Rx .+ r * first(xs)],[Ry,Ry .+ r * first(ys)]
xns, yns = [Rx,Rx .+ r * last(xs)],[Ry,Ry .+ r * last(ys)]
xcs, ycs = [Rx,c],[Ry,f(c)]
sty = (2, :0.25, :dash)
plot!(x0s, y0s; line=sty);
plot!(xcs, ycs; line=sty);
plot!(xns, yns; line=sty)
end
```
```{julia}
#| echo: false
plotly()
nothing
```
## Questions
@@ -857,8 +732,8 @@ choices = [
"``1 + x^{1/2}``",
"``1 + (1/2) \\cdot x``",
"``1 - (1/2) \\cdot x``"]
answ = 3
radioq(choices, answ)
answer = 3
radioq(choices, answer)
```
###### Question
@@ -875,8 +750,8 @@ choices = [
"``1 + x^k``",
"``1 + k \\cdot x``",
"``1 - k \\cdot x``"]
answ = 3
radioq(choices, answ)
answer = 3
radioq(choices, answer)
```
###### Question
@@ -894,8 +769,8 @@ choices = [
"``x``",
"``1 - x^2/2``"
]
answ = 1
radioq(choices, answ)
answer = 1
radioq(choices, answer)
```
###### Question
@@ -913,8 +788,8 @@ choices = [
"``1 + x``",
"``1 - x``"
]
answ = 2
radioq(choices, answ)
answer = 2
radioq(choices, answer)
```
###### Question
@@ -932,8 +807,8 @@ choices = [
"``1 + x``",
"``25``"
]
answ = 1
radioq(choices, answ)
answer = 1
radioq(choices, answer)
```
###### Question
@@ -946,8 +821,8 @@ Let $f(x) = \sqrt{x}$. Find the actual error in approximating $f(26)$ by the val
#| hold: true
#| echo: false
tgent(x) = 5 + x/10
answ = tgent(1) - sqrt(26)
numericq(answ)
answer = tgent(1) - sqrt(26)
numericq(answer)
```
###### Question
@@ -961,8 +836,8 @@ An estimate of some quantity was $12.34$ the actual value was $12$. What was the
#| echo: false
est = 12.34
act = 12.0
answ = (est -act)/act * 100
numericq(answ)
answer = (est -act)/act * 100
numericq(answer)
```
###### Question
@@ -978,8 +853,8 @@ tl(x) = x
x0 = 5 * pi/180
est = x0
act = sin(x0)
answ = (est -act)/act * 100
numericq(answ)
answer = (est -act)/act * 100
numericq(answer)
```
###### Question
@@ -992,8 +867,8 @@ The side length of a square is measured roughly to be $2.0$ cm. The actual lengt
#| hold: true
#| echo: false
tl(x) = 4 + 4x
answ = tl(.2) - 4
numericq(abs(answ))
answer = tl(.2) - 4
numericq(abs(answer))
```
###### Question