updates
This commit is contained in:
@@ -229,13 +229,24 @@ function secant_line_tangent_line_graph(n)
|
||||
m = (f(c+h) - f(c))/h
|
||||
|
||||
xs = range(0, stop=pi, length=50)
|
||||
plt = plot(f, 0, pi, legend=false, size=fig_size)
|
||||
fig_size=(800, 600)
|
||||
plt = plot(f, 0, pi, legend=false, size=fig_size,
|
||||
line=(2,),
|
||||
axis=([],false),
|
||||
ylims=(-.1,1.5)
|
||||
)
|
||||
plot!([0, 1.1* pi],[0,0], line=(3, :black))
|
||||
plot!([0, 0], [0,2*1], line=(3, :black))
|
||||
|
||||
plot!(plt, xs, f(c) .+ cos(c)*(xs .- c), color=:orange)
|
||||
plot!(plt, xs, f(c) .+ m*(xs .- c), color=:black)
|
||||
scatter!(plt, [c,c+h], [f(c), f(c+h)], color=:orange, markersize=5)
|
||||
|
||||
plot!(plt, [c, c+h, c+h], [f(c), f(c), f(c+h)], color=:gray30)
|
||||
annotate!(plt, [(c+h/2, f(c), text("h", :top)), (c + h + .05, (f(c) + f(c + h))/2, text("f(c+h) - f(c)", :left))])
|
||||
|
||||
annotate!(plt, [(c+h/2, f(c), text("h", :top)),
|
||||
(c + h + .05, (f(c) + f(c + h))/2, text("f(c+h) - f(c)", :left))
|
||||
])
|
||||
|
||||
plt
|
||||
end
|
||||
|
||||
@@ -773,6 +773,73 @@ answ = 4
|
||||
radioq(choices, answ)
|
||||
```
|
||||
|
||||
###### Question
|
||||
|
||||
Consider the following figure of a graph of $f$:
|
||||
|
||||
```{julia}
|
||||
#| echo: false
|
||||
ex(x) = x * tanh(exp(x))
|
||||
a, b = -5, 1
|
||||
plot(ex, a, b, legend=false,
|
||||
axis=([], false),
|
||||
color = :royalblue
|
||||
)
|
||||
plot!([a-.1, b+.1], [0,0], line=(3, :black))
|
||||
|
||||
zs = find_zeros(ex, (a, b))
|
||||
cps = find_zeros(ex', (a, b))
|
||||
ips = find_zeros(ex'', (a, b))
|
||||
|
||||
scatter!(zs, ex.(zs), marker=(5, "black", :circle))
|
||||
scatter!(cps, ex.(cps), marker=(5, "forestgreen", :diamond))
|
||||
scatter!(ips, ex.(ips), marker=(5, :brown3, :star5))
|
||||
```
|
||||
|
||||
The black circle denotes what?
|
||||
|
||||
```{julia}
|
||||
choices = [raw"A zero of $f$",
|
||||
raw"A critical point of $f$",
|
||||
raw"An inflection point of $f$"]
|
||||
answ = 1
|
||||
radioq(choices, answ)
|
||||
```
|
||||
|
||||
|
||||
The black circle denotes what?
|
||||
|
||||
```{julia}
|
||||
choices = [raw"A zero of $f$",
|
||||
raw"A critical point of $f$",
|
||||
raw"An inflection point of $f$"]
|
||||
answ = 1
|
||||
radioq(choices, answ)
|
||||
```
|
||||
|
||||
|
||||
The green diamond denotes what?
|
||||
|
||||
```{julia}
|
||||
choices = [raw"A zero of $f$",
|
||||
raw"A critical point of $f$",
|
||||
raw"An inflection point of $f$"]
|
||||
answ = 2
|
||||
radioq(choices, answ)
|
||||
```
|
||||
|
||||
|
||||
The red stars denotes what?
|
||||
|
||||
```{julia}
|
||||
choices = [raw"Zeros of $f$",
|
||||
raw"Critical points of $f$",
|
||||
raw"Inflection points of $f$"]
|
||||
answ = 3
|
||||
radioq(choices, answ)
|
||||
```
|
||||
|
||||
|
||||
###### Question
|
||||
|
||||
|
||||
@@ -1040,7 +1107,8 @@ This accurately summarizes how the term is used outside of math books. Does it a
|
||||
#| echo: false
|
||||
choices = ["Yes. Same words, same meaning",
|
||||
"""No, but it is close. An inflection point is when the *acceleration* changes from positive to negative, so if "results" are about how a company's rate of change is changing, then it is in the ballpark."""]
|
||||
radioq(choices, 2)
|
||||
answ = 2
|
||||
radioq(choices, answ)
|
||||
```
|
||||
|
||||
###### Question
|
||||
@@ -1054,5 +1122,6 @@ The function $f(x) = x^3 + x^4$ has a critical point at $0$ and a second derivat
|
||||
#| echo: false
|
||||
choices = ["As ``x^3`` has no extrema at ``x=0``, neither will ``f``",
|
||||
"As ``x^4`` is of higher degree than ``x^3``, ``f`` will be ``U``-shaped, as ``x^4`` is."]
|
||||
radioq(choices, 1)
|
||||
answ = 1
|
||||
radioq(choices, answ)
|
||||
```
|
||||
|
||||
@@ -11,6 +11,7 @@ using CalculusWithJulia
|
||||
using Plots
|
||||
plotly()
|
||||
using SymPy
|
||||
using Roots
|
||||
using TaylorSeries
|
||||
using DualNumbers
|
||||
```
|
||||
@@ -183,18 +184,30 @@ In each of these cases, a more complicated non-linear function is well approxim
|
||||
```{julia}
|
||||
#| hold: true
|
||||
#| echo: false
|
||||
#| label: fig-tangent-dy-dx
|
||||
#| fig-cap: "Graph with tangent line layered on"
|
||||
f(x) = sin(x)
|
||||
a, b = -1/4, pi/2
|
||||
|
||||
p = plot(f, a, b, legend=false);
|
||||
p = plot(f, a, b, legend=false,
|
||||
line=(3, :royalblue),
|
||||
axis=([], false)
|
||||
);
|
||||
|
||||
plot!(p, x->x, a, b);
|
||||
plot!(p, [0,1,1], [0, 0, 1], color=:brown);
|
||||
|
||||
plot!(p, [1,1], [0, sin(1)], color=:green, linewidth=4);
|
||||
annotate!(p, collect(zip([1/2, 1+.075, 1/2-1/8], [.05, sin(1)/2, .75], ["Δx", "Δy", "m=dy/dx"])));
|
||||
|
||||
scatter!([0], [0], marker=(5, :mediumorchid3))
|
||||
annotate!(p, [(0, f(0), text("(c,f(c))", :bottom,:right))])
|
||||
annotate!(p, collect(zip([1/2, 1+.075, 1/2-1/8],
|
||||
[.05, sin(1)/2, .75],
|
||||
["Δx", "Δy", "m=dy/dx"])));
|
||||
p
|
||||
```
|
||||
|
||||
The plot shows the tangent line with slope $dy/dx$ and the actual change in $y$, $\Delta y$, for some specified $\Delta x$. 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.
|
||||
@@ -643,6 +656,151 @@ x = Dual(1, 1)
|
||||
|
||||
We can see the derivative again reflects the chain rule, it being given by `1/x * xp` where `xp` acts like `dx` (from assignments `%5` and `%4`). Comparing the two outputs, we see only the assignment to `%5` differs, it reflecting the derivative of the function.
|
||||
|
||||
## 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 begin the best linear approximation to the graph of the function at the point. The slope of the tangent line is the limi of the slopes of different secant lines. Consdider 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
|
||||
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), "(c,f(c))",:top),
|
||||
(c+h, f(c+h), "(c+h, f(c+h))",:bottom),
|
||||
(Rx, nlc(Rx), "R",:left)])
|
||||
end
|
||||
```
|
||||
|
||||
|
||||
|
||||
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-simplfying 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
|
||||
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), "(c,f(c))",:top),
|
||||
(Rx, nlc(Rx), "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
|
||||
```
|
||||
|
||||
|
||||
## Questions
|
||||
|
||||
|
||||
@@ -330,26 +330,35 @@ Let $f(x)$ be differentiable on $(a,b)$ and continuous on $[a,b]$. Then there ex
|
||||
This says for any secant line between $a < b$ there will be a parallel tangent line at some $c$ with $a < c < b$ (all provided $f$ is differentiable on $(a,b)$ and continuous on $[a,b]$).
|
||||
|
||||
|
||||
This graph illustrates the theorem. The orange line is the secant line. A parallel line tangent to the graph is guaranteed by the mean value theorem. In this figure, there are two such lines, rendered using red.
|
||||
Figure @fig-mean-value-theorem illustrates the theorem. The orange line is the secant line. A parallel line tangent to the graph is guaranteed by the mean value theorem. In this figure, there are two such lines, rendered using red.
|
||||
|
||||
|
||||
```{julia}
|
||||
#| hold: true
|
||||
#| echo: false
|
||||
#| label: fig-mean-value-theorem
|
||||
f(x) = x^3 - x
|
||||
a, b = -2, 1.75
|
||||
m = (f(b) - f(a)) / (b-a)
|
||||
cps = find_zeros(x -> f'(x) - m, a, b)
|
||||
|
||||
p = plot(f, a-1, b+1, linewidth=3, legend=false)
|
||||
plot!(x -> f(a) + m*(x-a), a-1, b+1, linewidth=3, color=:orange)
|
||||
p = plot(f, a-0.75, b+1,
|
||||
color=:mediumorchid3,
|
||||
linewidth=3, legend=false,
|
||||
axis=([],false),
|
||||
)
|
||||
|
||||
|
||||
plot!(x -> f(a) + m*(x-a), a-1, b+1, linewidth=5, color=:royalblue)
|
||||
scatter!([a,b], [f(a), f(b)])
|
||||
annotate!([(a, f(a), text("a", :bottom)),
|
||||
(b, f(b), text("b", :bottom))])
|
||||
|
||||
|
||||
for cp in cps
|
||||
plot!(x -> f(cp) + f'(cp)*(x-cp), a-1, b+1, color=:red)
|
||||
plot!(x -> f(cp) + f'(cp)*(x-cp), a-1, b+1, color=:brown3)
|
||||
end
|
||||
|
||||
scatter!(cps, f.(cps))
|
||||
subsscripts = collect("₀₁₂₃₄₅₆₇₈₉")
|
||||
annotate!([(cp, f(cp), text("c"*subsscripts[i], :bottom)) for (i,cp) ∈ enumerate(cps)])
|
||||
|
||||
@@ -224,40 +224,49 @@ Raphson (1690) proposed a simplification avoiding the computation of new polynom
|
||||
##### Example: visualizing convergence
|
||||
|
||||
|
||||
This graphic demonstrates the method and the rapid convergence:
|
||||
@fig-newtons-method demonstrates the method and the rapid convergence:
|
||||
|
||||
|
||||
```{julia}
|
||||
#| echo: false
|
||||
function newtons_method_graph(n, f, a, b, c)
|
||||
nothing
|
||||
function newtons_method_graph(n, f, a, b, c; label=false)
|
||||
|
||||
xstars = [c]
|
||||
xs = [c]
|
||||
ys = [0.0]
|
||||
|
||||
plt = plot(f, a, b, legend=false, size=fig_size)
|
||||
plt = plot(f, a, b, legend=false, size=fig_size,
|
||||
line = (:royalblue, 3),
|
||||
axis = ([], false)
|
||||
)
|
||||
plot!(plt, [a, b], [0,0], color=:black)
|
||||
|
||||
|
||||
ts = range(a, stop=b, length=50)
|
||||
for i in 1:n
|
||||
x0 = xs[end]
|
||||
x1 = x0 - f(x0)/D(f)(x0)
|
||||
x1 = x0 - f(x0)/f'(x0)
|
||||
push!(xstars, x1)
|
||||
append!(xs, [x0, x1])
|
||||
append!(ys, [f(x0), 0])
|
||||
end
|
||||
plot!(plt, xs, ys, color=:orange)
|
||||
scatter!(plt, xstars, 0*xstars, color=:orange, markersize=5)
|
||||
if label
|
||||
subs = collect("₁₂₃₄₅₆₇₈₉")
|
||||
labs = ["x$(subs[i])" for i in eachindex(xstars)]
|
||||
annotate!(collect(zip(xstars, 0*xstars, labs,[:bottom for _ in xstars])))
|
||||
end
|
||||
plt
|
||||
end
|
||||
nothing
|
||||
```
|
||||
|
||||
```{julia}
|
||||
#| hold: true
|
||||
#| echo: false
|
||||
#| cache: true
|
||||
#| label: fig-newtons-method
|
||||
### {{{newtons_method_example}}}
|
||||
gr()
|
||||
caption = """
|
||||
@@ -270,7 +279,7 @@ n = 6
|
||||
fn, a, b, c = x->log(x), .15, 2, .2
|
||||
|
||||
anim = @animate for i=1:n
|
||||
newtons_method_graph(i-1, fn, a, b, c)
|
||||
newtons_method_graph(i-1, fn, a, b, c; label=true)
|
||||
end
|
||||
|
||||
imgfile = tempname() * ".gif"
|
||||
|
||||
@@ -76,7 +76,7 @@ ImageFile(imgfile, caption)
|
||||
## The secant line and the tangent line
|
||||
|
||||
|
||||
We approach this general problem **much** more indirectly than is needed. We introducing notations that are attributed to Newton and proceed from there. By leveraging `SymPy` we avoid tedious computations and *hopefully* gain some insight.
|
||||
We approach this general problem **much** more indirectly than is needed. We introduce notations that are attributed to Newton and proceed from there. By leveraging `SymPy` we avoid tedious computations and *hopefully* gain some insight.
|
||||
|
||||
|
||||
Suppose $f(x)$ is a function which is defined in a neighborhood of $c$ and has as many continuous derivatives as we care to take at $c$.
|
||||
@@ -102,7 +102,7 @@ $$
|
||||
tl(x) = f(c) + f'(c) \cdot(x - c).
|
||||
$$
|
||||
|
||||
The key is the term multiplying $(x-c)$ for the secant line is an approximation to the related term for the tangent line. That is, the secant line approximates the tangent line, which is the linear function that best approximates the function at the point $(c, f(c))$. This is quantified by the *mean value theorem* which states under our assumptions on $f(x)$ that there exists some $\xi$ between $x$ and $c$ for which:
|
||||
The key is the term multiplying $(x-c)$ for the secant line this is an approximation to the related term for the tangent line. That is, the secant line approximates the tangent line, which is the linear function that best approximates the function at the point $(c, f(c))$. This is quantified by the *mean value theorem* which states under our assumptions on $f(x)$ that there exists some $\xi$ between $x$ and $c$ for which:
|
||||
|
||||
|
||||
$$
|
||||
@@ -153,7 +153,7 @@ As in the linear case, there is flexibility in the exact points chosen for the i
|
||||
---
|
||||
|
||||
|
||||
Now, we take a small detour to define some notation. Instead of writing our two points as $c$ and $c+h,$ we use $x_0$ and $x_1$. For any set of points $x_0, x_1, \dots, x_n$, define the **divided differences** of $f$ inductively, as follows:
|
||||
Now, we take a small detour to define some notation. Instead of writing our two points as $c$ and $c+h,$ we use $x_0$ and $x_1$. For any set of points $x_0, x_1, \dots, x_n$, recursively define the Newton **divided differences** of $f$ inductively, as follows:
|
||||
|
||||
$$
|
||||
\begin{align*}
|
||||
|
||||
Reference in New Issue
Block a user