This commit is contained in:
jverzani
2025-07-23 08:05:43 -04:00
parent 31ce21c8ad
commit c3a94878f3
50 changed files with 3711 additions and 1385 deletions

1
quarto/.gitignore vendored
View File

@@ -3,5 +3,6 @@
/_freeze/
/*/*_files/
/*/*.ipynb/
/*/bonepile.qmd
/*/references.bib
weave_support.jl

View File

@@ -11,7 +11,7 @@ typst_tpl = mt"""
---
title: {{:title}}
date: today
jupyter: julia-1.11
engine: julia
execute:
daemon: false
format:
@@ -25,6 +25,11 @@ format:
#set figure(placement: auto)
bibliography: references.bib
---
```{julia}
#| echo: false
import Plots; Plots.plotly() = Plots.gr();
nothing
```
"""
index = "_pdf_index"

View File

@@ -230,13 +230,22 @@ function secant_line_tangent_line_graph(n)
xs = range(0, stop=pi, length=50)
fig_size=(800, 600)
plt = plot(f, 0, pi, legend=false, size=fig_size,
line=(2,),
axis=([],false),
plt = plot(;
xaxis=([], false),
yaxis=([], false),
framestyle=:origin,
legend=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!(f, 0, pi/2; line=(:black, 2))
plot!(f, pi/2, pi/2 + pi/5; line=(:black, 2, 1/4))
plot!(f, pi/2 + pi/5, pi; line=(:black, 2))
plot!(0.1 .+ [0,0],[-.1, 1.5]; line=(:gray,1), arrow=true, side=:head)
plot!([-0.2, 3.4], [.1, .1]; line=(:gray, 1), arrow=true, side=:head)
plot!(plt, xs, f(c) .+ cos(c)*(xs .- c), color=:orange)
plot!(plt, xs, f(c) .+ m*(xs .- c), color=:black)
@@ -244,8 +253,10 @@ function secant_line_tangent_line_graph(n)
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(L"h", :top)),
(c + h + .05, (f(c) + f(c + h))/2, text(L"f(c+h) - f(c)", :left)),
])
plt
@@ -258,7 +269,7 @@ The slope of each secant line represents the *average* rate of change between $c
n = 5
n = 6
anim = @animate for i=0:n
secant_line_tangent_line_graph(i)
end
@@ -279,11 +290,59 @@ $$
We will define the tangent line at $(c, f(c))$ to be the line through the point with the slope from the limit above - provided that limit exists. Informally, the tangent line is the line through the point that best approximates the function.
::: {#fig-tangent_line_approx_graph}
```{julia}
#| echo: false
gr()
let
function make_plot(Δ)
f(x) = 1 + sin(x-c)
df(x) = cos(x-c)
plt = plot(;
#xaxis=([], false),
yaxis=([], false),
aspect_ratio=:equal,
legend=false,
)
c = 1
xticks!([c-Δ, c, c+Δ], [latexstring("c-$Δ"), L"c", latexstring("c-$Δ")])
y₀ = f(c) - 2/3 * Δ
tl(x) = f(c) + df(c) * (x-c)
plot!(f, c - Δ, c + Δ; line=(:black, 2))
plot!(tl, c - Δ, c + Δ; line=(:red, 2))
plot!([c,c], [tl(c-Δ), f(c)]; line=(:gray, :dash, 1))
#plot!([c-1.1*Δ, c+1.1*Δ], y₀ .+ [0,0]; line=(:gray, 1), arrow=true)
current()
end
ps = make_plot.((1.5, 1.0, 0.5, 0.1))
plot(ps...)
end
```
Illustration that the tangent line is the best linear approximation *near* $c$.
:::
```{julia}
#| echo: false
plotly()
nothing
```
```{julia}
#| hold: true
#| echo: false
#| cache: true
#| eval: false
gr()
function line_approx_fn_graph(n)
f(x) = sin(x)

Binary file not shown.

After

Width:  |  Height:  |  Size: 37 KiB

View File

@@ -50,7 +50,7 @@ A parallel definition with $a < b$ implying $f(a) > f(b)$ would be used for a *s
We can try and prove these properties for a function algebraically we'll see both are related to the zeros of some function. However, before proceeding to that it is usually helpful to get an idea of where the answer is using exploratory graphs.
We will use a helper function, `plotif(f, g, a, b)` that plots the function `f` over `[a,b]` highlighting the regions in the domain when `g` is non-negative. Such a function is defined for us in the accompanying `CalculusWithJulia` package, which has been previously been loaded.
We will use a helper function, `plotif(f, g, a, b)` that plots the function `f` over `[a,b]` highlighting the regions in the domain when `g` is non-negative. Such a function is defined for us in the accompanying `CalculusWithJulia` package, which has been previously loaded.
To see where a function is positive, we simply pass the function object in for *both* `f` and `g` above. For example, let's look at where $f(x) = \sin(x)$ is positive:
@@ -475,22 +475,22 @@ Let's look at the function $x^2 \cdot e^{-x}$ for positive $x$. A quick graph sh
```{julia}
h(x) = x^2 * exp(-x)
plotif(h, h'', 0, 8)
g(x) = x^2 * exp(-x)
plotif(g, g'', 0, 8)
```
From the graph, we would expect that the second derivative - which is continuous - would have two zeros on $[0,8]$:
```{julia}
ips = find_zeros(h'', 0, 8)
ips = find_zeros(g'', 0, 8)
```
As well, between the zeros we should have the sign pattern `+`, `-`, and `+`, as we verify:
```{julia}
sign_chart(h'', 0, 8)
sign_chart(g'', 0, 8)
```
### Second derivative test
@@ -744,6 +744,90 @@ choices=[
answ = 3
radioq(choices, answ)
```
###### Question
The function
$$
f(x) =
\begin{cases}
\frac{x}{2} + x^2 \sin(\frac{\pi}{x}) & x \neq 0\\
0 & x = 0
\end{cases}
$$
is graphed below over $[-1/3, 1/3]$.
```{julia}
#| echo: false
plt = let
gr()
empty_style = (xaxis=([], false),
yaxis=([], false),
framestyle=:origin,
legend=false)
axis_style = (arrow=true, side=:head, line=(:gray, 1))
## f'(0) > 0 but not increasing
f(x) = x/2 + x^2 * sinpi(1/x)
g(x) = x/2 - x^2
a, b = -1/3, 1/3
xs = range(a, b, 10_000)
ys = f.(xs)
y0,y1 = extrema(ys)
plot(; empty_style..., aspect_ratio=:equal)
plot!([a,b],[0,0]; axis_style...)
plot!([0,0], [y0,y1]; axis_style...)
plot!(xs, f.(xs); line=(:black, 1))
plot!(xs, x -> x/2 + x^2; line=(:gray, 1, :dot))
plot!(xs, x -> x/2 - x^2; line=(:gray, 1, :dot))
plot!(xs, x -> x/2; line=(:gray, 1))
a1 = (1/4 + 1/5)/2
a2 = -(1*1/3 + 4*1/4)/5
annotate!([
(a1, g(a1), text(L"\frac{x}{2} - x^2", 10, :top)),
(a1, f(a1), text(L"\frac{x}{2} + x^2", 10, :bottom)),
(-1/6, f(1/6), text(L"\frac{x}{2} + x^2\sin(\frac{\pi}{x})", 10, :bottom))
])
plot!([-1/6, -1/13.5], [f(1/6), f(-1/13.5)]; axis_style...)
end
plt
```
```{julia}
#| echo: false
plotly()
nothing
```
This function has a derivative at $0$ that is *positive*
```{julia}
f(x) = x == 0 ? 0 : x/2 + x^2 * sinpi(1/x)
@syms h
limit((f(0+h) - f(0))/h, h=>0; dir="+-")
```
Is the function increasing **around** $0$?
(The derivative away from $0$ is given by:
```{julia}
@syms x
diff(f(x), x)
```
```{julia}
#| echo: false
choices = ["Yes", "No"]
answer = 1
buttonq(choices, answer; explanation=raw"""
The slope of the tangent line away from $0$ oscillates from positive to negative at every rational number of the form $1/n$ due to the $\cos(\pi/x)$ term, so it is neither going just up or down around $0$. (This example comes from @Angenent.)
""")
```
###### Question
@@ -779,21 +863,30 @@ 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))
let
gr()
ex(x) = x * tanh(exp(x))
a, b = -5, 1
plot(ex, a, b, legend=false,
axis=([], false),
line=(:black, 2)
)
plot!([a-.1, b+.1], [0,0], line=(:gray,1), arrow=true, side=:head)
zs = find_zeros(ex, (a, b))
cps = find_zeros(ex', (a, b))
ips = find_zeros(ex'', (a, b))
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))
scatter!(zs, ex.(zs), fill=(:black,), marker=(8, :circle))
scatter!(cps, ex.(cps), fill=(:green,), marker=(8, :diamond))
scatter!(ips, ex.(ips), fill=(:brown3,), marker=(8,:star5))
end
```
```{julia}
#| echo: false
plotly()
nothing
```
The black circle denotes what?

View File

@@ -93,6 +93,42 @@ In general though, we may not be able to solve for $y$ in terms of $x$. What the
The idea is to *assume* that $y$ is representable by some function of $x$. This makes sense, moving on the curve from $(x,y)$ to some nearby point, means changing $x$ will cause some change in $y$. This assumption is only made *locally* - basically meaning a complicated graph is reduced to just a small, well-behaved, section of its graph.
::: {#fig-well-behaved-section}
```{julia}
#| echo: false
let
gr()
a = 1
k = 2
F(x,y) = a * (x + a)*(x^2 + y^2) - k*x^2
xs = range(-3/2, 3/2, 100)
ys = range(-2, 2, 100)
contour(xs, ys, F; levels=[0],
axis=([], nothing),
line=(:black, 1),
framestyle=:none, legend=false)
x₀, y₀ = 3/4, -0.2834733547569205
m = (-a^2*x₀ - 3*a*x₀^2/2 - a*y₀^2/2 + k*x₀)/(a*(a + x₀)*y₀)
plot!(x -> y₀ + m*(x - x₀), x₀-0.5, x₀ + 0.5; line=(:gray, 2))
plot!(x -> -x*sqrt(-(a^2 + a*x - k)/(a*(a + x))), -1/8,0.99;
line=(:black,4))
scatter!([x₀], [y₀]; marker=(:circle,5,:yellow))
end
```
```{julia}
#| echo: false
plotly()
nothing
```
Graph of an equation with a well behaved section emphasized. The tangent line can be found by finding a formula for this well behaved section and differentiating *or* by implicit differentiation simply by assuming a form for the implicit function.
:::
With this assumption, asking what $dy/dx$ is has an obvious meaning - what is the slope of the tangent line to the graph at $(x,y)$. (The assumption eliminates the question of what a tangent line would mean when a graph self intersects.)
@@ -120,7 +156,7 @@ This says the slope of the tangent line depends on the point $(x,y)$ through the
As a check, we compare to what we would have found had we solved for $y= \sqrt{1 - x^2}$ (for $(x,y)$ with $y \geq 0$). We would have found: $dy/dx = 1/2 \cdot 1/\sqrt{1 - x^2} \cdot (-2x)$. Which can be simplified to $-x/y$. This should show that the method above - assuming $y$ is a function of $x$ and differentiating - is not only more general, but can even be easier.
The name - *implicit differentiation* - comes from the assumption that $y$ is implicitly defined in terms of $x$. According to the [Implicit Function Theorem](http://en.wikipedia.org/wiki/Implicit_function_theorem) the above method will work provided the curve has sufficient smoothness near the point $(x,y)$.
The name - *implicit differentiation* - comes from the assumption that $y$ is implicitly defined in terms of $x$. According to the [Implicit Function Theorem](http://en.wikipedia.org/wiki/Implicit_function_theorem) the above method will work provided the curve has sufficient smoothness near the point $(x,y)$. (Continuously differentiable and non vanishing derivative in $y$.)
##### Examples
@@ -140,10 +176,16 @@ For $a = 2, b=1$ we have the graph:
#| hold: true
a, b = 2, 1
f(x,y) = x^2*y + a * b * y - a^2 * x
implicit_plot(f)
implicit_plot(f; legend=false)
x₀, y₀ = 0, 0
m = (a^2 - 2x₀*y₀) / (a*b + x₀^2)
plot!(x -> y₀ + m*(x - x₀), -1, 1)
```
We can see that at each point in the viewing window the tangent line exists due to the smoothness of the curve. Moreover, at a point $(x,y)$ the tangent will have slope $dy/dx$ satisfying:
To the plot we added a tangent line at $(0,0)$.
We can see that at each point in the viewing window the tangent line exists due to the smoothness of the curve. To find the slope of the tangent line at a point $(x,y)$ the tangent line will have slope $dy/dx$ satisfying:
$$
@@ -177,7 +219,7 @@ A graph for $a=3$ shows why it has the name it does:
#| hold: true
a = 3
f(x,y) = x^4 - a^2*(x^2 - y^2)
implicit_plot(f)
implicit_plot(f; xticks=-5:5)
```
The tangent line at $(x,y)$ will have slope, $dy/dx$ satisfying:
@@ -341,7 +383,7 @@ The next step is solve for $dy/dx$ - the lone answer to the linear equation - wh
```{julia}
dydx = diff(u(x), x)
ex3 = solve(ex2, dydx)[1] # pull out lone answer with [1] indexing
ex3 = only(solve(ex2, dydx)) # pull out the only answer
```
As this represents an answer in terms of `u(x)`, we replace that term with the original variable:
@@ -369,9 +411,9 @@ Let $a = b = c = d = 1$, then $(1,4)$ is a point on the curve. We can draw a tan
```{julia}
H = ex(a=>1, b=>1, c=>1, d=>1)
x0, y0 = 1, 4
𝒎 = dydx₁(x=>1, y=>4, a=>1, b=>1, c=>1, d=>1)
m = dydx₁(x=>1, y=>4, a=>1, b=>1, c=>1, d=>1)
implicit_plot(lambdify(H); xlims=(-5,5), ylims=(-5,5), legend=false)
plot!(y0 + 𝒎 * (x-x0))
plot!(y0 + m * (x-x0))
```
Basically this includes all the same steps as if done "by hand." Some effort could have been saved in plotting, had values for the parameters been substituted initially, but not doing so shows their dependence in the derivative.
@@ -379,7 +421,7 @@ Basically this includes all the same steps as if done "by hand." Some effort cou
:::{.callout-warning}
## Warning
The use of `lambdify(H)` is needed to turn the symbolic expression, `H`, into a function.
The use of `lambdify(H)` is needed to turn the symbolic expression, `H`, into a function for plotting purposes.
:::
@@ -517,15 +559,9 @@ This could have been made easier, had we leveraged the result of the previous ex
#### Example: from physics
Many problems are best done with implicit derivatives. A video showing such a problem along with how to do it analytically is [here](http://ocw.mit.edu/courses/mathematics/18-01sc-single-variable-calculus-fall-2010/unit-2-applications-of-differentiation/part-b-optimization-related-rates-and-newtons-method/session-32-ring-on-a-string/).
This video starts with a simple question:
> If you have a rope and heavy ring, where will the ring position itself due to gravity?
This problem illustrates one best done with implicit derivatives. A video showing this problem along with how to do it analytically is [here](http://ocw.mit.edu/courses/mathematics/18-01sc-single-variable-calculus-fall-2010/unit-2-applications-of-differentiation/part-b-optimization-related-rates-and-newtons-method/session-32-ring-on-a-string/).
Well, suppose you hold the rope in two places, which we can take to be $(0,0)$ and $(a,b)$. Then let $(x,y)$ be all the possible positions of the ring that hold the rope taught. Then we have this picture:
@@ -534,19 +570,41 @@ Well, suppose you hold the rope in two places, which we can take to be $(0,0)$ a
```{julia}
#| hold: true
#| echo: false
let
gr()
P = (4,1)
Q = (1, -3)
scatter([0,4], [0,1], legend=false, xaxis=nothing, yaxis=nothing)
plot!([0,1,4],[0,-3,1])
𝑎, 𝑏= .05, .25
plot(;
axis=([],false),
legend=false)
scatter!([0,4], [0,1])
plot!([0,1,4],[0,-3,1]; line=(:black,2))
a, b = .05, .25
ts = range(0, 2pi, length=100)
plot!(1 .+ 𝑎*sin.(ts), -3 .+ 𝑏*cos.(ts), color=:gold)
annotate!((4-0.3,1,"(a,b)"))
plot!([0,1,1],[0,0,-3], color=:gray, alpha=0.25)
plot!([1,1,4],[0,1,1], color=:gray, alpha=0.25)
Δ = 0.15
annotate!([(1/2, 0-Δ, "x"), (5/2, 1 - Δ, "a-x"), (1-Δ, -1, "|y|"), (1+Δ, -1, "b-y")])
plot!(1 .+ a*sin.(ts), -3 .+ b*cos.(ts), line=(:gold,2))
plot!([0,1,1],[0,0,-3], color=:gray, alpha=0.75)
plot!([1,1,4],[0,1,1], color=:gray, alpha=0.75)
Δ = 0.05
annotate!([
(0,0, text(L"(0,0)",:bottom)),
(4,1, text(L"(a,b)",:bottom)),
(1/2, 0, text(L"x",:top)),
(5/2, 1, text(L"a-x", :top)),
(1, -1, text(L"|y|",:right)),
(1+Δ, -1, text(L"b-y",:left)),
(1+2a, -3, text(L"(x,y)",:left))
])
current()
end
```
```{julia}
#| echo: false
plotly()
nothing
```
Since the length of the rope does not change, we must have for any admissible $(x,y)$ that:

View File

@@ -28,17 +28,17 @@ We know this is $1$ using a bound from geometry, but might also guess this is on
$$
\sin(x) = x - \sin(\xi)x^2/2, \quad 0 < \xi < x.
\sin(x) = x - \sin(\xi)\frac{x^2}{2}, \quad 0 < \xi < x.
$$
This would yield:
$$
\lim_{x \rightarrow 0} \frac{\sin(x)}{x} = \lim_{x\rightarrow 0} \frac{x -\sin(\xi) x^2/2}{x} = \lim_{x\rightarrow 0} 1 - \sin(\xi) \cdot x/2 = 1.
\lim_{x \rightarrow 0} \frac{\sin(x)}{x} = \lim_{x\rightarrow 0} \frac{x -\sin(\xi) \frac{x^2}{2}}{x} = \lim_{x\rightarrow 0} 1 - \sin(\xi) \cdot \frac{x}{2} = 1.
$$
This is because we know $\sin(\xi) x/2$ has a limit of $0$, when $|\xi| \leq |x|$.
This is because we know $\sin(\xi) \frac{x}{2}$ has a limit of $0$, when $|\xi| \leq |x|$.
That doesn't look any easier, as we worried about the error term, but if just mentally replaced $\sin(x)$ with $x$ - which it basically is near $0$ - then we can see that the limit should be the same as $x/x$ which we know is $1$ without thinking.
@@ -384,10 +384,10 @@ the first equality by L'Hospital's rule, as the second limit exists.
Indeterminate forms of the type $0 \cdot \infty$, $0^0$, $\infty^\infty$, $\infty - \infty$ can be re-expressed to be in the form $0/0$ or $\infty/\infty$ and then L'Hospital's theorem can be applied.
###### Example: rewriting $0 \cdot \infty$
##### Example: rewriting $0 \cdot \infty$
What is the limit $x \log(x)$ as $x \rightarrow 0+$? The form is $0\cdot \infty$, rewriting, we see this is just:
What is the limit of $x \log(x)$ as $x \rightarrow 0+$? The form is $0\cdot \infty$, rewriting, we see this is just:
$$
@@ -401,10 +401,10 @@ $$
\lim_{x \rightarrow 0+}\frac{1/x}{-1/x^2} = \lim_{x \rightarrow 0+} -x = 0.
$$
###### Example: rewriting $0^0$
##### Example: rewriting $0^0$
What is the limit $x^x$ as $x \rightarrow 0+$? The expression is of the form $0^0$, which is indeterminate. (Even though floating point math defines the value as $1$.) We can rewrite this by taking a log:
What is the limit of $x^x$ as $x \rightarrow 0+$? The expression is of the form $0^0$, which is indeterminate. (Even though floating point math defines the value as $1$.) We can rewrite this by taking a log:
$$

View File

@@ -186,25 +186,45 @@ In each of these cases, a more complicated non-linear function is well approxim
#| 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
let
gr()
f(x) = sin(x)
a, b = -1/4, pi/2
p = plot(f, a, b, legend=false,
line=(3, :royalblue),
axis=([], 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, 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);
plot!(p, [1,1], [0, sin(1)], color=:green, linewidth=4);
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"])));
x₀ = 1.15
δ = 0.1
plot!(p, [x₀,x₀,1], [sin(1)/2-δ,0,0], line=(:black, 1, :dash), arrow=true)
plot!(p, [x₀,x₀,1], [sin(1)/2+δ,1, 1], line=(:black, 1, :dash), arrow=true)
plot!(p, [1/2 - 0.8δ, 0], [-δ, -δ]*3/4, line=(:black, 1, :dash), arrow=true)
plot!(p, [1/2 + 0.8δ, 1], [-δ, -δ]*3/4, line=(:black, 1, :dash), arrow=true)
scatter!([0], [0], marker=(5, :mediumorchid3))
annotate!(p, [
(0, f(0), text(L"(c, f(c))", :bottom, :right)),
(1/2, 0, text(L"\Delta x", :bottom)),
(1/2, 0, text(L"dx", :top)),
(1-0.02, sin(1)/2, text(L"Δ y", :right)),
(x₀, sin(1)/2, text(L"dy")),
(2/3, 2/3, text(L"m = \frac{dy}{dx} \approx \frac{\Delta y}{\Delta x}",
:bottom, rotation=33)) # why 33 and not 45?
])
p
end
```
```{julia}
#| echo: false
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.
@@ -480,7 +500,8 @@ 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 \cdot \frac{1}{2} \cdot \frac{f'(e) - f'(0)}{e} =
\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).
$$
@@ -551,7 +572,8 @@ Is it a coincidence that a basic algebraic operation with tangent lines approxim
$$
\begin{align*}
f(x) \cdot g(x) &= [f(c) + f'(c)(x-c) + \mathcal{O}((x-c)^2)] \cdot [g(c) + g'(c)(x-c) + \mathcal{O}((x-c)^2)]\\
&=[f(c) + f'(c)(x-c)] \cdot [g(c) + g'(c)(x-c)] + (f(c) + f'(c)(x-c)) \cdot \mathcal{O}((x-c)^2) + (g(c) + g'(c)(x-c)) \cdot \mathcal{O}((x-c)^2) + [\mathcal{O}((x-c)^2)]^2\\
&=[f(c) + f'(c)(x-c)] \cdot [g(c) + g'(c)(x-c)] \\
&+ (f(c) + f'(c)(x-c)) \cdot \mathcal{O}((x-c)^2) + (g(c) + g'(c)(x-c)) \cdot \mathcal{O}((x-c)^2) + [\mathcal{O}((x-c)^2)]^2\\
&= [f(c) + f'(c)(x-c)] \cdot [g(c) + g'(c)(x-c)] + \mathcal{O}((x-c)^2)\\
&= f(c) \cdot g(c) + [f'(c)\cdot g(c) + f(c)\cdot g'(c)] \cdot (x-c) + [f'(c)\cdot g'(c) \cdot (x-c)^2 + \mathcal{O}((x-c)^2)] \\
&= f(c) \cdot g(c) + [f'(c)\cdot g(c) + f(c)\cdot g'(c)] \cdot (x-c) + \mathcal{O}((x-c)^2)
@@ -630,7 +652,7 @@ Automatic differentiation (forward mode) essentially uses this technique. A "dua
```{julia}
Dual(0, 1)
x = Dual(0, 1)
```
Then what is $x$? It should reflect both $(\sin(0), \cos(0))$ the latter being the derivative of $\sin$. We can see this is *almost* what is computed behind the scenes through:
@@ -638,11 +660,13 @@ Then what is $x$? It should reflect both $(\sin(0), \cos(0))$ the latter being t
```{julia}
#| hold: true
x = Dual(0, 1)
@code_lowered sin(x)
```
This output of `@code_lowered` can be confusing, but this simple case needn't be. Working from the end we see an assignment to a variable named `%3` of `Dual(%6, %12)`. The value of `%6` is `sin(x)` where `x` is the value `0` above. The value of `%12` is `cos(x)` *times* the value `1` above (the `xp`), which reflects the *chain* rule being used. (The derivative of `sin(u)` is `cos(u)*du`.) So this dual number encodes both the function value at `0` and the derivative of the function at `0`.
This output of `@code_lowered` can be confusing, but this simple case needn't be, as we know what to look for: we need to evaluate `sin` at `1` and carry along the derivative `cos(x)` **times** the derivative at `x`.
The `sin` is computed in `%6` and is passed to `Dual` in `%13` as the first arguments. The `cos` is computed in `%11` and then *multiplied* in `%` by `xp`, which holds the derivative information about `x`. This is passed as the second argument to `Dual` in `%13`.
Similarly, we can see what happens to `log(x)` at `1` (encoded by `Dual(1,1)`):
@@ -654,14 +678,15 @@ x = Dual(1, 1)
@code_lowered log(x)
```
We can see the derivative again reflects the chain rule, it being given by `1/x * xp` where `xp` acts like `dx` (from assignments `%9` and `%8`). Comparing the two outputs, we see only the assignment to `%9` differs, it reflecting the derivative of the function.
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 begin 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.
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)$.
@@ -692,6 +717,7 @@ Call $R$ the intersection point of the two normal lines:
#| echo: false
using Roots
let
gr()
f(x) = x^4
fp(x) = 4x^3
c = 1/4
@@ -706,12 +732,17 @@ let
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)])
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$?
@@ -760,6 +791,7 @@ This formula for $r$ is known as the radius of curvature of $f$ -- the radius of
```{julia}
#| echo: false
let
gr()
f(x) = x^4
fp(x) = 4x^3
fpp(x) = 12x^2
@@ -779,8 +811,8 @@ let
scatter!([c], f.([c]))
scatter!([Rx], [nlc(Rx)])
annotate!([(c, f(c), "(c,f(c))",:top),
(Rx, nlc(Rx), "R",:left)])
annotate!([(c, f(c), L"(c,f(c))",:top),
(Rx, nlc(Rx), L"R",:left)])
Delta = pi/10
@@ -801,6 +833,12 @@ let
end
```
```{julia}
#| echo: false
plotly()
nothing
```
## Questions

View File

@@ -277,6 +277,13 @@ For $f$ differentiable on $(a,b)$ and continuous on $[a,b]$, if $f(a)=f(b)$, the
:::
::: {#fig-l-hospital-144}
![Figure from L'Hospital's calculus book](figures/lhopital-144.png)
Figure from L'Hospital's calculus book showing Rolle's theorem where $c=E$ in the labeling.
:::
This modest observation opens the door to many relationships between a function and its derivative, as it ties the two together in one statement.
@@ -323,46 +330,71 @@ The mean value theorem is a direct generalization of Rolle's theorem.
::: {.callout-note icon=false}
## Mean value theorem
Let $f(x)$ be differentiable on $(a,b)$ and continuous on $[a,b]$. Then there exists a value $c$ in $(a,b)$ where $f'(c) = (f(b) - f(a)) / (b - a)$.
Let $f(x)$ be differentiable on $(a,b)$ and continuous on $[a,b]$. Then there exists a value $c$ in $(a,b)$ where
$$
f'(c) = (f(b) - f(a)) / (b - a).
$$
:::
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]$).
Figure @fig-mean-value-theorem illustrates the theorem. The blue 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 brown.
@fig-mean-value-theorem illustrates the theorem. The secant line between $a$ and $b$ is dashed. For this function there are two values of $c$ where the slope of the tangent line is seen to be the same as the slope of this secant line. At least one is guaranteed by the theorem.
```{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)
let
# mean value theorem
gr()
f(x) = x^3 -4x^2 + 3x - 1
a, b = -3/4, 3+3/4
plot(; axis=([], nothing),
legend=false,
xlims=(-1.1,4),
framestyle=:none)
y₀ = 0.3 + f(-1)
p = plot(f, a-0.75, b+1,
color=:mediumorchid3,
linewidth=3, legend=false,
axis=([],false),
)
plot!(f, -1, 4; line=(:black, 2))
plot!([-1.1, 4], y₀*[1,1]; line=(:black, 1), arrow=true, head=:top)
p,q = (a,f(a)), (b, f(b))
scatter!([p,q]; marker=(:circle, 4, :red))
plot!([p,q]; line=(:gray, 2, :dash))
m = (f(b) - f(a))/(b-a)
c₁, c₂ = find_zeros(x -> f'(x) - m, (a,b))
Δ = 2/3
for c ∈ (c₁, c₂)
plot!(tangent(f,c), c-Δ, c+Δ; line=(:gray, 2))
plot!([(c, y₀), (c, f(c))]; line=(:gray, 1, :dash))
end
for c ∈ (a,b)
plot!([(c, y₀), (c, f(c))]; line=(:gray, 1))
end
annotate!([
(a, y₀, text(L"a", :top)),
(b, y₀, text(L"b", :top)),
(c₁, y₀, text(L"c_1", :top)),
(c₂, y₀, text(L"c_2", :top)),
])
current()
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=:brown3)
end
```
scatter!(cps, f.(cps))
subsscripts = collect("₀₁₂₃₄₅₆₇₈₉")
annotate!([(cp, f(cp), text("c"*subsscripts[i], :bottom)) for (i,cp) ∈ enumerate(cps)])
p
```{julia}
#| echo: false
plotly()
nothing
```
Like Rolle's theorem this is a guarantee that something exists, not a recipe to find it. In fact, the mean value theorem is just Rolle's theorem applied to:
@@ -506,7 +538,7 @@ function parametric_fns_graph(n)
xlim=(-1.1,1.1), ylim=(-pi/2-.1, pi/2+.1))
scatter!(plt, [f(ts[end])], [g(ts[end])], color=:orange, markersize=5)
val = @sprintf("% 0.2f", ts[end])
annotate!(plt, [(0, 1, "t = $val")])
annotate!(plt, [(0, 1, L"t = %$val")])
end
caption = L"""

View File

@@ -315,10 +315,10 @@ One way to think about this is the difference between `x` and the next largest f
For the specific example, `abs(b-a) <= 2eps(m)` means that the gap between `a` and `b` is essentially 2 floating point values from the $x$ value with the smallest $f(x)$ value.
For bracketing methods that is about as good as you can get. However, once floating values are understood, the absolute best you can get for a bracketing interval would be
For bracketing methods that is about as good as you can get. However, once floating point values are understood, the absolute best you can get for a bracketing interval would be
* along the way, a value `f(c)` is found which is *exactly* `0.0`
* along the way, a value `f(c)` is found which evaluates *exactly* to `0.0`
* the endpoints of the bracketing interval are *adjacent* floating point values, meaning the interval can not be bisected and `f` changes sign between the two values.
@@ -334,6 +334,8 @@ chandrapatla(fu, -9, 1, λ3)
Here the issue is `abs(b-a)` is tiny (of the order `1e-119`) but `eps(m)` is even smaller.
> For checking if $x_n \approx x_{n+1}$ both a relative and absolute error should be used unless something else is known.
For non-bracketing methods, like Newton's method or the secant method, different criteria are useful. There may not be a bracketing interval for `f` (for example `f(x) = (x-1)^2`) so the second criteria above might need to be restated in terms of the last two iterates, $x_n$ and $x_{n-1}$. Calling this difference $\Delta = |x_n - x_{n-1}|$, we might stop if $\Delta$ is small enough. As there are scenarios where this can happen, but the function is not at a zero, a check on the size of $f$ is needed.
@@ -347,7 +349,7 @@ First if `f(x_n)` is `0.0` then it makes sense to call `x_n` an *exact zero* of
However, there may never be a value with `f(x_n)` exactly `0.0`. (The value of `sin(1pi)` is not zero, for example, as `1pi` is an approximation to $\pi$, as well the `sin` of values adjacent to `float(pi)` do not produce `0.0` exactly.)
Suppose `x_n` is the closest floating number to $\alpha$, the zero. Then the relative rounding error, $($ `x_n` $- \alpha)/\alpha$, will be a value $\delta$ with $\delta$ less than `eps()`.
Suppose `x_n` is the closest floating point number to $\alpha$, the zero. Then the relative rounding error, $($ `x_n` $- \alpha)/\alpha$, will be a value $\delta$ with $\delta$ less than `eps()`.
How far then can `f(x_n)` be from $0 = f(\alpha)$?
@@ -364,10 +366,11 @@ $$
f(x_n) \approx f(\alpha) + f'(\alpha) \cdot (\alpha\delta) = f'(\alpha) \cdot \alpha \delta
$$
So we should consider `f(x_n)` an *approximate zero* when it is on the scale of $f'(\alpha) \cdot \alpha \delta$.
So we should consider `f(x_n)` an *approximate zero* when it is on the scale of $f'(\alpha) \cdot \alpha \delta$. That $\alpha$ factor means we consider a *relative* tolerance for `f`.
> For checking if $f(x_n) \approx 0$ both a relative and absolute error should be used--the relative error involving the size of $x_n$.
That $\alpha$ factor means we consider a *relative* tolerance for `f`. Also important when `x_n` is close to `0`, is the need for an *absolute* tolerance, one not dependent on the size of `x`. So a good condition to check if `f(x_n)` is small is
A good condition to check if `f(x_n)` is small is
`abs(f(x_n)) <= abs(x_n) * rtol + atol`, or `abs(f(x_n)) <= max(abs(x_n) * rtol, atol)`
@@ -396,7 +399,7 @@ It is not uncommon to assign `rtol` to have a value like `sqrt(eps())` to accoun
In Part III of @doi:10.1137/1.9781611977165 we find language of numerical analysis useful to formally describe the zero-finding problem. Key concepts are errors, conditioning, and stability. These give some theoretical justification for the tolerances above.
Abstractly a *problem* is a mapping, $F$, from a domain $X$ of data to a range $Y$ of solutions. Both $X$ and $Y$ have a sense of distance given by a *norm*. A norm is a generalization of the absolute value and gives quantitative meaning to terms like small and large.
Abstractly a *problem* is a mapping, $F$, from a domain $X$ of data to a range $Y$ of solutions. Both $X$ and $Y$ have a sense of distance given by a *norm*. A norm (denoted with $\lVert\cdot\rVert$) is a generalization of the absolute value and gives quantitative meaning to terms like small and large.
> A *well-conditioned* problem is one with the property that all small perturbations of $x$ lead to only small changes in $F(x)$.
@@ -435,7 +438,7 @@ $$
\tilde{F}(x) = F(\tilde{x})
$$
for some $\tilde{x}$ with $\lVert\tilde{x} - x\rVert/\lVert x\rVert$ is small.
for some $\tilde{x}$ where $\lVert\tilde{x} - x\rVert/\lVert x\rVert$ is small.
> "A backward stable algorithm gives exactly the right answer to nearly the right question."

View File

@@ -413,7 +413,7 @@ To machine tolerance the answer is a zero, even though the exact answer is irrat
The first example by Newton of applying the method to a non-polynomial function was solving an equation from astronomy: $x - e \sin(x) = M$, where $e$ is an eccentric anomaly and $M$ a mean anomaly. Newton used polynomial approximations for the trigonometric functions, here we can solve directly.
Let $e = 1/2$ and $M = 3/4$. With $f(x) = x - e\sin(x) - M$ then $f'(x) = 1 - e cos(x)$. Starting at 1, Newton's method for 3 steps becomes:
Let $e = 1/2$ and $M = 3/4$. With $f(x) = x - e\sin(x) - M$ then $f'(x) = 1 - e \cos(x)$. Starting at 1, Newton's method for 3 steps becomes:
```{julia}
ec, M = 0.5, 0.75
@@ -490,7 +490,7 @@ $$
x_{i+1} = x_i - (1/x_i - q)/(-1/x_i^2) = -qx^2_i + 2x_i.
$$
Now for $q$ in the interval $[1/2, 1]$ we want to get a *good* initial guess. Here is a claim. We can use $x_0=48/17 - 32/17 \cdot q$. Let's check graphically that this is a reasonable initial approximation to $1/q$:
Now for $q$ in the interval $[1/2, 1]$ we want to get a *good* initial guess. Here is a claim: we can use $x_0=48/17 - 32/17 \cdot q$. Let's check graphically that this is a reasonable initial approximation to $1/q$:
```{julia}
@@ -865,7 +865,7 @@ The function $f(x) = x^{20} - 1$ has two bad behaviours for Newton's
method: for $x < 1$ the derivative is nearly $0$ and for $x>1$ the
second derivative is very big. In this illustration, we have an
initial guess of $x_0=8/9$. As the tangent line is fairly flat, the
next approximation is far away, $x_1 = 1.313\dots$. As this guess
next approximation is far away, $x_1 = 1.313\dots$. As this guess
is much bigger than $1$, the ratio $f(x)/f'(x) \approx
x^{20}/(20x^{19}) = x/20$, so $x_i - f(x_i)/f'(x_i) \approx (19/20)x_i$
yielding slow, linear convergence until $f''(x_i)$ is moderate. For

View File

@@ -70,7 +70,7 @@ function perimeter_area_graphic_graph(n)
size=fig_size,
xlim=(0,10), ylim=(0,10))
scatter!(plt, [w], [h], color=:orange, markersize=5)
annotate!(plt, [(w/2, h/2, "Area=$(round(w*h,digits=1))")])
annotate!(plt, [(w/2, h/2, L"Area$=\; %$(round(w*h,digits=1))$")])
plt
end
@@ -79,7 +79,7 @@ caption = """
Some possible rectangles that satisfy the constraint on the perimeter and their area.
"""
n = 6
n = 5
anim = @animate for i=1:n
perimeter_area_graphic_graph(i-1)
end
@@ -187,8 +187,11 @@ ts = range(0, stop=pi, length=50)
x1,y1 = 4, 4.85840
x2,y2 = 3, 6.1438
delta = 4
p = plot(delta .+ x1*[0, 1,1,0], y1*[0,0,1,1], linetype=:polygon, fillcolor=:blue, legend=false)
plot!(p, x2*[0, 1,1,0], y2*[0,0,1,1], linetype=:polygon, fillcolor=:blue)
p = plot(delta .+ x1*[0, 1,1,0], y1*[0,0,1,1];
linetype=:polygon, fillcolor=:blue, legend=false,
aspect_ratio=:equal)
plot!(p, x2*[0, 1,1,0], y2*[0,0,1,1];
linetype=:polygon, fillcolor=:blue)
plot!(p, delta .+ x1/2 .+ x1/2*cos.(ts), y1.+x1/2*sin.(ts), linetype=:polygon, fillcolor=:red)
plot!(p, x2/2 .+ x2/2*cos.(ts), y2 .+ x2/2*sin.(ts), linetype=:polygon, fillcolor=:red)
@@ -308,7 +311,7 @@ A₀ = w₀ * h₀ + pi * (w₀/2)^2 / 2
Perim = 2*h₀ + w₀ + pi * w₀/2
h₁ = solve(Perim - 20, h₀)[1]
A₁ = A₀(h₀ => h₁)
w₁ = solve(diff(A₁,w₀), w₀)[1]
w₁ = solve(diff(A₁,w₀) ~ 0, w₀)[1]
```
We know that `w₀` is the maximum in this example from our previous work. We shall see soon, that just knowing that the second derivative is negative at `w₀` would suffice to know this. Here we check that condition:
@@ -392,14 +395,29 @@ The figure shows a ladder of length $l_1 + l_2$ that got stuck - it was too long
```{julia}
#| hold: true
#| echo: false
p = plot([0, 0, 15], [15, 0, 0], color=:blue, legend=false)
plot!(p, [5, 5, 15], [15, 8, 8], color=:blue)
plot!(p, [0,14.53402874075368], [12.1954981558864, 0], linewidth=3)
plot!(p, [0,5], [8,8], color=:orange)
plot!(p, [5,5], [0,8], color=:orange)
annotate!(p, [(13, 1/2, "θ"),
(2.5, 11, "l₂"), (10, 5, "l₁"), (2.5, 7.0, "l₂ ⋅ cos(θ)"),
(5.1, 4, "l₁ ⋅ sin(θ)")])
let
gr()
p = plot([0, 0, 15], [15, 0, 0],
xticks = [0,5, 15],
yticks = [0,8, 12],
line=(:blue, 2),
legend=false)
plot!(p, [5, 5, 15], [15, 8, 8]; line=(:blue,2))
plot!(p, [0,14.53402874075368], [12.1954981558864, 0], linewidth=3)
plot!(p, [0,5], [8,8], color=:orange)
plot!(p, [5,5], [0,8], color=:orange)
annotate!(p, [(13, 1/2, L"\theta"),
(2.5, 11, L"l_2"),
(10, 5, L"l_1"),
(2.5, 7.0, L"l_2 \cos(\theta)"),
(5.1, 4, text(L"l_1 \sin(\theta)", :top,rotation=90))])
end
```
```{julia}
#| echo: false
plotly()
nothing
```
We approach this problem in reverse. It is easy to see when a ladder is too long. It gets stuck at some angle $\theta$. So for each $\theta$ we find that ladder length that is just too long. Then we find the minimum length of all these ladders that are too long. If a ladder is this length or more it will get stuck for some angle. However, if it is less than this length it will not get stuck. So to maximize a ladder length, we minimize a different function. Neat.
@@ -834,10 +852,12 @@ A rancher with $10$ meters of fence wishes to make a pen adjacent to an existing
```{julia}
#| hold: true
#| echo: false
p = plot(; legend=false, aspect_ratio=:equal, axis=nothing, border=:none)
p = plot(; legend=false, aspect_ratio=:equal, axis=nothing, border=:none)
plot!([0,10, 10, 0, 0], [0,0,10,10,0]; linewidth=3)
plot!(p, [10,14,14,10], [2, 2, 8,8]; linewidth = 1)
annotate!(p, [(15, 5, "x"), (12,1, "y")])
annotate!(p, [(14-0.1, 5, text("x", :right)), (12,2, text("y",:bottom))])
p
```
@@ -1353,7 +1373,12 @@ p = 1/2
x = a/p
plot!(plt, [0, b*(1+p), 0, 0], [0, 0, a+x, 0])
plot!(plt, [b,b,0,0],[0,a,a,0])
annotate!(plt, [(b/2,0, "b"), (0,a/2,"a"), (0,a+x/2,"x"), (b+b*p/2,0,"bp")])
annotate!(plt, [
(b/2,0, text("b",:top)),
(0,a/2, text("a",:right)),
(0,a+x/2, text("x",:right)),
(b+b*p/2,0, text("bp",:top))
])
plt
```

View File

@@ -18,7 +18,7 @@ using SymPy
---
Related rates problems involve two (or more) unknown quantities that are related through an equation. As the two variables depend on each other, also so do their rates - change with respect to some variable which is often time, though exactly how remains to be discovered. Hence the name "related rates."
Related rates problems involve two (or more) unknown quantities that are related through an equation. As the two variables depend on each other, also so do their rates - change with respect to some variable which is often time. Exactly how remains to be discovered. Hence the name "related rates."
#### Examples
@@ -27,7 +27,7 @@ Related rates problems involve two (or more) unknown quantities that are related
The following is a typical "book" problem:
> A screen saver displays the outline of a $3$ cm by $2$ cm rectangle and then expands the rectangle in such a way that the $2$ cm side is expanding at the rate of $4$ cm/sec and the proportions of the rectangle never change. How fast is the area of the rectangle increasing when its dimensions are $12$ cm by $8$ cm? [Source.](http://oregonstate.edu/instruct/mth251/cq/Stage9/Practice/ratesProblems.html)
> A *vintage* screen saver displays the outline of a $3$ cm by $2$ cm rectangle and then expands the rectangle in such a way that the $2$ cm side is expanding at the rate of $4$ cm/sec and the proportions of the rectangle never change. How fast is the area of the rectangle increasing when its dimensions are $12$ cm by $8$ cm? [Source.](http://oregonstate.edu/instruct/mth251/cq/Stage9/Practice/ratesProblems.html)
@@ -125,7 +125,7 @@ w(t) = 2 + 4*t
```{julia}
h(t) = 3/2 * w(t)
h(t) = 3 * w(t) / 2
```
This means again that area depends on $t$ through this formula:
@@ -198,6 +198,50 @@ A ladder, with length $l$, is leaning against a wall. We parameterize this probl
If the ladder starts to slip away at the base, but remains in contact with the wall, express the rate of change of $h$ with respect to $t$ in terms of $db/dt$.
```{julia}
#| echo: false
let
gr()
l = 12
b = 6
h = sqrt(l^2 - b^2)
plot(;
axis=([],false),
legend=false,
aspect_ratio=:equal)
P,Q = (0,h),(b,0)
w = 0.2
S = Shape([-w,0,0,-w],[0,0,h+1,h+1])
plot!(S; fillstyle=:/, fillcolor=:gray80, fillalpha=0.5)
R = Shape([-w,b+2,b+2,-w],[-w,-w,0,0])
plot!(R, fill=(:gray, 0.25))
plot!([P,Q]; line=(:black, 2))
scatter!([P,Q])
b = b + 3/2
h = sqrt(l^2 - b^2)
plot!([b,b],[0,0]; arrow=true, side=:head, line=(:blue, 3))
plot!([0,0], [h,h]; arrow=true, side=:head, line=(:blue, 3))
annotate!([
(b,-w,text(L"(b(t),0)",:top)),
(-w, h, text(L"(0,h(t))", :bottom, rotation=90)),
(b/2, h/2, text(L"L", rotation = -atand(h,b), :bottom))
])
current()
end
```
```{julia}
#| echo: false
plotly()
nothing
```
We have from implicitly differentiating in $t$ the equation $l^2 = h^2 + b^2$, noting that $l$ is a constant, that:
@@ -236,7 +280,7 @@ As $b$ goes to $l$, $h$ goes to $0$, so $b/h$ blows up. Unless $db/dt$ goes to $
:::{.callout-note}
## Note
Often, this problem is presented with $db/dt$ having a constant rate. In this case, the ladder problem defies physics, as $dh/dt$ eventually is faster than the speed of light as $h \rightarrow 0+$. In practice, were $db/dt$ kept at a constant, the ladder would necessarily come away from the wall. The trajectory would follow that of a tractrix were there no gravity to account for.
Often, this problem is presented with $db/dt$ having a constant rate. In this case, the ladder problem defies physics, as $dh/dt$ eventually is faster than the speed of light as $h \rightarrow 0+$. In practice, were $db/dt$ kept at a constant, the ladder would necessarily come away from the wall.
:::
@@ -247,12 +291,15 @@ Often, this problem is presented with $db/dt$ having a constant rate. In this ca
```{julia}
#| hold: true
#| echo: false
#| eval: false
caption = "A man and woman walk towards the light."
imgfile = "figures/long-shadow-noir.png"
ImageFile(:derivatives, imgfile, caption)
```
![A man and woman walk towards the light](./figures/long-shadow-noir.png)
Shadows are a staple of film noir. In the photo, suppose a man and a woman walk towards a street light. As they approach the light the length of their shadow changes.
@@ -340,7 +387,7 @@ This can be solved for the unknown: $dx/dt = 50/20$.
A batter hits a ball toward third base at $75$ ft/sec and runs toward first base at a rate of $24$ ft/sec. At what rate does the distance between the ball and the batter change when $2$ seconds have passed?
We will answer this with `SymPy`. First we create some symbols for the movement of the ball towards third base, `b(t)`, the runner toward first base, `r(t)`, and the two velocities. We use symbolic functions for the movements, as we will be differentiating them in time:
We will answer this symbolically. First we create some symbols for the movement of the ball towards third base, `b(t)`, the runner toward first base, `r(t)`, and the two velocities. We use symbolic functions for the movements, as we will be differentiating them in time:
```{julia}

View File

@@ -42,12 +42,14 @@ gr()
taylor(f, x, c, n) = series(f, x, c, n+1).removeO()
function make_taylor_plot(u, a, b, k)
k = 2k
plot(u, a, b, title="plot of T_$k", linewidth=5, legend=false, size=fig_size, ylim=(-2,2.5))
if k == 1
plot!(zero, range(a, stop=b, length=100))
else
plot!(taylor(u, x, 0, k), range(a, stop=b, length=100))
end
plot(u, a, b;
title = L"plot of $T_{%$k}$",
line = (:black, 3),
legend = false,
size = fig_size,
ylim = (-2,2.5))
fn = k == 1 ? zero : taylor(u, x, 0, k)
plot!(fn, range(a, stop=b, length=100); line=(:red,2))
end
@@ -76,7 +78,7 @@ ImageFile(imgfile, caption)
## The secant line and the tangent line
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.
Heads up: we approach this general problem **much** more indirectly than is needed by introducing 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 +104,10 @@ $$
tl(x) = f(c) + f'(c) \cdot(x - c).
$$
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:
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:
$$
@@ -189,7 +194,7 @@ function divided_differences(f, x, xs...)
end
```
In the following, by adding a `getindex` method, we enable the `[]` notation of Newton to work with symbolic functions, like `u()` defined below, which is used in place of $f$:
In the following--even though it is *type piracy*--by adding a `getindex` method, we enable the `[]` notation of Newton to work with symbolic functions, like `u()` defined below, which is used in place of $f$:
```{julia}
@@ -199,48 +204,38 @@ Base.getindex(u::SymFunction, xs...) = divided_differences(u, xs...)
ex = u[c, c+h]
```
We can take a limit and see the familiar (yet differently represented) value of $u'(c)$:
A limit as $h\rightarrow 0$ would show a value of $u'(c)$.
```{julia}
limit(ex, h => 0)
```
The choice of points is flexible. Here we use $c-h$ and $c+h$:
```{julia}
limit(u[c-h, c+h], h=>0)
```
Now, let's look at:
```{julia}
ex₂ = u[c, c+h, c+2h]
simplify(ex₂)
```
Not so bad after simplification. The limit shows this to be an approximation to the second derivative divided by $2$:
If multiply by $2$ and simplify, a discrete approximation for the second derivative--the second order forward [difference equation](http://tinyurl.com/n4235xy)--is seen:
```{julia}
limit(ex₂, h => 0)
simplify(2ex₂)
```
(The expression is, up to a divisor of $2$, the second order forward [difference equation](http://tinyurl.com/n4235xy), a well-known approximation to $f''$.)
This relationship between higher-order divided differences and higher-order derivatives generalizes. This is expressed in this [theorem](http://tinyurl.com/zjogv83):
> Suppose $m=x_0 < x_1 < x_2 < \dots < x_n=M$ are distinct points. If $f$ has $n$ continuous derivatives then there exists a value $\xi$, where $m < \xi < M$, satisfying:
:::{.callout-note}
## Mean value theorem for Divided differences
Suppose $m=x_0 < x_1 < x_2 < \dots < x_n=M$ are distinct points. If $f$ has $n$ continuous derivatives then there exists a value $\xi$, where $m < \xi < M$, satisfying:
$$
f[x_0, x_1, \dots, x_n] = \frac{1}{n!} \cdot f^{(n)}(\xi).
$$
:::
This immediately applies to the above, where we parameterized by $h$: $x_0=c, x_1=c+h, x_2 = c+2h$. For then, as $h$ goes to $0$, it must be that $m, M \rightarrow c$, and so the limit of the divided differences must converge to $(1/2!) \cdot f^{(2)}(c)$, as $f^{(2)}(\xi)$ converges to $f^{(2)}(c)$.
@@ -496,16 +491,20 @@ f[x_0] &+ f[x_0,x_1] \cdot (x - x_0) + f[x_0, x_1, x_2] \cdot (x - x_0)\cdot(x-x
$$
and taking $x_i = c + i\cdot h$, for a given $n$, we have in the limit as $h > 0$ goes to zero that coefficients of this polynomial converge to the coefficients of the *Taylor Polynomial of degree n*:
and taking $x_i = c + i\cdot h$, for a given $n$, we have in the limit as $h > 0$ goes to zero that coefficients of this polynomial converge:
:::{.callout-note}
## Taylor polynomial of degree $n$
Suppose $f(x)$ has $n+1$ derivatives (continuous on $c$ and $x$), then
$$
f(c) + f'(c)\cdot(x-c) + \frac{f''(c)}{2!}(x-c)^2 + \cdots + \frac{f^{(n)}(c)}{n!} (x-c)^n.
T_n(x) = f(c) + f'(c)\cdot(x-c) + \frac{f''(c)}{2!}(x-c)^2 + \cdots + \frac{f^{(n)}(c)}{n!} (x-c)^n,
$$
This polynomial will be the best approximation of degree $n$ or less to the function $f$, near $c$. The error will be given - again by an application of the Cauchy mean value theorem:
will be the best approximation of degree $n$ or less to $f$, near $c$.
The error will be given - again by an application of the Cauchy mean value theorem:
$$
@@ -513,9 +512,10 @@ $$
$$
for some $\xi$ between $c$ and $x$.
:::
The Taylor polynomial for $f$ about $c$ of degree $n$ can be computed by taking $n$ derivatives. For such a task, the computer is very helpful. In `SymPy` the `series` function will compute the Taylor polynomial for a given $n$. For example, here is the series expansion to 10 terms of the function $\log(1+x)$ about $c=0$:
The Taylor polynomial for $f$ about $c$ of degree $n$ can be computed by taking $n$ derivatives. For such a task, the computer is very helpful. In `SymPy` the `series` function will compute the Taylor polynomial for a given $n$. For example, here is the series expansion to $10$ terms of the function $\log(1+x)$ about $c=0$:
```{julia}
@@ -803,15 +803,7 @@ Now, $2s = m - s\cdot m$, so the above can be reworked to be $\log(1+m) = m - s\
(For larger values of $m$, a similar, but different approximation, can be used to minimize floating point errors.)
How big can the error be between this *approximations* and $\log(1+m)$? We plot to see how big $s$ can be:
```{julia}
@syms v
plot(v/(2+v), sqrt(2)/2 - 1, sqrt(2)-1)
```
This shows, $s$ is as big as
How big can the error be between this *approximations* and $\log(1+m)$? The expression $m/(2+m)$ increases for $m > 0$, so, on this interval $s$ is as big as
```{julia}
@@ -822,17 +814,17 @@ The error term is like $2/19 \cdot \xi^{19}$ which is largest at this value of
```{julia}
(2/19)*Max^19
(2/19) * Max^19
```
Basically that is machine precision. Which means, that as far as can be told on the computer, the value produced by $2s + s \cdot p$ is about as accurate as can be done.
To try this out to compute $\log(5)$. We have $5 = 2^2(1+0.25)$, so $k=2$ and $m=0.25$.
We try this out to compute $\log(5)$. We have $5 = 2^2(1+ 1/4)$, so $k=2$ and $m=1/4$.
```{julia}
k, m = 2, 0.25
k, m = 2, 1/4
s = m / (2+m)
pₗ = 2 * sum(s^(2i)/(2i+1) for i in 1:8) # where the polynomial approximates the logarithm...
@@ -1209,7 +1201,10 @@ $$
$$
h(x)=b_0 + b_1 (x-x_n) + b_2(x-x_n)(x-x_{n-1}) + \cdots + b_n (x-x_n)(x-x_{n-1})\cdot\cdots\cdot(x-x_1).
\begin{align*}
h(x)&=b_0 + b_1 (x-x_n) + b_2(x-x_n)(x-x_{n-1}) + \cdots \\
&+ b_n (x-x_n)(x-x_{n-1})\cdot\cdots\cdot(x-x_1).
\end{align*}
$$
These two polynomials are of degree $n$ or less and have $u(x) = h(x)-g(x)=0$, by uniqueness. So the coefficients of $u(x)$ are $0$. We have that the coefficient of $x^n$ must be $a_n-b_n$ so $a_n=b_n$. Our goal is to express $a_n$ in terms of $a_{n-1}$ and $b_{n-1}$. Focusing on the $x^{n-1}$ term, we have:

View File

@@ -1,3 +1,7 @@
---
engine: julia
---
# Differential vector calculus
This section discussions generalizations of the derivative to functions which have more than one input and/or one output.

View File

@@ -18,6 +18,7 @@ QuadGK = "1fd47b50-473d-5c70-9696-f719f8f3bcdc"
QuizQuestions = "612c44de-1021-4a21-84fb-7261cf5eb2d4"
Roots = "f2b01f46-fcfa-551c-844a-d8ac1e96c665"
ScatteredInterpolation = "3f865c0f-6dca-5f4d-999b-29fe1e7e3c92"
SplitApplyCombine = "03a91e81-4c3e-53e1-a0a4-9c0c8f19dd66"
SymPy = "24249f21-da20-56a4-8eb1-6a02cf4ae2e6"
Tables = "bd369af6-aec1-5ad0-b16a-f7cc5008161c"
TextWrap = "b718987f-49a8-5099-9789-dcd902bef87d"

View File

@@ -9,6 +9,7 @@ files = (
"scalar_functions",
"scalar_functions_applications",
"vector_fields",
"matrix_calculus_notes.qmd",
"plots_plotting",
)

View File

@@ -80,6 +80,7 @@ Vector fields are also useful for other purposes, such as transformations, examp
For transformations, a useful visualization is to plot curves where one variables is fixed. Consider the transformation from polar coordinates to cartesian coordinates $F(r, \theta) = r \langle\cos(\theta),\sin(\theta)\rangle$. The following plot will show in blue fixed values of $r$ (circles) and in red fixed values of $\theta$ (rays).
::: {#fig-transformation-partial-derivative}
```{julia}
#| hold: true
@@ -97,10 +98,21 @@ pt = [1, pi/4]
J = ForwardDiff.jacobian(F, pt)
arrow!(F(pt...), J[:,1], linewidth=5, color=:red)
arrow!(F(pt...), J[:,2], linewidth=5, color=:blue)
pt = [0.5, pi/8]
J = ForwardDiff.jacobian(F, pt)
arrow!(F(pt...), J[:,1], linewidth=5, color=:red)
arrow!(F(pt...), J[:,2], linewidth=5, color=:blue)
```
Plot of a vector field from $R^2 \rightarrow R^2$ illustrated by drawing curves with fixed $r$ and $\theta$. The partial derivatives are added as layers.
:::
To the plot, we added the partial derivatives with respect to $r$ (in red) and with respect to $\theta$ (in blue). These are found with the soon-to-be discussed Jacobian. From the graph, you can see that these vectors are tangent vectors to the drawn curves.
The curves form a non-rectangular grid. Were the cells exactly parallelograms, the area would be computed taking into account the length of the vectors and the angle between them -- the same values that come out of a cross product.
## Parametrically defined surfaces
@@ -138,7 +150,7 @@ When a surface is described as a level curve, $f(x,y,z) = c$, then the gradient
When a surface is described parametrically, there is no "gradient." The *partial* derivatives are of interest, e.g., $\partial{F}/\partial{\theta}$ and $\partial{F}/\partial{\phi}$, vectors defined componentwise. These will be lie in the tangent plane of the surface, as they can be viewed as tangent vectors for parametrically defined curves on the surface. Their cross product will be *normal* to the surface. The magnitude of the cross product, which reflects the angle between the two partial derivatives, will be informative as to the surface area.
### Plotting parametrized surfaces in `Julia`
### Plotting parameterized surfaces in `Julia`
Consider the parametrically described surface above. How would it be plotted? Using the `Plots` package, the process is quite similar to how a surface described by a function is plotted, but the $z$ values must be computed prior to plotting.
@@ -236,6 +248,217 @@ arrow!(Phi(pt...), out₁[:,1], linewidth=3)
arrow!(Phi(pt...), out₁[:,2], linewidth=3)
```
##### Example: A detour into plotting
The presentation of a 3D figure in a 2D format requires the use of linear perspective. The `Plots` package adds lighting effects, to nicely render a surface, as seen.
In this example, we see some of the mathematics behind how drawing a surface can be done more primitively to showcase some facts about vectors. We follow a few techniques learned from @Angenent.
```{julia}
#| echo: false
gr()
nothing
```
For our purposes we wish to mathematically project a figure onto a 2D plane.
The plane here is described by a view point in 3D space, $\vec{v}$. Taking this as one vector in an orthogonal coordinate system, the other two can be easily produced, the first by switching two coordinates, as would be done in 2D; the second through the cross product:
```{julia}
function projection_plane(v)
vx, vy, vz = v
a = [-vy, vx, 0] # v ⋅ a = 0
b = v × a # so v ⋅ b = 0
return (a/norm(a), b/norm(b))
end
```
Using these two unit vectors to describe the plane, the projection of a point onto the plane is simply found by taking dot products:
```{julia}
function project(x, v)
â, b̂ = projection_plane(v)
(x ⋅ â, x ⋅ b̂) # (x ⋅ â) â + (x ⋅ b̂) b̂
end
```
Let's see this in action by plotting a surface of revolution given by
```{julia}
radius(t) = 1 / (1 + exp(t))
t₀, tₙ = 0, 3
surf(t, θ) = [t, radius(t)*cos(θ), radius(t)*sin(θ)]
```
We begin by fixing a view point and plotting the projected axes. We do the latter with a function for re-use.
```{julia}
v = [2, -2, 1]
function plot_axes()
empty_style = (xaxis = ([], false),
yaxis = ([], false),
legend=false)
plt = plot(; empty_style...)
axis_values = [[(0,0,0), (3.5,0,0)], # x axis
[(0,0,0), (0, 2.0 * radius(0), 0)], # yaxis
[(0,0,0), (0, 0, 1.5 * radius(0))]] # z axis
for (ps, ax) ∈ zip(axis_values, ("x", "y", "z"))
p0, p1 = ps
a, b = project(p0, v), project(p1, v)
annotate!([(b...,text(ax, :bottom))])
plot!([a, b]; arrow=true, head=:tip, line=(:gray, 1)) # gr() allows arrows
end
plt
end
plt = plot_axes()
```
We are using the vector of tuples interface (representing points) to specify the curve to draw.
Now we add on some curves for fixed $t$ and then fixed $\theta$ utilizing the fact that `project` returns a tuple of $x$--$y$ values to display.
```{julia}
for t in range(t₀, tₙ, 20)
curve = [project(surf(t, θ), v) for θ in range(0, 2pi, 100)]
plot!(curve; line=(:black, 1))
end
for θ in range(0, 2pi, 60)
curve = [project(surf(t, θ), v) for t in range(t₀, tₙ, 20)]
plot!(curve; line=(:black, 1))
end
plt
```
The graphic is a little busy!
Let's focus on the cells layering the surface. These have equal size in the $t \times \theta$ range, but unequal area on the screen. Where they parallellograms, the area could be found by taking the 2-dimensional cross product of the two partial derivatives, resulting in a formula like: $a_x b_y - a_y b_x$.
When we discuss integrals related to such figures, this amount of area will be characterized by a computation involving the determinant of the upcoming Jacobian function.
We make a function to close over the viewpoint vector that can be passed to `ForwardDiff`, as it will return a vector and not a tuple.
```{julia}
function psurf(v)
(t,θ) -> begin
v1, v2 = project(surf(t, θ), v)
[v1, v2] # or call collect to make a tuple into a vector
end
end
```
The function returned by `psurf` is from $R^2 \rightarrow R^2$. With such a function, the computation of this approximate area becomes:
```{julia}
function detJ(F, t, θ)
∂θ = ForwardDiff.derivative(θ -> F(t, θ), θ)
∂t = ForwardDiff.derivative(t -> F(t, θ), t)
(ax, ay), (bx, by) = ∂θ, ∂t
ax * by - ay * bx
end
```
For our purposes, we are interested in the sign of the returned value. Plotting, we can see that some "area" is positive, some "negative":
```{julia}
t = 1
G = psurf(v)
plot(θ -> detJ(G, t, θ), 0, 2pi)
```
With this parameterization and viewpoint, the positive area for the surface is when the normal vector points towards the viewing point. In the following, we only plot such values:
```{julia}
plt = plot_axes()
function I(F, t, θ)
x, y = F(t, θ)
detJ(F, t, θ) >= 0 ? (x, y) : (x, NaN) # use NaN for y value
end
for t in range(t₀, tₙ, 20)
curve = [I(G, t, θ) for θ in range(0, 2pi, 100)]
plot!(curve; line=(:gray, 1))
end
for θ in range(0, 2pi, 60)
curve = [I(G, t, θ) for t in range(t₀, tₙ, 20)]
plot!(curve; line=(:gray, 1))
end
plt
```
The values for which `detJ` is zero form the visible boundary of the object. We can plot just those to get an even less busy view. We identify them by finding the value of $\theta$ in $[0,\pi]$ and $[\pi,2\pi]$ that makes the `detJ` function zero:
```{julia}
fold(F, t, θmin, θmax) = find_zero(θ -> detJ(F, t, θ), (θmin, θmax))
ts = range(t₀, tₙ, 100)
back_edge = fold.(G, ts, 0, pi)
front_edge = fold.(G, ts, pi, 2pi)
plt = plot_axes()
plot!(project.(surf.(ts, back_edge), (v,)); line=(:black, 1))
plot!(project.(surf.(ts, front_edge), (v,)); line=(:black, 1))
```
Adding caps makes the graphic stand out. The caps are just discs (fixed values of $t$) which are filled in with gray using a transparency so that the axes aren't masked.
```{julia}
θs = range(0, 2pi, 100)
S = Shape(project.(surf.(t₀, θs), (v,)))
plot!(S; fill=(:gray, 0.33))
S = Shape(project.(surf.(tₙ, θs), (v,)))
plot!(S; fill=(:gray, 0.33))
```
Finally, we introduce some shading using the same technique but assuming the light comes from a different position.
```{julia}
lightpt = [2, -2, 5] # from further above
H = psurf(lightpt)
light_edge = fold.(H, ts, pi, 2pi);
```
Angles between the light edge and the front edge would be in shadow. We indicate this by drawing lines for fixed $t$ values. As denser lines indicate more shadow, we feather how these are drawn:
```{julia}
for (i, (t, top, bottom)) in enumerate(zip(ts, light_edge, front_edge))
λ = iseven(i) ? 1.0 : 0.8
top = bottom + λ*(top - bottom)
curve = [project(surf(t, θ), v) for θ in range(bottom, top, 20)]
plot!(curve, line=(:black, 1))
end
plt
```
We can compare to the graph produced by `surface` for the same function:
```{julia}
ts = range(t₀, tₙ, 50)
θs = range(0, 2pi, 100)
surface(unzip(surf.(ts, θs'))...; legend=false)
```
```{julia}
#| echo: false
plotly()
nothing
```
## The total derivative

View File

@@ -1,3 +1,7 @@
---
engine: julia
---
# Integrals
Identifying the area under a curve between two values is an age-old problem. In this chapter we see that for many case the Fundamental Theorem of Calculus can be used to identify the area. When not applicable, we will see how such areas may be accurately estimated.

View File

@@ -0,0 +1,41 @@
# Appendix
```{julia}
#| hold: true
#| echo: false
gr()
## For **some reason** having this in the natural place messes up the plots.
## {{{approximate_surface_area}}}
xs,ys = range(-1, stop=1, length=50), range(-1, stop=1, length=50)
f(x,y)= 2 - (x^2 + y^2)
dr = [1/2, 3/4]
df = [f(dr[1],0), f(dr[2],0)]
function sa_approx_graph(i)
p = plot(xs, ys, f, st=[:surface], legend=false)
for theta in range(0, stop=i/10*2pi, length=10*i )
path3d!(p,sin(theta)*dr, cos(theta)*dr, df)
end
p
end
n = 10
anim = @animate for i=1:n
sa_approx_graph(i)
end
imgfile = tempname() * ".gif"
gif(anim, imgfile, fps = 1)
caption = L"""
Surface of revolution of $f(x) = 2 - x^2$ about the $y$ axis. The lines segments are the images of rotating the secant line connecting $(1/2, f(1/2))$ and $(3/4, f(3/4))$. These trace out the frustum of a cone which approximates the corresponding surface area of the surface of revolution. In the limit, this approximation becomes exact and a formula for the surface area of surfaces of revolution can be used to compute the value.
"""
plotly()
ImageFile(imgfile, caption)
```

View File

@@ -95,11 +95,11 @@ function make_arclength_graph(n)
ts = range(0, 2pi, 100)
λ = 0.005
cs = [λ .* xys for xys ∈ sincos.(ts)]
λ = 0.01
C = Plots.scale(Shape(:circle), λ)
for v ∈ zip(x.(pttn), y.(pttn))
S = Shape([v .+ xy for xy in cs])
for (u,v) ∈ zip(x.(pttn), y.(pttn))
S = Plots.translate(C, u,v)
plot!(S; fill=(:white,), line=(:black,2))
end
@@ -555,7 +555,9 @@ plot(t -> g(𝒔(t)), t -> f(𝒔(t)), 0, sinv(2*pi))
Following (faithfully) [Kantorwitz and Neumann](https://www.researchgate.net/publication/341676916_The_English_Galileo_and_His_Vision_of_Projectile_Motion_under_Air_Resistance), we consider a function $f(x)$ with the property that **both** $f$ and $f'$ are strictly concave down on $[a,b]$ and suppose $f(a) = f(b)$. Further, assume $f'$ is continuous. We will see this implies facts about arc-length and other integrals related to $f$.
The following figure is clearly of a concave down function. The asymmetry about the critical point will be seen to be a result of the derivative also being concave down. This asymmetry will be characterized in several different ways in the following including showing that the arc length from $(a,0)$ to $(c,f(c))$ is longer than from $(c,f(c))$ to $(b,0)$.
@fig-kantorwitz-neumann is clearly of a concave down function. The asymmetry about the critical point will be seen to be a result of the derivative also being concave down. This asymmetry will be characterized in several different ways in the following including showing that the arc length from $(a,0)$ to $(c,f(c))$ is longer than from $(c,f(c))$ to $(b,0)$.
::: {#@fig-kantorwitz-neumann}
```{julia}
@@ -590,7 +592,12 @@ plot!(zero)
annotate!([(0, 𝒚, "a"), (152, 𝒚, "b"), (u, 𝒚, "u"), (v, 𝒚, "v"), (c, 𝒚, "c")])
```
Take $a < u < c < v < b$ with $f(u) = f(v)$ and $c$ a critical point, as in the picture. There must be a critical point by Rolle's theorem, and it must be unique, as the derivative, which exists by the assumptions, must be strictly decreasing due to concavity of $f$ and hence there can be at most $1$ critical point.
Graph of function $f(x)$ with both $f$ and $f'$ strictly concave down.
:::
By Rolle's theorem there exists $c$ in $(a,b)$, a critical point, as in the picture. There must be a critical point by Rolle's theorem, and it must be unique, as the derivative, which exists by the assumptions, must be strictly decreasing due to concavity of $f$ and hence there can be at most $1$ critical point.
Take $a < u < c < v < b$ with $f(u) = f(v)$.
Some facts about this picture can be proven from the definition of concavity:
@@ -653,7 +660,7 @@ By the fundamental theorem of calculus:
$$
(f_1^{-1}(y) + f_2^{-1}(y))\big|_\alpha^\beta > 0
(f_1^{-1}(y) + f_2^{-1}(y))\Big|_\alpha^\beta > 0
$$
On rearranging:

View File

@@ -16,6 +16,8 @@ using Roots
---
![A jigsaw puzzle needs a certain amount of area to complete. For a traditional rectangular puzzle, this area is comprised of the sum of the areas for each piece. Decomposing a total area into the sum of smaller, known, ones--even if only approximate--is the basis of definite integration.](figures/jigsaw.png)
The question of area has long fascinated human culture. As children, we learn early on the formulas for the areas of some geometric figures: a square is $b^2$, a rectangle $b\cdot h$, a triangle $1/2 \cdot b \cdot h$ and for a circle, $\pi r^2$. The area of a rectangle is often the intuitive basis for illustrating multiplication. The area of a triangle has been known for ages. Even complicated expressions, such as [Heron's](http://tinyurl.com/mqm9z) formula which relates the area of a triangle with measurements from its perimeter have been around for 2000 years. The formula for the area of a circle is also quite old. Wikipedia dates it as far back as the [Rhind](http://en.wikipedia.org/wiki/Rhind_Mathematical_Papyrus) papyrus for 1700 BC, with the approximation of $256/81$ for $\pi$.
@@ -81,39 +83,46 @@ gr()
f(x) = x^2
colors = [:black, :blue, :orange, :red, :green, :orange, :purple]
## Area of parabola
## Area of parabola
function make_triangle_graph(n)
title = "Area of parabolic cup ..."
n==1 && (title = "Area = 1/2")
n==2 && (title = "Area = previous + 1/8")
n==3 && (title = "Area = previous + 2*(1/8)^2")
n==4 && (title = "Area = previous + 4*(1/8)^3")
n==5 && (title = "Area = previous + 8*(1/8)^4")
n==6 && (title = "Area = previous + 16*(1/8)^5")
n==7 && (title = "Area = previous + 32*(1/8)^6")
n==1 && (title = L"Area $= 1/2$")
n==2 && (title = L"Area $=$ previous $+\; \frac{1}{8}$")
n==3 && (title = L"Area $=$ previous $+\; 2\cdot\frac{1}{8^2}$")
n==4 && (title = L"Area $=$ previous $+\; 4\cdot\frac{1}{8^3}$")
n==5 && (title = L"Area $=$ previous $+\; 8\cdot\frac{1}{8^4}$")
n==6 && (title = L"Area $=$ previous $+\; 16\cdot\frac{1}{8^5}$")
n==7 && (title = L"Area $=$ previous $+\; 32\cdot\frac{1}{8^6}$")
plt = plot(f, 0, 1, legend=false, size = fig_size, linewidth=2)
annotate!(plt, [(0.05, 0.9, text(title,:left))]) # if in title, it grows funny with gr
n >= 1 && plot!(plt, [1,0,0,1, 0], [1,1,0,1,1], color=colors[1], linetype=:polygon, fill=colors[1], alpha=.2)
n == 1 && plot!(plt, [1,0,0,1, 0], [1,1,0,1,1], color=colors[1], linewidth=2)
plt = plot(f, 0, 1;
legend=false,
size = fig_size,
linewidth=2)
annotate!(plt, [
(0.05, 0.9, text(title,:left))
]) # if in title, it grows funny with gr
n >= 1 && plot!(plt, [1,0,0,1, 0], [1,1,0,1,1];
color=colors[1], linetype=:polygon,
fill=colors[1], alpha=.2)
n == 1 && plot!(plt, [1,0,0,1, 0], [1,1,0,1,1];
color=colors[1], linewidth=2)
for k in 2:n
xs = range(0, stop=1, length=1+2^(k-1))
ys = map(f, xs)
k < n && plot!(plt, xs, ys, linetype=:polygon, fill=:black, alpha=.2)
ys = f.(xs)
k < n && plot!(plt, xs, ys;
linetype=:polygon, fill=:black, alpha=.2)
if k == n
plot!(plt, xs, ys, color=colors[k], linetype=:polygon, fill=:black, alpha=.2)
plot!(plt, xs, ys, color=:black, linewidth=2)
plot!(plt, xs, ys;
color=colors[k], linetype=:polygon, fill=:black, alpha=.2)
plot!(plt, xs, ys;
color=:black, linewidth=2)
end
end
plt
end
n = 7
anim = @animate for i=1:n
make_triangle_graph(i)
@@ -183,7 +192,7 @@ $$
S_n = f(c_1) \cdot (x_1 - x_0) + f(c_2) \cdot (x_2 - x_1) + \cdots + f(c_n) \cdot (x_n - x_{n-1}).
$$
Clearly for a given partition and choice of $c_i$, the above can be computed. Each term $f(c_i)\cdot(x_i-x_{i-1})$ can be visualized as the area of a rectangle with base spanning from $x_{i-1}$ to $x_i$ and height given by the function value at $c_i$. The following visualizes left Riemann sums for different values of $n$ in a way that makes Beekman's intuition plausible that as the number of rectangles gets larger, the approximate sum will get closer to the actual area.
Clearly for a given partition and choice of $c_i$, the above can be computed. Each term $f(c_i)\cdot(x_i-x_{i-1}) = f(c_i)\Delta_i$ can be visualized as the area of a rectangle with base spanning from $x_{i-1}$ to $x_i$ and height given by the function value at $c_i$. The following visualizes left Riemann sums for different values of $n$ in a way that makes Beekman's intuition plausible that as the number of rectangles gets larger, the approximate sum will get closer to the actual area.
```{julia}
@@ -354,7 +363,7 @@ When the integral exists, it is written $V = \int_a^b f(x) dx$.
:::{.callout-note}
## History note
The expression $V = \int_a^b f(x) dx$ is known as the *definite integral* of $f$ over $[a,b]$. Much earlier than Riemann, Cauchy had defined the definite integral in terms of a sum of rectangular products beginning with $S=(x_1 - x_0) f(x_0) + (x_2 - x_1) f(x_1) + \cdots + (x_n - x_{n-1}) f(x_{n-1})$ (the left Riemann sum). He showed the limit was well defined for any continuous function. Riemann's formulation relaxes the choice of partition and the choice of the $c_i$ so that integrability can be better understood.
The expression $V = \int_a^b f(x) dx$ is known as the *definite integral* of $f$ over $[a,b]$. Much earlier than Riemann, Cauchy had defined the definite integral in terms of a sum of rectangular products beginning with $S=f(x_0) \cdot (x_1 - x_0) + f(x_1) \cdot (x_2 - x_1) + \cdots + f(x_{n-1}) \cdot (x_n - x_{n-1}) $ (the left Riemann sum). He showed the limit was well defined for any continuous function. Riemann's formulation relaxes the choice of partition and the choice of the $c_i$ so that integrability can be better understood.
:::
@@ -364,18 +373,6 @@ The expression $V = \int_a^b f(x) dx$ is known as the *definite integral* of $f$
The following formulas are consequences when $f(x)$ is integrable. These mostly follow through a judicious rearranging of the approximating sums.
The area is $0$ when there is no width to the interval to integrate over:
> $$
> \int_a^a f(x) dx = 0.
> $$
Even our definition of a partition doesn't really apply, as we assume $a < b$, but clearly if $a=x_0=x_n=b$ then our only"approximating" sum could be $f(a)(b-a) = 0$.
The area under a constant function is found from the area of rectangle, a special case being $c=0$ yielding $0$ area:
@@ -388,16 +385,65 @@ The area under a constant function is found from the area of rectangle, a specia
For any partition of $a < b$, we have $S_n = c(x_1 - x_0) + c(x_2 -x_1) + \cdots + c(x_n - x_{n-1})$. By factoring out the $c$, we have a *telescoping sum* which means the sum simplifies to $S_n = c(x_n-x_0) = c(b-a)$. Hence any limit must be this constant value.
Scaling the $y$ axis by a constant can be done before or after computing the area:
::: {#fig-consequence-rectangle-area}
```{julia}
#| echo: false
gr()
let
c = 1
a,b = 0.5, 1.5
f(x) = c
Δ = 0.1
plt = plot(;
xaxis=([], false),
yaxis=([], false),
legend=false,
)
plot!(f, a, b; line=(:black, 2))
plot!([a-Δ, b + Δ], [0,0]; line=(:gray, 1), arrow=true, side=:head)
plot!([a-Δ/2, a-Δ/2], [-Δ, c + Δ]; line=(:gray, 1), arrow=true, side=:head)
plot!([a,a],[0,f(a)]; line=(:black, 1, :dash))
plot!([b,b],[0,f(b)]; line=(:black, 1, :dash))
annotate!([
(a, 0, text(L"a", :top, :right)),
(b, 0, text(L"b", :top, :left)),
(a-Δ/2-0.01, c, text(L"c", :right))
])
current()
end
```
```{julia}
#| echo: false
plotly()
nothing
```
Illustration that the area under a constant function is that of a rectangle
:::
The area is $0$ when there is no width to the interval to integrate over:
> $$
> \int_a^b cf(x) dx = c \int_a^b f(x) dx.
> \int_a^a f(x) dx = 0.
> $$
Let $a=x_0 < x_1 < \cdots < x_n=b$ be any partition. Then we have $S_n= cf(c_1)(x_1-x_0) + \cdots + cf(c_n)(x_n-x_{n-1})$ $=$ $c\cdot\left[ f(c_1)(x_1 - x_0) + \cdots + f(c_n)(x_n - x_{n-1})\right]$. The "limit" of the left side is $\int_a^b c f(x) dx$. The "limit" of the right side is $c \cdot \int_a^b f(x)$. We call this a "sketch" as a formal proof would show that for any $\epsilon$ we could choose a $\delta$ so that any partition with norm $\delta$ will yield a sum less than $\epsilon$. Here, then our "any" partition would be one for which the $\delta$ on the left hand side applies. The computation shows that the same $\delta$ would apply for the right hand side when $\epsilon$ is the same.
Even our definition of a partition doesn't really apply, as we assume $a < b$, but clearly if $a=x_0=x_n=b$ then our only"approximating" sum could be $f(a)(b-a) = 0$.
#### Shifts
A jigsaw puzzle piece will have the same area if it is moved around on the table or flipped over. Similarly some shifts preserve area under a function.
The area is invariant under shifts left or right.
@@ -424,6 +470,59 @@ $$
The left side will have a limit of $\int_a^b f(x-c) dx$ the right would have a "limit" of $\int_{a-c}^{b-c}f(x)dx$.
::: {#fig-consequence-rectangle-area}
```{julia}
#| echo: false
gr()
let
f(x) = 2 + cospi(x^2/10)*sinpi(x)
plt = plot(;
xaxis=([], false),
yaxis=([], false),
legend=false,
)
a, b = 0,4
c = 5
plot!(f, a, b; line=(:black, 2))
plot!(x -> f(x-c), a+c, b+c; line=(:red, 2))
plot!([-1, b+c + 1], [0,0]; line=(:gray, 2), arrow=true, side=:head)
for x ∈ (a,b)
plot!([x,x],[0,f(x)]; line=(:black,1, :dash))
end
for x ∈ (a+c,b+c)
plot!([x,x],[0,f(x)]; line=(:red,1, :dash))
end
annotate!([
(a+c,0, text(L"a", :top)),
(b+c,0, text(L"b", :top)),
(a,0, text(L"a-c", :top)),
(b,0, text(L"b-c", :top)),
(1.0, 3, text(L"f(x)",:left)),
(1.0+c, 3, text(L"f(x-c)",:left)),
])
current()
end
```
```{julia}
#| echo: false
plotly()
nothing
```
Illustration that the area under shift remains the same
:::
Similarly, reflections don't effect the area under the curve, they just require a new parameterization:
@@ -433,7 +532,79 @@ Similarly, reflections don't effect the area under the curve, they just require
The scaling operation $g(x) = f(cx)$ has the following:
::: {#fig-consequence-reflect-area}
```{julia}
#| echo: false
gr()
let
f(x) = 2 + cospi(x^2/10)*sinpi(x)
g(x) = f(-x)
plt = plot(;
xaxis=([], false),
yaxis=([], false),
legend=false,
)
a, b = 1,4
plot!(f, a, b; line=(:black, 2))
plot!(g, -b, -a; line=(:red, 2))
plot!([-5, 5], [0,0]; line=(:gray,1), arrow=true, side=:head)
plot!([0,0], [-0.1,3.15]; line=(:gray,1), arrow=true, side=:head)
for x in (a,b)
plot!([x,x], [0,f(x)]; line=(:black,1,:dash))
plot!([-x,-x], [0,g(-x)]; line=(:red,1,:dash))
end
annotate!([
(a,0, text(L"a", :top)),
(b,0, text(L"b", :top)),
(-a,0, text(L"-a", :top)),
(-b,0, text(L"-b", :top)),
])
current()
end
```
```{julia}
#| echo: false
plotly()
nothing
```
Illustration that the area remains constant under reflection through $y$ axis.
:::
The "reversed" area is the same, only accounted for with a minus sign.
> $$
> \int_a^b f(x) dx = -\int_b^a f(x) dx.
> $$
#### Scaling
Scaling the $y$ axis by a constant can be done before or after computing the area:
> $$
> \int_a^b cf(x) dx = c \int_a^b f(x) dx.
> $$
Let $a=x_0 < x_1 < \cdots < x_n=b$ be any partition. Then we have $S_n= cf(c_1)(x_1-x_0) + \cdots + cf(c_n)(x_n-x_{n-1})$ $=$ $c\cdot\left[ f(c_1)(x_1 - x_0) + \cdots + f(c_n)(x_n - x_{n-1})\right]$. The "limit" of the left side is $\int_a^b c f(x) dx$. The "limit" of the right side is $c \cdot \int_a^b f(x)$. We call this a "sketch" as a formal proof would show that for any $\epsilon$ we could choose a $\delta$ so that any partition with norm $\delta$ will yield a sum less than $\epsilon$. Here, then our "any" partition would be one for which the $\delta$ on the left hand side applies. The computation shows that the same $\delta$ would apply for the right hand side when $\epsilon$ is the same.
The scaling operation on the $x$ axis, $g(x) = f(cx)$, has the following property:
> $$
@@ -448,8 +619,9 @@ The scaling operation shifts $a$ to $ca$ and $b$ to $cb$ so the limits of integr
Combining two operations above, the operation $g(x) = \frac{1}{h}f(\frac{x-c}{h})$ will leave the area between $a$ and $b$ under $g$ the same as the area under $f$ between $(a-c)/h$ and $(b-c)/h$.
---
#### Area is additive
When two jigsaw pieces interlock their combined area is that of each added. This also applies to areas under functions.
The area between $a$ and $b$ can be broken up into the sum of the area between $a$ and $c$ and that between $c$ and $b$.
@@ -463,35 +635,185 @@ The area between $a$ and $b$ can be broken up into the sum of the area between $
For this, suppose we have a partition for both the integrals on the right hand side for a given $\epsilon/2$ and $\delta$. Combining these into a partition of $[a,b]$ will mean $\delta$ is still the norm. The approximating sum will combine to be no more than $\epsilon/2 + \epsilon/2$, so for a given $\epsilon$, this $\delta$ applies.
This is due to the area on the left and right of $0$ being equivalent.
::: {#fig-consequence-additive-area}
```{julia}
#| echo: false
gr()
let
f(x) = 2 + cospi(x^2/7)*sinpi(x)
a,b,c = 0.1, 8, 3
xs = range(a,c,100)
A1 = Shape(vcat(xs,c,a), vcat(f.(xs), 0, 0))
xs = range(c,b,100)
A2 = Shape(vcat(xs,b,c), vcat(f.(xs), 0, 0))
The "reversed" area is the same, only accounted for with a minus sign.
plt = plot(;
xaxis=([], false),
yaxis=([], false),
legend=false,
)
plot!([0,0] .- 0.1,[-.1,3]; line=(:gray, 1), arrow=true, side=:head)
plot!([0-.2, b+0.5],[0,0]; line=(:gray, 1), arrow=true, side=:head)
plot!(A1; fill=(:gray60,1), line=(nothing,))
plot!(A2; fill=(:gray90,1), line=(nothing,))
plot!(f, a, b; line=(:black, 2))
for x in (a,b,c)
plot!([x,x], [0, f(x)]; line=(:black, 1, :dash))
end
> $$
> \int_a^b f(x) dx = -\int_b^a f(x) dx.
> $$
annotate!([(x,0,text(latexstring("$y"),:top)) for (x,y) in zip((a,b,c),("a","b","c"))])
end
```
```{julia}
#| echo: false
plotly()
nothing
```
Illustration that the area between $a$ and $b$ can be computed as area between $a$ and $c$ and then $c$ and $b$.
:::
A consequence of the last few statements is:
> If $f(x)$ is an even function, then $\int_{-a}^a f(x) dx = 2 \int_0^a f(x) dx$. If $f(x)$ is an odd function, then $\int_{-a}^a f(x) dx = 0$.
> If $f(x)$ is an even function, then $\int_{-a}^a f(x) dx = 2 \int_0^a f(x) dx$.
> If $f(x)$ is an odd function, then $\int_{-a}^a f(x) dx = 0$.
Additivity works in the $y$ direction as well.
If $f(x)$ and $g(x)$ are two functions then
> $$
> \int_a^b (f(x) + g(x)) dx = \int_a^b f(x) dx + \int_a^b g(x) dx
> $$
For any partitioning with $x_i, x_{i-1}$ and $c_i$ this holds:
$$
(f(c_i) + g(c_i)) \cdot (x_i - x_{i-1}) =
f(c_i) \cdot (x_i - x_{i-1}) + g(c_i) \cdot (x_i - x_{i-1})
$$
This leads to the same statement for the areas under the curves.
The *linearity* of the integration operation refers to this combination of the above:
> $$
> \int_a^b (cf(x) + dg(x)) dx = c\int_a^b f(x) dx + d \int_a^b g(x)dx
> $$
The integral of a shifted function satisfies:
> $$
> \int_a^b \left(D + C\cdot f(\frac{x - B}{A})\right) dx = D\cdot(b-a) + C \cdot A \int_{\frac{a-B}{A}}^{\frac{b-B}{A}} f(x) dx
> $$
This follows from a few of the statements above:
$$
\begin{align*}
\int_a^b \left(D + C\cdot f(\frac{x - B}{A})\right) dx &=
\int_a^b D dx + C \int_a^b f(\frac{x-B}{A}) dx \\
&= D\cdot(b-a) + C\cdot A \int_{\frac{a-B}{A}}^{\frac{b-B}{A}} f(x) dx
\end{align*}
$$
#### Inequalities
Area under a non-negative function is non-negative
> $$
> \int_a^b f(x) dx \geq 0,\quad\text{when } a < b, \text{ and } f(x) \geq 0
> $$
Under this assumption, for any partitioning with $x_i, x_{i-1}$ and $c_i$ it holds the $f(c_i)\cdot(x_i - x_{i-1}) \geq 0$. So any sum of non-negative values can only be non-negative, even in the limit.
If $g$ bounds $f$ then the area under $g$ will bound the area under $f$, in particular if $f(x)$ is non negative, so will the area under $f$ be non negative for any $a < b$. (This assumes that $g$ and $f$ are integrable.)
> If $0 \leq f(x) \leq g(x)$ then $\int_a^b f(x) dx \leq \int_a^b g(x) dx.$
If $g$ bounds $f$ then the area under $g$ will bound the area under $f$.
> $$
> $\int_a^b f(x) dx \leq \int_a^b g(x) dx \quad\text{when } a < b\text{ and } 0 \leq f(x) \leq g(x)
> $$
For any partition of $[a,b]$ and choice of $c_i$, we have the term-by-term bound $f(c_i)(x_i-x_{i-1}) \leq g(c_i)(x_i-x_{i-1})$ So any sequence of partitions that converges to the limits will have this inequality maintained for the sum.
::: {#fig-consequence-0-area}
```{julia}
#| echo: false
gr()
let
f(x) = 1/6+x^3*(2-x)/2
g(x) = 1/6+exp(x/3)+(1-x/1.7)^6-0.6
a, b = 0, 2
plot(; empty_style...)
plot!([a-.5, b+.25], [0,0]; line=(:gray, 1), arrow=true, side=:head)
plot!([0,0] .- 0.25, [-0.25, 1.8]; line=(:gray, 1), arrow=true, side=:head)
xs = range(a,b,250)
S = Shape(vcat(xs, reverse(xs)), vcat(f.(xs), g.(reverse(xs))))
plot!(S; fill=(:gray70, 0.3), line=(nothing,))
S = Shape(vcat(xs, reverse(xs)), vcat(zero.(xs), f.(reverse(xs))))
plot!(S; fill=(:gray90, 0.3), line=(nothing,))
plot!(f, a, b; line=(:black, 4))
plot!(g, a, b; line=(:black, 2))
for x in (a,b)
plot!([x,x], [0, g(x)]; line=(:black,1,:dash))
end
annotate!([(x,0,text(t, :top)) for (x,t) in zip((a,b),(L"a", L"b"))])
current()
end
```
```{julia}
#| echo: false
plotly()
nothing
```
Illustration that if $f(x) \le g(x)$ on $[a,b]$ then the integrals share the same property. The excess area is clearly positive.
:::
(This also follows by considering $h(x) = g(x) - f(x) \geq 0$ by assumption, so $\int_a^b h(x) dx \geq 0$.)
For non-negative functions, integrals over larger domains are bigger
> $$
> \int_a^c f(x) dx \le \int_a^b f(x) dx,\quad\text{when } c < b \text{ and } f(x) \ge 0
> $$
This follows as $\int_c^b f(x) dx$ is non-negative under these assumptions.
### Some known integrals
@@ -606,7 +928,7 @@ The main idea behind this is that the difference between the maximum and minimum
For example, the function $f(x) = 1$ for $x$ in $[0,1]$ and $0$ otherwise will be integrable, as it is continuous at all but two points, $0$ and $1$, where it jumps.
* Some functions can have infinitely many points of discontinuity and still be integrable. The example of $f(x) = 1/q$ when $x=p/q$ is rational, and $0$ otherwise is often used as an example.
* Some functions can have infinitely many points of discontinuity and still be integrable. The example of $f(x) = 1/q$ when $x=p/q$ is rational, and $0$ otherwise is often used to illustrate this.
## Numeric integration
@@ -636,11 +958,11 @@ deltas = diff(xs) # forms x2-x1, x3-x2, ..., xn-xn-1
cs = xs[1:end-1] # finds left-hand end points. xs[2:end] would be right-hand ones.
```
Now to multiply the values. We want to sum the product `f(cs[i]) * deltas[i]`, here is one way to do so:
We want to sum the products $f(c_i)\Delta_i$. Here is one way to do so using `zip` to iterate over the paired off values in `cs` and `deltas`.
```{julia}
sum(f(cs[i]) * deltas[i] for i in 1:length(deltas))
sum(f(ci)*Δi for (ci, Δi) in zip(cs, deltas))
```
Our answer is not so close to the value of $1/3$, but what did we expect - we only used $n=5$ intervals. Trying again with $50,000$ gives us:
@@ -652,7 +974,7 @@ n = 50_000
xs = a:(b-a)/n:b
deltas = diff(xs)
cs = xs[1:end-1]
sum(f(cs[i]) * deltas[i] for i in 1:length(deltas))
sum(f(ci)*Δi for (ci, Δi) in zip(cs, deltas))
```
This value is about $10^{-5}$ off from the actual answer of $1/3$.
@@ -745,7 +1067,7 @@ plot!(zero)
We could add the signed area over $[0,1]$ to the above, but instead see a square of area $1$, a triangle with area $1/2$ and a triangle with signed area $-1$. The total is then $1/2$.
This figure may make the above decomposition more clear:
This figure--using equal sized axes--may make the above decomposition more clear:
```{julia}
#| echo: false
@@ -758,6 +1080,7 @@ let
plot!(Shape([-1,0,1], [0,-1,0]); fill=(:gray90,))
plot!(Shape([1,2,2,1], [0,1,0,0]); fill=(:gray10,))
plot!(Shape([2,3,3,2], [0,0,1,1]); fill=(:gray,))
plot!([0,0], [0, g(0)]; line=(:black,1,:dash))
end
```
@@ -844,7 +1167,9 @@ We have the well-known triangle [inequality](http://en.wikipedia.org/wiki/Triang
This suggests that the following inequality holds for integrals:
> $\lvert \int_a^b f(x) dx \rvert \leq \int_a^b \lvert f(x) \rvert dx$.
> $$
> \lvert \int_a^b f(x) dx \rvert \leq \int_a^b \lvert f(x) \rvert dx$.
> $$
@@ -922,7 +1247,8 @@ The formulas for an approximation to the integral $\int_{-1}^1 f(x) dx$ discusse
$$
\begin{align*}
S &= f(x_1) \Delta_1 + f(x_2) \Delta_2 + \cdots + f(x_n) \Delta_n\\
&= w_1 f(x_1) + w_2 f(x_2) + \cdots + w_n f(x_n).
&= w_1 f(x_1) + w_2 f(x_2) + \cdots + w_n f(x_n)\\
&= \sum_{i=1}^n w_i f(x_i).
\end{align*}
$$
@@ -957,7 +1283,7 @@ f(x) = x^5 - x + 1
quadgk(f, -2, 2)
```
The error term is $0$, answer is $4$ up to the last unit of precision (1 ulp), so any error is only in floating point approximations.
The error term is $0$, the answer is $4$ up to the last unit of precision (1 ulp), so any error is only in floating point approximations.
For the numeric computation of definite integrals, the `quadgk` function should be used over the Riemann sums or even Simpson's rule.
@@ -1476,6 +1802,70 @@ val, _ = quadgk(f, a, b)
numericq(val)
```
###### Question
Let $A=1.98$ and $B=1.135$ and
$$
f(x) = \frac{1 - e^{-Ax}}{B\sqrt{\pi}x} e^{-x^2}.
$$
Find $\int_0^1 f(x) dx$
```{julia}
#| echo: false
let
A,B = 1.98, 1.135
f(x) = (1 - exp(-A*x))*exp(-x^2)/(B*sqrt(pi)*x)
val,_ = quadgk(f, 0, 1)
numericq(val)
end
```
###### Question
A bound for the complementary error function ( positive function) is
$$
\text{erfc}(x) \leq \frac{1}{2}e^{-2x^2} + \frac{1}{2}e^{-x^2} \leq e^{-x^2}
\quad x \geq 0.
$$
Let $f(x)$ be the first bound, $g(x)$ the second.
Assuming this is true, confirm numerically using `quadgk` that
$$
\int_0^3 f(x) dx \leq \int_0^3 g(x) dx
$$
The value of $\int_0^3 f(x) dx$ is
```{julia}
#| echo: false
let
f(x) = 1/2 * exp(-2x^2) + 1/2 * exp(-x^2)
val,_ = quadgk(f, 0, 3)
numericq(val)
end
```
The value of $\int_0^3 g(x) dx$ is
```{julia}
#| echo: false
let
g(x) = exp(-x^2)
val,_ = quadgk(g, 0, 3)
numericq(val)
end
```
###### Question

View File

@@ -32,7 +32,7 @@ can be interpreted as the "signed" area between $f(x)$ and $g(x)$ over $[a,b]$.
```{julia}
#| hold: true
#| echo: false
#| label: fig-area-between-f-g
#| label: fig-area-between-f-g-shade
#| fig-cap: "Area between two functions"
f1(x) = x^2
g1(x) = sqrt(x)
@@ -626,7 +626,6 @@ Each term describes the area of a trapezoid, possibly signed.
This figure illustrates for a simple case:
```{julia}
using Plots
xs = [1, 3, 4, 2, 1] # n = 4 to give 5=n+1 values
ys = [1, 1, 2, 3, 1]
p = plot(xs, ys; line=(3, :black), ylims=(0,4), legend=false)

Binary file not shown.

After

Width:  |  Height:  |  Size: 727 KiB

View File

@@ -156,10 +156,75 @@ In Part 1, the integral $F(x) = \int_a^x f(u) du$ is defined for any Riemann int
:::
This figure relating the area under some continuous $f(x)$ from $a$ to both $x$ and $x+h$ for some small $h$ helps to visualize the two fundamental theorems.
::: {#fig-FTC-derivative}
```{julia}
#| echo: false
let
gr()
f(x) = sin(x)
A(x) = cos(x)
a,b = 0, 6pi/13
h = pi/20
xs = range(a, b, 100)
p1 = plot(; empty_style...)
plot!([0,0] .- 0.05,[-0.1, 1]; line=(:gray,1), arrow=true, side=:head)
plot!([-0.1, b+h + pi/10], [0,0]; line=(:gray,1), arrow=true, side=:head)
xs = range(a, b, 100)
S = Shape(vcat(xs, reverse(xs)), vcat(f.(xs), zero.(xs)))
plot!(S; fill=(:gray90, 0.25), line=(nothing,))
plot!(f, a, b+h; line=(:black, 2))
xs = range(b, b+h, 100)
S = Shape(vcat(xs, reverse(xs)), vcat(f.(xs), zero.(xs)))
plot!(S; fill=(:gray70, 0.25), line=(nothing,))
plot!([b,b,b+h,b+h],[0,f(b),f(b),0]; line=(:black,1,:dash))
annotate!([
(a,0,text(L"a", :top, :left)),
(b, 0, text(L"x", :top)),
(b+h,0,text(L"x+h", :top)),
(2b/3, 1/2, text(L"A(x)")),
(b + h/2, 1/2, text(L"f(x)\cdot h \approx A(x+h)-A(x)", rotation=90))
])
current()
end
```
```{julia}
#| echo: false
plotly()
nothing
```
Area under curve between $a$ and $b$ labeled with $A(b)$ for $b=x$ and $b=x+h$.
:::
The last rectangle is exactly $f(x)h$ and approximately $A(x+h)-A(x)$, the difference being the small cap above the shaded rectangle. This gives the approximate derivative:
$$
A'(x) \approx \frac{A(x+h) - A(x)}{h} \approx \frac{f(x)\cdot h}{h} = f(x)
$$
That is, by taking limits, $A(x) = \int_a^x f(u) du$ is an antiderivative of $f(x)$. Moreover, from geometric considerations of area, if $a < c < b$, then
$$
A(b) - A(c) = \int_a^b f(x) dx - \int_a^c f(x) dx = \int_c^b f(x) dx
$$
That is $A(x)$ satisfies the two parts of the fundamental theorem.
## Using the fundamental theorem of calculus to evaluate definite integrals
The major use of the FTC is the computation of $\int_a^b f(x) dx$. Rather than resort to Riemann sums or geometric arguments, there is an alternative - *when possible*, find a function $F$ with $F'(x) = f(x)$ and compute $F(b) - F(a)$.
The most visible use of the FTC is the computation of definite integrals, $\int_a^b f(x) dx$. Rather than resort to Riemann sums or geometric arguments, there is an alternative - *when possible*, find a function $F$ with $F'(x) = f(x)$ and compute $F(b) - F(a)$.
Some examples:
@@ -213,21 +278,21 @@ The expression $F(b) - F(a)$ is often written in this more compact form:
$$
\int_a^b f(x) dx = F(b) - F(a) = F(x)\big|_{x=a}^b, \text{ or just expr}\big|_{x=a}^b.
\int_a^b f(x) dx = F(b) - F(a) = F(x)\Big|_{x=a}^b, \text{ or just expr}\Big|_{x=a}^b.
$$
The vertical bar is used for the *evaluation* step, in this case the $a$ and $b$ mirror that of the definite integral. This notation lends itself to working inline, as we illustrate with this next problem where we "know" a function "$F$", so just express it "inline":
$$
\int_0^{\pi/4} \sec^2(x) dx = \tan(x) \big|_{x=0}^{\pi/4} = 1 - 0 = 1.
\int_0^{\pi/4} \sec^2(x) dx = \tan(x) \Big|_{x=0}^{\pi/4} = 1 - 0 = 1.
$$
A consequence of this notation is:
$$
F(x) \big|_{x=a}^b = -F(x) \big|_{x=b}^a.
F(x) \Big|_{x=a}^b = -F(x) \Big|_{x=b}^a.
$$
This says nothing more than $F(b)-F(a) = -F(a) - (-F(b))$, though more compactly.
@@ -324,13 +389,13 @@ Answers may not be available as elementary functions, but there may be special f
integrate(x / sqrt(1-x^3), x)
```
The different cases explored by `integrate` are after the questions.
Different cases explored by `integrate` are mentioned after the questions.
## Rules of integration
There are some "rules" of integration that allow integrals to be re-expressed. These follow from the rules of derivatives.
There are some "rules" of integration that allow indefinite integrals to be re-expressed.
* The integral of a constant times a function:
@@ -353,7 +418,7 @@ $$
This follows immediately as if $F(x)$ and $G(x)$ are antiderivatives of $f(x)$ and $g(x)$, then $[F(x) + G(x)]' = f(x) + g(x)$, so the right hand side will have a derivative of $f(x) + g(x)$.
In fact, this more general form where $c$ and $d$ are constants covers both cases:
In fact, this more general form where $c$ and $d$ are constants covers both cases and referred to by the linearity of the integral:
$$
@@ -373,7 +438,7 @@ $$
\begin{align*}
\int (a_n x^n + \cdots + a_1 x + a_0) dx
&= \int a_nx^n dx + \cdots + \int a_1 x dx + \int a_0 dx \\
&= a_n \int x^n dx + \cdots + a_1 \int x dx + a_0 \int dx \\
&= a_n \int x^n dx + \cdots + a_1 \int x^1 dx + a_0 \int x^0 dx \\
&= a_n\frac{x^{n+1}}{n+1} + \cdots + a_1 \frac{x^2}{2} + a_0 \frac{x}{1}.
\end{align*}
$$
@@ -417,12 +482,14 @@ This seems like a lot of work, and indeed it is more than is needed. The followi
$$
\int_0^\pi 100 \sin(x) dx = 100(-\cos(x)) \big|_0^{\pi} = 100 \cos(x) \big|_{\pi}^0 = 100(1) - 100(-1) = 200.
\int_0^\pi 100 \sin(x) dx = 100(-\cos(x)) \Big|_0^{\pi} = 100 \cos(x) \Big|_{\pi}^0 = 100(1) - 100(-1) = 200.
$$
## The derivative of the integral
The relationship that $[\int_a^x f(u) du]' = f(x)$ is a bit harder to appreciate, as it doesn't help answer many ready made questions. Here we give some examples of its use.
@@ -433,12 +500,16 @@ $$
F(x) = \int_a^x f(u) du.
$$
The value of $a$ does not matter, as long as the integral is defined.
The value of $a$ does not matter, as long as the integral is defined. This $F$ satisfies the first fundamental theorem, as $F(a)=0$.
```{julia}
#| hold: true
#| echo: false
#| eval: false
##{{{ftc_graph}}}
gr()
function make_ftc_graph(n)
@@ -479,9 +550,9 @@ imgfile = tempname() * ".gif"
gif(anim, imgfile, fps = 1)
plotly()
ImageFile(imgfile, caption)
```
The picture for this, for non-negative $f$, is of accumulating area as $x$ increases. It can be used to give insight into some formulas:
#The picture for this, for non-negative $f$, is of accumulating area as $x$ increases. It can be used to give insight into some formulas:
```
For any function, we know that $F(b) - F(c) + F(c) - F(a) = F(b) - F(a)$. For this specific function, this translates into this property of the integral:
@@ -550,7 +621,7 @@ In probability theory, for a positive, continuous random variable, the probabili
For example, the exponential distribution with rate $1$ has $f(x) = e^{-x}$. Compute $F(x)$.
This is just $F(x) = \int_0^x e^{-u} du = -e^{-u}\big|_0^x = 1 - e^{-x}$.
This is just $F(x) = \int_0^x e^{-u} du = -e^{-u}\Big|_0^x = 1 - e^{-x}$.
The "uniform" distribution on $[a,b]$ has
@@ -1120,6 +1191,192 @@ answ = 2
radioq(choices, answ)
```
###### Question
The error function (`erf`) is defined in terms of an integral:
$$
\text{erf}(x) = \frac{2}{\sqrt{\pi}} \int_0^x \exp(-t^2) dt, \quad{x \geq 0}
$$
The constant is chosen so that $\lim_{x \rightarrow \infty} \text{erf}(x) = 1$.
What is the derivative of $\text{erf}(x)$?
```{julia}
#| echo: false
choices = [L"\exp(-x^2)",
L"-2x \exp(-x^2)",
L"\frac{2}{\sqrt{\pi}} \exp(-x^2)"]
radioq(choices, 3; keep_order=true, explanation="Don't forget the scalar multiple")
```
Is the function $\text{erf(x)}$ *increasing* on $[0,\infty)$?
```{julia}
#| echo: false
choices = ["No",
"Yes, the derivative is positive on this interval",
"Yes, the derivative is negative on this interval",
"Yes, the derivative is increasing on this interval",
"Yes, the derivative is decreasing on this interval"]
radioq(choices, 2; keep_order=true)
```
Is the function $\text{erf(x)}$ *concave down* on $[0,\infty)$?
```{julia}
#| echo: false
choices = ["No",
"Yes, the derivative is positive on this interval",
"Yes, the derivative is negative on this interval",
"Yes, the derivative is increasing on this interval",
"Yes, the derivative is decreasing on this interval"]
radioq(choices, 5; keep_order=true)
```
For $x > 0$, consider the function
$$
F(x) = \frac{2}{\sqrt{\pi}} \int_{-x}^0 \exp(-t^2) dt
$$
Why is $F'(x) = \text{erf}'(x)$?
```{julia}
#| echo: false
choices = ["The integrand is an *even* function so the itegral from ``0`` to ``x`` is the same as the integral from ``-x`` to ``0``",
"This isn't true"]
radioq(choices, 1; keep_order=true)
```
Consider the function
$$
F(x) = \frac{2}{\sqrt{\pi}} \int_0^{\sqrt{x}} \exp(-t^2) dt, \quad x \geq 0
$$
What is the derivative of $F$?
```{julia}
#| echo: false
choices = [L"\exp(-x^2)",
L"\frac{2}{\sqrt{\pi}} \exp(-x^2)",
L"\frac{2}{\sqrt{\pi}} \exp(-x^2) \cdot (-2x)"]
radioq(choices, 3; keep_order=true, explanation="Don't forget to apply the chain rule, as ``F(x) = \\text{erf}(\\sqrt{x})``")
```
###### Question
Define two function through the integrals:
$$
\begin{align*}
S(x) &= \int_0^x \sin(t^2) dt\\
C(x) &= \int_0^x \cos(t^2) dt
\end{align*}
$$
These are called *Fresnel Integrals*.
A non-performant implementation might look like:
```{julia}
S(x) = first(quadgk(t -> sin(t^2), 0, x))
```
Define a similar function for $C(x)$ and them make a parametric plot for $0 \le t \le 5$.
Describe the shape.
```{julia}
#| echo: false
choices = ["It makes a lovely star shape",
"It makes a lovely spiral shape",
"It makes a lovely circle"]
radioq(choices, 2; keep_order=true)
```
What is the value of $S'(x)^2 + C'(x)^2$ when $x=\pi$?
```{julia}
#| echo: false
numericq(1)
```
###### Question
Define a function with parameter $\alpha \geq 1$ by:
$$
\gamma(x; \alpha) = \int_0^x \exp(-t) t^{\alpha-1} dt, \quad x > 0
$$
What is the ratio of $\gamma'(2; 3) / \gamma'(2; 4)$?
```{julia}
#| echo: false
df(x,alpha) = exp(-x)*x^(alpha -1)
numericq(df(2,3)/df(2,4))
```
###### Question
Define a function
$$
i(x) = \int_0^{x^2} \exp(-t) t^{1/2} dt
$$
What is the derivative if $i$?
```{julia}
#| echo: false
choices = [L"\exp(-x) x^{1/2}",
L"\exp(-x) x^{1/2} \cdot 2x",
L"\exp(-x^2) (x^2)^{1/2}",
L"\exp(-x^2) (x^2)^{1/2}\cdot 2x"]
radioq(choices, 4; keep_order=true)
```
###### Question
The function `sinint` from `SpecialFunctions` computes
$$
F(x) = \int_0^x \frac{\sin(t)}{t} dt = \int_0^x \phi(t) dt,
$$
Where we define $\phi$ above to be $1$ when $t=0$, so that it will be continuous over $[0,x]$.
A related integral might be:
$$
G(x) = \int_0^x \frac{\sin(\pi t)}{\pi t} dt = \int_0^x \phi(\pi t) dt
$$
As this is an integral involving a simple transformation of $\phi(x)$, we can see that $G(x) = (1/\pi) F(\pi x)$. What is the derivative of $G$?
```{julia}
#| echo: false
choices = [
L"\phi(x)",
L"\phi(\pi x)",
L"\pi \phi(\pi x)"
]
radioq(choices, 2; keep_order=true)
```
###### Question
@@ -1144,12 +1401,14 @@ radioq(choices, answ, keep_order=true)
Barrow presented a version of the fundamental theorem of calculus in a 1670 volume edited by Newton, Barrow's student (cf. [Wagner](http://www.maa.org/sites/default/files/0746834234133.di020795.02p0640b.pdf)). His version can be stated as follows (cf. [Jardine](http://www.maa.org/publications/ebooks/mathematical-time-capsules)):
Consider the following figure where $f$ is a strictly increasing function with $f(0) = 0$. and $x > 0$. The function $A(x) = \int_0^x f(u) du$ is also plotted. The point $Q$ is $f(x)$, and the point $P$ is $A(x)$. The point $T$ is chosen to so that the length between $T$ and $x$ times the length between $Q$ and $x$ equals the length from $P$ to $x$. ($\lvert Tx \rvert \cdot \lvert Qx \rvert = \lvert Px \rvert$.) Barrow showed that the line segment $PT$ is tangent to the graph of $A(x)$. This figure illustrates the labeling for some function:
Consider the following figure where $f$ is a strictly increasing function with $f(0) = 0$. and $x > 0$. The function $A(x) = \int_0^x f(u) du$ is also plotted with a dashed red line. The point $Q$ is $f(x)$, and the point $P$ is $A(x)$. The point $T$ is chosen to so that the length between $T$ and $x$ times the length between $Q$ and $x$ equals the length from $P$ to $x$. ($\lvert Tx \rvert \cdot \lvert Qx \rvert = \lvert Px \rvert$.) Barrow showed that the line segment $PT$ is tangent to the graph of $A(x)$. This figure illustrates the labeling for some function:
```{julia}
#| hold: true
#| echo: false
let
gr()
f(x) = x^(2/3)
x = 2
A(x) = quadgk(f, 0, x)[1]
@@ -1160,14 +1419,21 @@ P = A(x)
secpt = u -> 0 + P/(x-T) * (u-T)
xs = range(0, stop=x+1/4, length=50
)
p = plot(f, 0, x + 1/4, legend=false)
plot!(p, A, 0, x + 1/4, color=:red)
p = plot(f, 0, x + 1/4, legend=false, line=(:black,2))
plot!(p, A, 0, x + 1/4, line=(:red, 2,:dash))
scatter!(p, [T, x, x, x], [0, 0, Q, P], color=:orange)
annotate!(p, collect(zip([T, x, x+.1, x+.1], [0-.15, 0-.15, Q-.1, P], ["T", "x", "Q", "P"])))
annotate!(p, collect(zip([T, x, x+.1, x+.1], [0-.15, 0-.15, Q-.1, P], [L"T", L"x", L"Q", L"P"])))
plot!(p, [T-1/4, x+1/4], map(secpt, [T-1/4, x + 1/4]), color=:orange)
plot!(p, [T, x, x], [0, 0, P], color=:green)
p
p
end
```
```{julia}
#| echo: false
plotly()
nothing
```
The fact that $\lvert Tx \rvert \cdot \lvert Qx \rvert = \lvert Px \rvert$ says what in terms of $f(x)$, $A(x)$ and $A'(x)$?

View File

@@ -33,20 +33,26 @@ function make_sqrt_x_graph(n)
b = 1
a = 1/2^n
xs = range(1/2^8, stop=b, length=250)
x1s = range(a, stop=b, length=50)
xs = range(1/2^n, stop=b, length=1000)
x1s = range(a, stop=b, length=1000)
@syms x
f(x) = 1/sqrt(x)
val = N(integrate(f(x), (x, 1/2^n, b)))
title = "area under f over [1/$(2^n), $b] is $(rpad(round(val, digits=2), 4))"
plt = plot(f, range(a, stop=b, length=251), xlim=(0,b), ylim=(0, 15), legend=false, size=fig_size, title=title)
plot!(plt, [b, a, x1s...], [0, 0, map(f, x1s)...], linetype=:polygon, color=:orange)
title = L"area under $f$ over $[2^{-%$n}, %$b]$ is $%$(rpad(round(val, digits=2), 4))$"
plt = plot(f, range(a, stop=b, length=1000);
xlim=(0,b), ylim=(0, 15),
legend=false,
title=title)
plot!(plt, [b, a, x1s...], [0, 0, map(f, x1s)...];
linetype=:polygon, color=:orange)
plt
end
caption = L"""
Area under $1/\sqrt{x}$ over $[a,b]$ increases as $a$ gets closer to $0$. Will it grow unbounded or have a limit?
@@ -133,7 +139,7 @@ The limit is infinite, so does not exist except in an extended sense.
Before showing this, we recall the fundamental theorem of calculus. The limit existing is the same as saying the limit of $F(M) - F(a)$ exists for an antiderivative of $f(x)$.
For this particular problem, it can be shown by integration by parts that for positive, integer values of $n$ that an antiderivative exists of the form $F(x) = p(x)e^{-x}$, where $p(x)$ is a polynomial of degree $n$. But we've seen that for any $n>0$, $\lim_{x \rightarrow \infty} x^n e^{-x} = 0$, so the same is true for any polynomial. So, $\lim_{M \rightarrow \infty} F(M) - F(1) = -F(1)$.
For this particular problem, it can be shown with integration by parts that for positive, integer values of $n$ that an antiderivative exists of the form $F(x) = p(x)e^{-x}$, where $p(x)$ is a polynomial of degree $n$. But we've seen that for any $n>0$, $\lim_{x \rightarrow \infty} x^n e^{-x} = 0,$ so the same is true for any polynomial. So, $\lim_{M \rightarrow \infty} F(M) - F(1) = -F(1)$.
* The function $e^x$ is integrable over $(-\infty, a]$ but not
@@ -175,6 +181,87 @@ As $M$ goes to $\infty$, this will converge to $1$.
limit(sympy.Si(M), M => oo)
```
##### Example
To formally find the limit as $x\rightarrow \infty$ of
$$
\text{Si}(x) = \int_0^\infty \frac{\sin(t)}{t} dt
$$
we introduce a trick and rely on some theorems that have not been discussed.
First, we notice that $\Si(x)$ is the value of $I(\alpha)$ when $\alpha=0$ where
$$
I(\alpha) = \int_0^\infty \exp(-\alpha t) \frac{\sin(t)}{t} dt
$$
We differentiate $I$ in $\alpha$ to get:
$$
\begin{align*}
I'(\alpha) &= \frac{d}{d\alpha} \int_0^\infty \exp(-\alpha t) \frac{\sin(t)}{t} dt \\
&= \int_0^\infty \frac{d}{d\alpha} \exp(-\alpha t) \frac{\sin(t)}{t} dt \\
&= \int_0^\infty (-t) \exp(-\alpha t) \frac{\sin(t)}{t} dt \\
&= -\int_0^\infty \exp(-\alpha t) \sin(t) dt \\
\end{align*}
$$
As illustrated previously, this integral can be integrated by parts, though here we have infinite limits and have adjusted for the minus sign:
$$
\begin{align*}
-I'(\alpha) &= \int_0^\infty \exp(-\alpha t) \sin(t) dt \\
&=\sin(t) \frac{-\exp(-\alpha t)}{\alpha} \Big|_0^\infty -
\int_0^\infty \frac{-\exp(-\alpha t)}{\alpha} \cos(t) dt \\
&= 0 + \frac{1}{\alpha} \cdot \int_0^\infty \exp(-\alpha t) \cos(t) dt \\
&= \frac{1}{\alpha} \cdot \cos(t)\frac{-\exp(-\alpha t)}{\alpha} \Big|_0^\infty -
\frac{1}{\alpha} \cdot \int_0^\infty \frac{-\exp(-\alpha t)}{\alpha} (-\sin(t)) dt \\
&= \frac{1}{\alpha^2} - \frac{1}{\alpha^2} \cdot \int_0^\infty \exp(-\alpha t) \sin(t) dt
\end{align*}
$$
Combining gives:
$$
\left(1 + \frac{1}{\alpha^2}\right) \int_0^\infty \exp(-\alpha t) \sin(t) dt = \frac{1}{\alpha^2}
$$
Solving gives the desired integral as
$$
I'(\alpha) = -\frac{1}{\alpha^2} / (1 + \frac{1}{\alpha^2}) = -\frac{1}{1 + \alpha^2}.
$$
This has a known antiderivative: $I(\alpha) = -\tan^{-1}(\alpha) + C$. As $\alpha \rightarrow \infty$ *if* we can pass the limit *inside* the integral, then $I(\alpha) \rightarrow 0$. So $\lim_{x \rightarrow \infty} -\tan^{-1}(x) + C = 0$ or $C = \pi/2$.
As our question is answered by $I(0)$, we get $I(0) = \tan^{-1}(0) + C = C = \pi/2$.
The above argument requires two places where a *limit* is passed inside the integral. The first involved the derivative. The [Leibniz integral rule](https://en.wikipedia.org/wiki/Leibniz_integral_rule) can be used to verify the first use is valid:
:::{.callout-note icon=false}
## Leibniz integral rule
If $f(x,t)$ and the derivative in $x$ for a fixed $t$ is continuous (to be discussed later) in a region containing $a(x) \leq t \leq b(x)$ and $x_0 < x < x_1$ and both $a(x)$ and $b(x)$ are continuously differentiable, then
$$
\frac{d}{dx}\int_{a(x)}^{b(x)} f(x, t) dt =
\int_{a(x)}^{b(x)} \frac{d}{dx}f(x,t) dt +
f(x, b(x)) \frac{d}{dx}b(x) - f(x, a(x)) \frac{d}{dx}a(x).
$$
:::
This extends the fundamental theorem of calculus for cases where the integrand also depends on $x$. In our use, both $a'(x)$ and $b'(x)$ are $0$.
[Uniform convergence](https://en.wikipedia.org/wiki/Uniform_convergence) can be used to establish the other.
### Numeric integration

View File

@@ -39,13 +39,13 @@ Now we turn our attention to the implications of the *product rule*: $[uv]' = u'
By the fundamental theorem of calculus:
$$
[u(x)\cdot v(x)]\big|_a^b = \int_a^b [u(x) v(x)]' dx = \int_a^b u'(x) \cdot v(x) dx + \int_a^b u(x) \cdot v'(x) dx.
[u(x)\cdot v(x)]\Big|_a^b = \int_a^b [u(x) v(x)]' dx = \int_a^b u'(x) \cdot v(x) dx + \int_a^b u(x) \cdot v'(x) dx.
$$
Or,
$$
\int_a^b u(x) v'(x) dx = [u(x)v(x)]\big|_a^b - \int_a^b v(x) u'(x)dx.
\int_a^b u(x) v'(x) dx = [u(x)v(x)]\Big|_a^b - \int_a^b v(x) u'(x)dx.
$$
:::
@@ -58,16 +58,16 @@ The following visually illustrates integration by parts:
#| label: fig-integration-by-parts
#| fig-cap: "Integration by parts figure ([original](http://en.wikipedia.org/wiki/Integration_by_parts#Visualization))"
let
## parts picture
## parts picture
gr()
u(x) = sin(x*pi/2)
v(x) = x
xs = range(0, stop=1, length=50)
a,b = 1/4, 3/4
p = plot(u, v, 0, 1, legend=false, axis=([], false))
plot!([0, u(1)], [0,0], line=(:black, 3))
plot!([0, 0], [0, v(1) ], line=(:black, 3))
plot!(p, zero, 0, 1)
p = plot(u, v, 0, 1; legend=false, axis=([], false), line=(:black,2))
plot!([0, u(1)], [0,0]; line=(:gray, 1), arrow=true, side=:head)
plot!([0, 0], [0, v(1) ]; line=(:gray, 1), arrow=true, side=:head)
xs = range(a, b, length=50)
plot!(Shape(vcat(u.(xs), reverse(u.(xs))),
@@ -81,21 +81,28 @@ plot!(p, [u(a),u(a),0, 0, u(b),u(b),u(a)],
[0, v(a), v(a), v(b), v(b), 0, 0],
linetype=:polygon, fill=(:brown3, 0.25))
annotate!(p, [(0.65, .25, "A"),
(0.4, .55, "B"),
(u(a),v(a) + .08, "(u(a),v(a))"),
(u(b),v(b)+.08, "(u(b),v(b))"),
(u(a),0, "u(a)",:top),
(u(b),0, "u(b)",:top),
(0, v(a), "v(a) ",:right),
(0, v(b), "v(b) ",:right)
annotate!(p, [(0.65, .25, text(L"A")),
(0.4, .55, text(L"B")),
(u(a),v(a), text(L"(u(a),v(a))", :bottom, :right)),
(u(b),v(b), text(L"(u(b),v(b))", :bottom, :right)),
(u(a),0, text(L"u(a)", :top)),
(u(b),0, text(L"u(b)", :top)),
(0, v(a), text(L"v(a)", :right)),
(0, v(b), text(L"v(b)", :right)),
(0,0, text(L"(0,0)", :top))
])
end
```
```{julia}
#| echo: false
plotly()
nothing
```
@fig-integration-by-parts shows a parametric plot of $(u(t),v(t))$ for $a \leq t \leq b$..
The total shaded area, a rectangle, is $u(b)v(b)$, the area of $A$ and $B$ combined is just $u(b)v(b) - u(a)v(a)$ or $[u(x)v(x)]\big|_a^b$. We will show that $A$ is $\int_a^b v(x)u'(x)dx$ and $B$ is $\int_a^b u(x)v'(x)dx$ giving the formula.
The total shaded area, a rectangle, is $u(b)v(b)$, the area of $A$ and $B$ combined is just $u(b)v(b) - u(a)v(a)$ or $[u(x)v(x)]\Big|_a^b$. We will show that $A$ is $\int_a^b v(x)u'(x)dx$ and $B$ is $\int_a^b u(x)v'(x)dx$ giving the formula.
We can compute $A$ by a change of variables with $x=u^{-1}(t)$ (so $u'(x)dx = dt$):
@@ -109,6 +116,7 @@ $$
$B$ is similar with the roles of $u$ and $v$ reversed.
----
Informally, the integration by parts formula is sometimes seen as $\int udv = uv - \int v du$, as well can be somewhat confusingly written as:
@@ -131,10 +139,10 @@ Consider the integral $\int_0^\pi x\sin(x) dx$. If we let $u=x$ and $dv=\sin(x)
$$
\begin{align*}
\int_0^\pi x\sin(x) dx &= \int_0^\pi u dv\\
&= uv\big|_0^\pi - \int_0^\pi v du\\
&= x \cdot (-\cos(x)) \big|_0^\pi - \int_0^\pi (-\cos(x)) dx\\
&= uv\Big|_0^\pi - \int_0^\pi v du\\
&= x \cdot (-\cos(x)) \Big|_0^\pi - \int_0^\pi (-\cos(x)) dx\\
&= \pi (-\cos(\pi)) - 0(-\cos(0)) + \int_0^\pi \cos(x) dx\\
&= \pi + \sin(x)\big|_0^\pi\\
&= \pi + \sin(x)\Big|_0^\pi\\
&= \pi.
\end{align*}
$$
@@ -166,8 +174,8 @@ Putting together gives:
$$
\begin{align*}
\int_1^2 x \log(x) dx
&= (\log(x) \cdot \frac{x^2}{2}) \big|_1^2 - \int_1^2 \frac{x^2}{2} \frac{1}{x} dx\\
&= (2\log(2) - 0) - (\frac{x^2}{4})\big|_1^2\\
&= (\log(x) \cdot \frac{x^2}{2}) \Big|_1^2 - \int_1^2 \frac{x^2}{2} \frac{1}{x} dx\\
&= (2\log(2) - 0) - (\frac{x^2}{4})\Big|_1^2\\
&= 2\log(2) - (1 - \frac{1}{4}) \\
&= 2\log(2) - \frac{3}{4}.
\end{align*}
@@ -204,7 +212,7 @@ Were this a definite integral problem, we would have written:
$$
\int_a^b \log(x) dx = (x\log(x))\big|_a^b - \int_a^b dx = (x\log(x) - x)\big|_a^b.
\int_a^b \log(x) dx = (x\log(x))\Big|_a^b - \int_a^b dx = (x\log(x) - x)\Big|_a^b.
$$
##### Example
@@ -214,14 +222,14 @@ Sometimes integration by parts is used two or more times. Here we let $u=x^2$ an
$$
\int_a^b x^2 e^x dx = (x^2 \cdot e^x)\big|_a^b - \int_a^b 2x e^x dx.
\int_a^b x^2 e^x dx = (x^2 \cdot e^x)\Big|_a^b - \int_a^b 2x e^x dx.
$$
But we can do $\int_a^b x e^xdx$ the same way:
$$
\int_a^b x e^x = (x\cdot e^x)\big|_a^b - \int_a^b 1 \cdot e^xdx = (xe^x - e^x)\big|_a^b.
\int_a^b x e^x = (x\cdot e^x)\Big|_a^b - \int_a^b 1 \cdot e^xdx = (xe^x - e^x)\Big|_a^b.
$$
Combining gives the answer:
@@ -229,8 +237,8 @@ Combining gives the answer:
$$
\int_a^b x^2 e^x dx
= (x^2 \cdot e^x)\big|_a^b - 2( (xe^x - e^x)\big|_a^b ) =
e^x(x^2 - 2x + 2) \big|_a^b.
= (x^2 \cdot e^x)\Big|_a^b - 2( (xe^x - e^x)\Big|_a^b ) =
e^x(x^2 - 2x + 2) \Big|_a^b.
$$
In fact, it isn't hard to see that an integral of $x^m e^x$, $m$ a positive integer, can be handled in this manner. For example, when $m=10$, `SymPy` gives:
@@ -247,14 +255,29 @@ The general answer is $\int x^n e^xdx = p(x) e^x$, where $p(x)$ is a polynomial
##### Example
The same technique is attempted for this integral, but ends differently. First in the following we let $u=\sin(x)$ and $dv=e^x dx$:
The same technique is attempted for the integral of $e^x\sin(x)$, but ends differently.
First we let $u=\sin(x)$ and $dv=e^x dx$, then
$$
du = \cos(x)dx \quad \text{and}\quad v = e^x.
$$
So:
$$
\int e^x \sin(x)dx = \sin(x) e^x - \int \cos(x) e^x dx.
$$
Now we let $u = \cos(x)$ and again $dv=e^x dx$:
Now we let $u = \cos(x)$ and again $dv=e^x dx$, then
$$
du = -\sin(x)dx \quad \text{and}\quad v = e^x.
$$
So:
$$
@@ -301,7 +324,7 @@ $$
This is called a reduction formula as it reduces the problem from an integral with a power of $n$ to one with a power of $n - 2$, so could be repeated until the remaining indefinite integral required knowing either $\int \cos(x) dx$ (which is $-\sin(x)$) or $\int \cos(x)^2 dx$, which by a double angle formula application, is $x/2 + \sin(2x)/4$.
`SymPy` is quite able to do this repeated bookkeeping. For example with $n=10$:
`SymPy` is able and willing to do this repeated bookkeeping. For example with $n=10$:
```{julia}
@@ -350,7 +373,7 @@ Using right triangles to simplify, the last value $\cos(\sin^{-1}(x))$ can other
The [trapezoid](http://en.wikipedia.org/wiki/Trapezoidal_rule) rule is an approximation to the definite integral like a Riemann sum, only instead of approximating the area above $[x_i, x_i + h]$ by a rectangle with height $f(c_i)$ (for some $c_i$), it uses a trapezoid formed by the left and right endpoints. That is, this area is used in the estimation: $(1/2)\cdot (f(x_i) + f(x_i+h)) \cdot h$.
Even though we suggest just using `quadgk` for numeric integration, estimating the error in this approximation is still of some theoretical interest.
Even though we suggest just using `quadgk` for numeric integration, estimating the error in this approximation is of theoretical interest.
Recall, just using *either* $x_i$ or $x_{i-1}$ for $c_i$ gives an error that is "like" $1/n$, as $n$ gets large, though the exact rate depends on the function and the length of the interval.
@@ -359,18 +382,18 @@ Recall, just using *either* $x_i$ or $x_{i-1}$ for $c_i$ gives an error that is
This [proof](http://www.math.ucsd.edu/~ebender/20B/77_Trap.pdf) for the error estimate is involved, but is reproduced here, as it nicely integrates many of the theoretical concepts of integration discussed so far.
First, for convenience, we consider the interval $x_i$ to $x_i+h$. The actual answer over this is just $\int_{x_i}^{x_i+h}f(x) dx$. By a $u$-substitution with $u=x-x_i$ this becomes $\int_0^h f(t + x_i) dt$. For analyzing this we integrate once by parts using $u=f(t+x_i)$ and $dv=dt$. But instead of letting $v=t$, we choose to add - as is our prerogative - a constant of integration $A$, so $v=t+A$:
First, for convenience, we consider the interval $x_i$ to $x_i+h$. The actual answer over this is just $\int_{x_i}^{x_i+h}f(x) dx$. By a $u$-substitution with $u=x-x_i$ this becomes $\int_0^h f(t + x_i) dt$. For analyzing this we integrate once by parts using $u=f(t+x_i)$ and $dv=dt$. But instead of letting $v=t$, we choose to add--as is our prerogative--a constant of integration $A$, so $v=t+A$:
$$
\begin{align*}
\int_0^h f(t + x_i) dt &= uv \big|_0^h - \int_0^h v du\\
&= f(t+x_i)(t+A)\big|_0^h - \int_0^h (t + A) f'(t + x_i) dt.
\int_0^h f(t + x_i) dt &= uv \Big|_0^h - \int_0^h v du\\
&= f(t+x_i)(t+A)\Big|_0^h - \int_0^h (t + A) f'(t + x_i) dt.
\end{align*}
$$
We choose $A$ to be $-h/2$, any constant is possible, for then the term $f(t+x_i)(t+A)\big|_0^h$ becomes $(1/2)(f(x_i+h) + f(x_i)) \cdot h$, or the trapezoid approximation. This means, the error over this interval - actual minus estimate - satisfies:
We choose $A$ to be $-h/2$, any constant is possible, for then the term $f(t+x_i)(t+A)\Big|_0^h$ becomes $(1/2)(f(x_i+h) + f(x_i)) \cdot h$, or the trapezoid approximation. This means, the error over this interval - actual minus estimate - satisfies:
$$
@@ -392,7 +415,7 @@ Again we added a constant of integration, $B$, to $v$. The error becomes:
$$
\text{error}_i = -(\frac{(t+A)^2}{2} + B)f'(t+x_i)\big|_0^h + \int_0^h (\frac{(t+A)^2}{2} + B) \cdot f''(t+x_i) dt.
\text{error}_i = -\left(\frac{(t+A)^2}{2} + B\right)f'(t+x_i)\Big|_0^h + \int_0^h \left(\frac{(t+A)^2}{2} + B\right) \cdot f''(t+x_i) dt.
$$
With $A=-h/2$, $B$ is chosen so $(t+A)^2/2 + B = 0$ at endpoints, or $B=-h^2/8$. The error becomes
@@ -406,14 +429,14 @@ Now, we assume the $\lvert f''(t)\rvert$ is bounded by $K$ for any $a \leq t \le
$$
\lvert \text{error}_i \rvert \leq K \int_0^h \lvert (\frac{(t-h/2)^2}{2} - \frac{h^2}{8}) \rvert dt.
\lvert \text{error}_i \rvert \leq K \int_0^h \lVert \left(\frac{(t-h/2)^2}{2} - \frac{h^2}{8}\right) \rVert dt.
$$
But what is the function in the integrand? Clearly it is a quadratic in $t$. Expanding gives $1/2 \cdot (t^2 - ht)$. This is negative over $[0,h]$ (and $0$ at these endpoints, so the integral above is just:
$$
\frac{1}{2}\int_0^h (ht - t^2)dt = \frac{1}{2} (\frac{ht^2}{2} - \frac{t^3}{3})\big|_0^h = \frac{h^3}{12}
\frac{1}{2}\int_0^h (ht - t^2)dt = \frac{1}{2} \left(\frac{ht^2}{2} - \frac{t^3}{3}\right)\Big|_0^h = \frac{h^3}{12}
$$
This gives the bound: $\vert \text{error}_i \rvert \leq K h^3/12$. The *total* error may be less, but is not more than the value found by adding up the error over each of the $n$ intervals. As our bound does not depend on the $i$, we have this sum satisfies:

View File

@@ -144,10 +144,10 @@ $$
So in particular $K$ is in $[m, M]$. But $m$ and $M$ correspond to values of $f(x)$, so by the intermediate value theorem, $K=f(c)$ for some $c$ that must lie in between $c_m$ and $c_M$, which means as well that it must be in $[a,b]$.
##### Proof of second part of Fundamental Theorem of Calculus
##### Proof of the second part of the Fundamental Theorem of Calculus
The mean value theorem is exactly what is needed to prove formally the second part of the Fundamental Theorem of Calculus. Again, suppose $f(x)$ is continuous on $[a,b]$ with $a < b$. For any $a < x < b$, we define $F(x) = \int_a^x f(u) du$. Then the derivative of $F$ exists and is $f$.
The mean value theorem is exactly what is needed to formally prove the second part of the Fundamental Theorem of Calculus. Again, suppose $f(x)$ is continuous on $[a,b]$ with $a < b$. For any $a < x < b$, we define $F(x) = \int_a^x f(u) du$. Then the derivative of $F$ exists and is $f$.
Let $h>0$. Then consider the forward difference $(F(x+h) - F(x))/h$. Rewriting gives:

View File

@@ -14,7 +14,9 @@ using SymPy
Integration is facilitated when an antiderivative for $f$ can be found, as then definite integrals can be evaluated through the fundamental theorem of calculus.
However, despite differentiation being an algorithmic procedure, integration is not. There are "tricks" to try, such as substitution and integration by parts. These work in some cases. However, there are classes of functions for which algorithms exist. For example, the `SymPy` `integrate` function mostly implements an algorithm that decides if an elementary function has an antiderivative. The [elementary](http://en.wikipedia.org/wiki/Elementary_function) functions include exponentials, their inverses (logarithms), trigonometric functions, their inverses, and powers, including $n$th roots. Not every elementary function will have an antiderivative comprised of (finite) combinations of elementary functions. The typical example is $e^{x^2}$, which has no simple antiderivative, despite its ubiquitousness.
However, despite differentiation being an algorithmic procedure, integration is not. There are "tricks" to try, such as substitution and integration by parts. These work in some cases--but not all!
However, there are classes of functions for which algorithms exist. For example, the `SymPy` `integrate` function mostly implements an algorithm that decides if an elementary function has an antiderivative. The [elementary](http://en.wikipedia.org/wiki/Elementary_function) functions include exponentials, their inverses (logarithms), trigonometric functions, their inverses, and powers, including $n$th roots. Not every elementary function will have an antiderivative comprised of (finite) combinations of elementary functions. The typical example is $e^{x^2}$, which has no simple antiderivative, despite its ubiquitousness.
There are classes of functions where an (elementary) antiderivative can always be found. Polynomials provide a case. More surprisingly, so do their ratios, *rational functions*.
@@ -238,7 +240,11 @@ $$
#### Examples
Find an antiderivative for $1/(x\cdot(x^2+1)^2)$.
Find an antiderivative for
$$
\frac{1}{x\cdot(x^2+1)^2}.
$$
We have a partial fraction decomposition is:
@@ -259,7 +265,11 @@ integrate(1/q, x)
---
Find an antiderivative of $1/(x^2 - 2x-3)$.
Find an antiderivative of
$$
\frac{1}{x^2 - 2x-3}.
$$
We again just let `SymPy` do the work. A partial fraction decomposition is given by:

View File

@@ -289,11 +289,11 @@ where $u = (x-\mu)/\sigma$, so $du = (1/\sigma) dx$.
This shows that integrals involving a normal density with parameters $\mu$ and $\sigma$ can be computed using the *standard* normal density with $\mu=0$ and $\sigma=1$. Unfortunately, there is no elementary antiderivative for $\exp(-u^2/2)$, so integrals for the standard normal must be numerically approximated.
There is a function `erf` in the `SpecialFunctions` package (which is loaded by `CalculusWithJulia`) that computes:
There is a function `erf` in the `SpecialFunctions` package (which is loaded by `CalculusWithJulia`) defined by:
$$
\int_0^x \frac{2}{\sqrt{\pi}} \exp(-t^2) dt
\text{erf}(x) = \frac{2}{\sqrt{\pi}}\int_0^x \exp(-t^2) dt
$$
A further change of variables by $t = u/\sqrt{2}$ (with $\sqrt{2}dt = du$) gives:

View File

@@ -63,7 +63,7 @@ $$
If the curve is parameterized by $(g(t), f(t))$ between $a$ and $b$ then the surface area is
$$
\int_a^b 2\pi f(t) \cdot \sqrt{g'(t)^2 + f'(t)^2} dx.
\int_a^b 2\pi f(t) \cdot \sqrt{g'(t)^2 + f'(t)^2} dt.
$$
These formulas do not add in the surface area of either of the ends.
@@ -90,11 +90,343 @@ To see why this formula is as it is, we look at the parameterized case, the firs
Let a partition of $[a,b]$ be given by $a = t_0 < t_1 < t_2 < \cdots < t_n =b$. This breaks the curve into a collection of line segments. Consider the line segment connecting $(g(t_{i-1}), f(t_{i-1}))$ to $(g(t_i), f(t_i))$. Rotating this around the $x$ axis will generate something approximating a disc, but in reality will be the frustum of a cone. What will be the surface area?
::: {#fig-surface-area}
```{julia}
#| echo: false
let
gr()
function projection_plane(v)
vx, vy, vz = v
a = [-vy, vx, 0] # v ⋅ a = 0
b = v × a # so v ⋅ b = 0
return (a/norm(a), b/norm(b))
end
function project(x, v)
â, b̂ = projection_plane(v)
(x ⋅ â, x ⋅ b̂) # (x ⋅ â) â + (x ⋅ b̂) b̂
end
radius(t) = 1 / (1 + exp(t))
t₀, tₙ = 0, 3
surf(t, θ) = [t, radius(t)*cos(θ), radius(t)*sin(θ)]
Consider a right-circular cone parameterized by an angle $\theta$ and the largest radius $r$ (so that the height satisfies $r/h=\tan(\theta)$). If this cone were made of paper, cut up a side, and laid out flat, it would form a sector of a circle, whose area would be $R^2\gamma/2$ where $R$ is the radius of the circle (also the side length of our cone), and $\gamma$ an angle that we can figure out from $r$ and $\theta$. To do this, we note that the arc length of the circle's edge is $R\gamma$ and also the circumference of the bottom of the cone so $R\gamma = 2\pi r$. With all this, we can solve to get $A = \pi r^2/\sin(\theta)$. But we have a frustum of a cone with radii $r_0$ and $r_1$, so the surface area is a difference: $A = \pi (r_1^2 - r_0^2) /\sin(\theta)$.
v = [2, -2, 1]
function plot_axes()
empty_style = (xaxis = ([], false),
yaxis = ([], false),
legend=false)
plt = plot(; empty_style...)
axis_values = [[(0,0,0), (3.5,0,0)], # x axis
[(0,0,0), (0, 2.0 * radius(0), 0)], # yaxis
[(0,0,0), (0, 0, 1.5 * radius(0))]] # z axis
for (ps, ax) ∈ zip(axis_values, ("x", "y", "z"))
p0, p1 = ps
a, b = project(p0, v), project(p1, v)
annotate!([(b...,text(ax, :bottom))])
plot!([a, b]; arrow=true, head=:tip, line=(:gray, 1)) # gr() allows arrows
end
plt
end
function psurf(v)
(t,θ) -> begin
v1, v2 = project(surf(t, θ), v)
[v1, v2] # or call collect to make a tuple into a vector
end
end
function detJ(F, t, θ)
∂θ = ForwardDiff.derivative(θ -> F(t, θ), θ)
∂t = ForwardDiff.derivative(t -> F(t, θ), t)
(ax, ay), (bx, by) = ∂θ, ∂t
ax * by - ay * bx
end
function cap!(t, v; kwargs...)
θs = range(0, 2pi, 100)
S = Shape(project.(surf.(t, θs), (v,)))
plot!(S; kwargs...)
end
## ----
G = psurf(v)
fold(F, t, θmin, θmax) = find_zero(θ -> detJ(F, t, θ), (θmin, θmax))
plt = plot_axes()
Relating this to our values in terms of $f$ and $g$, we have $r_1=f(t_i)$, $r_0 = f(t_{i-1})$, and $\sin(\theta) = \Delta f / \sqrt{(\Delta g)^2 + (\Delta f)^2}$, where $\Delta f = f(t_i) - f(t_{i-1})$ and similarly for $\Delta g$.
ts = range(t₀, tₙ, 100)
back_edge = fold.(G, ts, 0, pi)
front_edge = fold.(G, ts, pi, 2pi)
db = Dict(t => v for (t,v) in zip(ts, back_edge))
df = Dict(t => v for (t,v) in zip(ts, front_edge))
# basic shape
plt = plot_axes()
plot!(project.(surf.(ts, back_edge), (v,)); line=(:black, 1))
plot!(project.(surf.(ts, front_edge), (v,)); line=(:black, 1))
# add caps
cap!(t₀, v; fill=(:gray, 0.33))
cap!(tₙ, v; fill=(:gray, 0.33))
# add rotated surface segment
i,j = 33,38
a = ts[i]
θs = range(db[ts[i]], df[ts[i]], 100)
θs = reverse(range(db[ts[j]], df[ts[j]], 100))
function 𝐺(t,θ)
v1, v2 = G(t, θ)
(v1, v2)
end
S = Shape(vcat(𝐺.(ts[i], θs), 𝐺.(ts[j], θs)))
plot!(S)
θs = range(df[ts[i]], 2pi + db[ts[i]], 100)
plot!([𝐺(ts[i], θ) for θ in θs]; line=(:black, 1, :dash))
θs = range(df[ts[j]], 2pi + db[ts[j]], 100)
plot!([𝐺(ts[j], θ) for θ in θs]; line=(:black, 1))
plot!([project((ts[i], 0,0),v), 𝐺(ts[i],db[ts[i]])]; line=(:black, 1, :dot), arrow=true)
plot!([project((ts[j], 0,0),v), 𝐺(ts[j],db[ts[j]])]; line=(:black, 1, :dot), arrow=true)
# add shading
lightpt = [2, -2, 5] # from further above
H = psurf(lightpt)
light_edge = fold.(H, ts, pi, 2pi);
for (i, (t, top, bottom)) in enumerate(zip(ts, light_edge, front_edge))
λ = iseven(i) ? 1.0 : 0.8
top = bottom + λ*(top - bottom)
curve = [project(surf(t, θ), v) for θ in range(bottom, top, 20)]
plot!(curve, line=(:black, 1))
end
# annotations
_x, _y, _z = surf(ts[i],db[ts[i]])
__x, __y = project((_x, _y/2, _z/2), v)
_x, _y, _z = surf(ts[j],db[ts[j]])
__x, __y = project((_x, _y/2, _z/2), v)
# annotations
annotate!([
(__x, __y, text(L"r_i", :left, :top)),
(__x, __y, text(L"r_{i+1}",:left, :top)),
])
current()
end
```
```{julia}
#| echo: false
plotly()
nothing
```
Illustration of function $(g(t), f(t))$ rotated about the $x$ axis with a section shaded.
:::
Consider a right-circular cone parameterized by an angle $\theta$ which at a given height has radius $r$ and slant height $l$ (so that the height satisfies $r/l=\sin(\theta)$). If this cone were made of paper, cut up a side, and laid out flat, it would form a sector of a circle, as illustrated below:
::: {#fig-frustum-cone-area}
```{julia}
#| echo: false
p1 = let
gr()
function projection_plane(v)
vx, vy, vz = v
a = [-vy, vx, 0] # v ⋅ a = 0
b = v × a # so v ⋅ b = 0
return (a/norm(a), b/norm(b))
end
function project(x, v)
â, b̂ = projection_plane(v)
(x ⋅ â, x ⋅ b̂) # (x ⋅ â) â + (x ⋅ b̂) b̂
end
function plot_axes(v)
empty_style = (xaxis = ([], false),
yaxis = ([], false),
legend=false)
plt = plot(; empty_style..., aspect_ratio=:equal)
a,b,c,d,e = project.([(0,0,2), (0,0,3), surf(3, 3pi/2), surf(2, 3pi/2),(0,0,0)], (v,))
pts = [a,b,c,d,a]#project.([a,b,c,d,a], (v,))
plot!(pts; line=(:gray, 1))
plot!([c,d]; line=(:black, 2))
plot!([d, e,a]; line=(:gray, 1,1))
#plot!(project.([e,a,d,e],(v,)); line=(:gray, 1))
plt
end
function psurf(v)
(t,θ) -> begin
v1, v2 = project(surf(t, θ), v)
[v1, v2] # or call collect to make a tuple into a vector
end
end
function detJ(F, t, θ)
∂θ = ForwardDiff.derivative(θ -> F(t, θ), θ)
∂t = ForwardDiff.derivative(t -> F(t, θ), t)
(ax, ay), (bx, by) = ∂θ, ∂t
ax * by - ay * bx
end
function cap!(t, v; kwargs...)
θs = range(0, 2pi, 100)
S = Shape(project.(surf.(t, θs), (v,)))
plot!(S; kwargs...)
end
function fold(F, t, θmin, θmax)
𝐹(θ) = detJ(F, t, θ)
𝐹(θmin) * 𝐹(θmax) <= 0 || return NaN
find_zero(𝐹, (θmin, θmax))
end
radius(t) = t/2
t₀, tₙ = 0, 3
surf(t, θ) = [radius(t)*cos(θ), radius(t)*sin(θ), t] # z axis
v = [2, -2, 1]
G = psurf(v)
ts = range(t₀, tₙ, 100)
back_edge = fold.(G, ts, 0, pi)
front_edge = fold.(G, ts, pi, 2pi)
db = Dict(t => v for (t,v) in zip(ts, back_edge))
df = Dict(t => v for (t,v) in zip(ts, front_edge))
plt = plot_axes(v)
plot!(project.(surf.(ts, back_edge), (v,)); line=(:black, 1))
plot!(project.(surf.(ts, front_edge), (v,)); line=(:black, 1))
cap!(tₙ, v; fill=(:gray80, 0.33))
i = 67
tᵢ = ts[i] # tᵢ = 2.0
plot!(project.([surf.(tᵢ, θ) for θ in range(df[tᵢ], 2pi + db[tᵢ], 100)], (v,)))
# add surface to rotate
## add light
lightpt = [2, -2, 5] # from further above
H = psurf(lightpt)
light_edge = fold.(H, ts, pi, 2pi);
for (i, (t, top, bottom)) in enumerate(zip(ts, light_edge, front_edge))
λ = iseven(i) ? 1.0 : 0.8
(isnan(top) || isnan(bottom)) && continue
top = bottom + λ*(top - bottom)
curve = [project(surf(t, θ), v) for θ in range(bottom, top, 20)]
#plot!(curve, line=(:black, 1))
end
a,b,c = project(surf(tₙ, 3pi/2), v), project(surf(2, 3pi/2),v), project((0,0,0), v)
#plot!([a,b], line=(:black, 3))
#plot!([b,c]; line=(:black,2))
# annotations
_x,_y,_z = surf(tₙ, 3pi/2)
r1 = project((_x/2, _y/2, _z), v)
_x,_y,_z = surf(2, 3pi/2)
r2 = project((_x/2, _y/2, _z), v)
_x, _y, _z = surf(1/2, 3pi/2)
theta = project((_x/2, _y/2, _z), v)
a, b = project.((surf(3, 3pi/2), surf(2, 3pi/2)), (v,))
annotate!([
(r1..., text(L"r_2",:bottom)),
(r2..., text(L"r_1",:bottom)),
(theta..., text(L"\theta")),
(a..., text(L"l_2",:right, :top)),
(b..., text(L"l_1", :right, :top))
])
current()
end
p2 = let
θ = 2pi - pi/3
θs = range(2pi-θ, 2pi, 100)
r1, r2 = 2, 3
empty_style = (xaxis = ([], false),
yaxis = ([], false),
legend=false,
aspect_ratio=:equal)
plt = plot(; empty_style...)
plot!(r1.*cos.(θs), r1 .* sin.(θs); line=(:black, 1))
plot!(r2.*cos.(θs), r2 .* sin.(θs); line=(:black, 1))
plot!([(0,0),(r1,0)]; line=(:gray, 1, :dash))
plot!([(r1,0),(r2,0)]; line=(:black, 1))
s, c = sincos(2pi-θ)
plot!([(0,0),(r1,0)]; line=(:gray, 1, :dash))
plot!([(0,0), (r1*c, r1*s)]; line=(:gray, 1, :dash))
plot!([(r1,0),(r2,0)]; line=(:black, 1))
plot!([(r1*c, r1*s), (r2*c, r2*s)]; line=(:black, 2))
s,c = sincos((2pi - θ)/2)
annotate!([
(1/2*c, 1/2*s, text(L"\gamma")),
(r1*c, r1*s, text(L"l_1",:left, :top)),
(r2*c, r2*s, text(L"l_2", :left, :top)),
])
#=
δ = pi/8
scs = reverse(sincos.(range(2pi-θ, 2pi - θ + pi - δ,100)))
plot!([1/2 .* (c,s) for (s,c) in scs]; line=(:gray, 1,:dash), arrow=true, side=:head)
scs = sincos.(range(2pi - θ + pi + δ, 2pi,100))
plot!([1/2 .* (c,s) for (s,c) in scs]; line=(:gray, 1,:dash), arrow=true, side=:head)
=#
end
plot(p1, p2)
```
```{julia}
#| echo: false
plotly()
nothing
```
The surface of a frustum of a cone and the same area spread out flat. Angle $\gamma = 2\pi(1 - \sin(\theta)$.
:::
By comparing circumferences, it is seen that the angles $\theta$ and $\gamma$ are related by $\gamma = 2\pi(1 - \sin(\theta))$ (as $2\pi r_2 = 2\pi l_2\sin(\theta) = (2\pi-\gamma)/(2\pi) \cdot 2\pi l_2$). The values $l_i$ and $r_i$ are related by $r_i = l_i \sin(\theta)$. The area in both pictures is: $(\pi l_2^2 - \pi l_1^2) \cdot (2\pi-\gamma)/(2\pi)$ which simplifies to $\pi (l_2 + l_1) \cdot \sin(\theta) \cdot (l_2 - l_1)$ or $2\pi \cdot (r_2 - r_1)/2 \cdot \text{slant height}$.
Relating this to our values in terms of $f$ and $g$, we have $r_1=f(t_i)$, $r_0 = f(t_{i-1})$, and the slant height is related by $(l_2-l_1)^2 = (g(t_2)-g(t_1))^2 + (f(t_2) - f(t_1))^2$.
Putting this altogether we get that the surface area generarated by rotating the line segment around the $x$ axis is
@@ -102,7 +434,7 @@ Putting this altogether we get that the surface area generarated by rotating the
$$
\text{sa}_i = \pi (f(t_i)^2 - f(t_{i-1})^2) \cdot \sqrt{(\Delta g)^2 + (\Delta f)^2} / \Delta f =
\pi (f(t_i) + f(t_{i-1})) \cdot \sqrt{(\Delta g)^2 + (\Delta f)^2}.
2\pi \frac{f(t_i) + f(t_{i-1})}{2} \cdot \sqrt{(\Delta g)^2 + (\Delta f)^2}.
$$
(This is $2 \pi$ times the average radius times the slant height.)
@@ -122,7 +454,9 @@ $$
\text{SA} = \int_a^b 2\pi f(t) \sqrt{g'(t)^2 + f'(t)^2} dt.
$$
If we assume integrability of the integrand, then as our partition size goes to zero, this approximate surface area converges to the value given by the limit. (As with arc length, this needs a technical adjustment to the Riemann integral theorem as here we are evaluating the integrand function at four points ($t_i$, $t_{i-1}$, $\xi$ and $\psi$) and not just at some $c_i$. An figure appears at the end.
If we assume integrability of the integrand, then as our partition size goes to zero, this approximate surface area converges to the value given by the limit. (As with arc length, this needs a technical adjustment to the Riemann integral theorem as here we are evaluating the integrand function at four points ($t_i$, $t_{i-1}$, $\xi$ and $\psi$) and not just at some $c_i$.
#### Examples
@@ -176,7 +510,7 @@ F(1) - F(0)
### Plotting surfaces of revolution
The commands to plot a surface of revolution will be described more clearly later; for now we present them as simply a pattern to be followed in case plots are desired. Suppose the curve in the $x-y$ plane is given parametrically by $(g(u), f(u))$ for $a \leq u \leq b$.
The commands to plot a surface of revolution will be described more clearly later; for now we present them as simply a pattern to be followed in case plots are desired. Suppose the curve in the $x-z$ plane is given parametrically by $(g(u), f(u))$ for $a \leq u \leq b$.
To be concrete, we parameterize the circle centered at $(6,0)$ with radius $2$ by:
@@ -195,14 +529,14 @@ The plot of this curve is:
#| hold: true
us = range(a, b, length=100)
plot(g.(us), f.(us), xlims=(-0.5, 9), aspect_ratio=:equal, legend=false)
plot!([0,0],[-3,3], color=:red, linewidth=5) # y axis emphasis
plot!([3,9], [0,0], color=:green, linewidth=5) # x axis emphasis
plot!([(0, -3), (0, 3)], line=(:red, 5)) # z axis emphasis
plot!([(3, 0), (9, 0)], line=(:green, 5)) # x axis emphasis
```
Though parametric plots have a convenience constructor, `plot(g, f, a, b)`, we constructed the points with `Julia`'s broadcasting notation, as we will need to do for a surface of revolution. The `xlims` are adjusted to show the $y$ axis, which is emphasized with a layered line. The line is drawn by specifying two points, $(x_0, y_0)$ and $(x_1, y_1)$ in the form `[x0,x1]` and `[y0,y1]`.
Though parametric plots have a convenience constructor, `plot(g, f, a, b)`, we constructed the points with `Julia`'s broadcasting notation, as we will need to do for a surface of revolution. The `xlims` are adjusted to show the $y$ axis, which is emphasized with a layered line. The line is drawn by specifying two points, $(x_0, y_0)$ and $(x_1, y_1)$ using tuples and wrapping in a vector.
Now, to rotate this about the $y$ axis, creating a surface plot, we have the following pattern:
Now, to rotate this about the $z$ axis, creating a surface plot, we have the following pattern:
```{julia}
S(u,v) = [g(u)*cos(v), g(u)*sin(v), f(u)]
@@ -210,23 +544,22 @@ us = range(a, b, length=100)
vs = range(0, 2pi, length=100)
ws = unzip(S.(us, vs')) # reorganize data
surface(ws..., zlims=(-6,6), legend=false)
plot!([0,0], [0,0], [-3,3], color=:red, linewidth=5) # y axis emphasis
plot!([(0,0,-3), (0,0,3)], line=(:red, 5)) # z axis emphasis
```
The `unzip` function is not part of base `Julia`, rather part of `CalculusWithJulia` (it is really `SplitApplyCombine`'s `invert` function). This function rearranges data into a form consumable by the plotting methods like `surface`. In this case, the result of `S.(us,vs')` is a grid (matrix) of points, the result of `unzip` is three grids of values, one for the $x$ values, one for the $y$ values, and one for the $z$ values. A manual adjustment to the `zlims` is used, as `aspect_ratio` does not have an effect with the `plotly()` backend and errors on 3d graphics with `pyplot()`.
The `unzip` function is not part of base `Julia`, rather part of `CalculusWithJulia` (it is really `SplitApplyCombine`'s `invert` function). This function rearranges data into a form consumable by the plotting methods like `surface`. In this case, the result of `S.(us,vs')` is a grid (matrix) of points, the result of `unzip` is three grids of values, one for the $x$ values, one for the $y$ values, and one for the $z$ values. A manual adjustment to the `zlims` is used, as `aspect_ratio` does not have an effect with the `plotly()` backend.
To rotate this about the $x$ axis, we have this pattern:
```{julia}
#| hold: true
S(u,v) = [g(u), f(u)*cos(v), f(u)*sin(v)]
us = range(a, b, length=100)
vs = range(0, 2pi, length=100)
ws = unzip(S.(us,vs'))
surface(ws..., legend=false)
plot!([3,9], [0,0],[0,0], color=:green, linewidth=5) # x axis emphasis
plot([(3,0,0), (9,0,0)], line=(:green,5)) # x axis emphasis
surface!(ws..., legend=false)
```
The above pattern covers the case of rotating the graph of a function $f(x)$ of $a,b$ by taking $g(t)=t$.
@@ -551,46 +884,3 @@ a, b = 0, pi
val, _ = quadgk(t -> 2pi* f(t) * sqrt(g'(t)^2 + f'(t)^2), a, b)
numericq(val)
```
# Appendix
```{julia}
#| hold: true
#| echo: false
gr()
## For **some reason** having this in the natural place messes up the plots.
## {{{approximate_surface_area}}}
xs,ys = range(-1, stop=1, length=50), range(-1, stop=1, length=50)
f(x,y)= 2 - (x^2 + y^2)
dr = [1/2, 3/4]
df = [f(dr[1],0), f(dr[2],0)]
function sa_approx_graph(i)
p = plot(xs, ys, f, st=[:surface], legend=false)
for theta in range(0, stop=i/10*2pi, length=10*i )
path3d!(p,sin(theta)*dr, cos(theta)*dr, df)
end
p
end
n = 10
anim = @animate for i=1:n
sa_approx_graph(i)
end
imgfile = tempname() * ".gif"
gif(anim, imgfile, fps = 1)
caption = L"""
Surface of revolution of $f(x) = 2 - x^2$ about the $y$ axis. The lines segments are the images of rotating the secant line connecting $(1/2, f(1/2))$ and $(3/4, f(3/4))$. These trace out the frustum of a cone which approximates the corresponding surface area of the surface of revolution. In the limit, this approximation becomes exact and a formula for the surface area of surfaces of revolution can be used to compute the value.
"""
plotly()
ImageFile(imgfile, caption)
```

View File

@@ -19,11 +19,47 @@ using SymPy
```{julia}
#| echo: false
#| results: "hidden"
import LinearAlgebra: norm
import LinearAlgebra: norm, cross
using SplitApplyCombine
nothing
```
```{julia}
#| echo: false
# commands used for plotting from https://github.com/SigurdAngenent/WisconsinCalculus/blob/master/figures/221/09surf_of_rotation2.py
#linear projection of R^3 onto R^2
function _proj(X, v)
# a is ⟂ to v and b is v × a
vx, vy, vz = v
a = [-vy, vx, 0]
b = cross([vx,vy,vz], a)
a, b = a/norm(a), b/norm(b)
return (a ⋅ X, b ⋅ X)
end
# project a curve in R3 onto R2
pline(viewp, ps...) = [_proj(p, viewp) for p in ps]
# determinant of Jacobian; area multiplier
# det(J); used to identify folds
function jac(X, u, v)
return det(ForwardDiff.jacobian(xs -> collect(X(xs...)), [u,v]))
end
function _fold(F, t, θmin, θmax)
λ = θ -> jac(F, t, θ) # F is projected surface, psurf
iszero(λ(θmin)) && return θmin
iszero(λ(θmax)) && return θmax
return solve(ZeroProblem(λ, (θmin, θmax)))
end
nothing
```
---
@@ -133,38 +169,13 @@ plt = let
# plot surface of revolution around x axis between [0, 3]
# best if r(t) descreases
rad(x) = 2/(1 + exp(x))#2/(2.0+x)
viewp = [2,-2,1]
##
unitize(x) = x / norm(x)
"""Orthogonal projection along the vector viewp"""
function make_Pmat(viewp)
a = unitize( [-viewp[2], viewp[1], 0] )
b = unitize( [-viewp[3]*viewp[1],
-viewp[3]*viewp[2],
viewp[1]^2 + viewp[2]^2] )
collect(zip(a,b))
end
#linear projection of R^3 onto R^2
function proj(X, viewp)
Pmat = make_Pmat(viewp)
x=sum([Pmat[i][1]*X[i] for i in 1:3])
y=sum([Pmat[i][2]*X[i] for i in 1:3])
(x, y) # a point
end
proj(X) = proj(X, viewp)
# discrete determinant of Jacobian; area multiplier?
function jac(X, u, v)
ϵ = 0.000001
A = map((p,q) -> (p-q)/ϵ, X(u+ϵ/2, v), X(u-ϵ/2, v))
B = map((p,q) -> (p-q)/ϵ, X(u, v+ϵ/2), X(u, v-ϵ/2))
return A[1]*B[2]-A[2]*B[1]
end
rad(x) = 2/(1 + exp(x))
trange = (0,3)
θrange = (0, 2pi)
viewp = [2,-2, 1]
##
proj(X) = _proj(X, viewp)
# surface of revolution
surf(t, z) = [t, rad(t)*cos(z), rad(t)*sin(z)]
@@ -172,80 +183,63 @@ plt = let
# project the surface at (t, a=theta)
psurf(t,z) = proj(surf(t,z))
bisect(f, a, b) = find_zero(f, (a,b), Bisection())
_fold(t, zmin, zmax) = bisect(z -> jac(psurf, t, z), zmin, zmax)
# create shape holding project disc
drawdiscF(t) = Shape(invert([psurf(t, 2*i*pi/100) for i in 1:101])...)
# project a line between two points
pline!(p,q; kwargs...) = plot!([proj(p),proj(q)];
line_style..., kwargs...)
α = 1.0
line_style = (; line=(:black, 1))
α = 1.0 # opacity
line_style = (; line=(:black, 1))
plot(; empty_style..., aspect_ratio=:equal)
# by layering, we get x-axis as desired
pline!([-1,0,0], [0,0,0])
plot!(pline(viewp, [-1,0,0], [0,0,0]); line_style...)
plot!(drawdiscF(0); fill =(:lightgray, α))
pline!([0,0,0], [1,0,0])
plot!(pline(viewp, [0,0,0], [1,0,0]); line_style...)
plot!(drawdiscF(1); fill =(:black, α)) # black to lightgray gives thickness
plot!(drawdiscF(1.1); fill=(:lightgray, α))
pline!([1.1,0,0], [2,0,0])
plot!(pline(viewp, [1.1,0,0], [2,0,0]); line_style...)
plot!(drawdiscF(2); fill=(:lightgray, α))
pline!([2,0,0], [3,0,0])
plot!(pline(viewp, [2,0,0], [3,0,0]); line_style...)
plot!(drawdiscF(3); fill=(:lightgray, α))
pline!([3,0,0], [4,0,0]; arrow=true, side=:head)
pline!([0,0,0], [0,0,1.25]; arrow=true, side=:head)
plot!(pline(viewp, [3,0,0], [4,0,0]); line_style..., arrow=true, side=:head)
plot!(pline(viewp, [0,0,0], [0,0,1.25]); line_style..., arrow=true, side=:head)
tt = range(0, pi, 30)
curve = [psurf(t, pi/2) for t in tt]
tt = range(trange..., 30)
curve = psurf.(tt, pi/2)
plot!(curve; line=(:black, 2))
f1 = [[t, _fold(t, 0, pi)] for t in tt]
f1 = [(t, _fold(psurf, t, 0, pi)) for t in tt]
curve = [psurf(f[1], f[2]) for f in f1]
plot!(curve; line=(:black,))
plot!(curve; line=(:black,1))
f2 = [[t, _fold(t, pi, 2*pi)] for t in tt]
f2 = [(t, _fold(psurf, t, pi, 2*pi)) for t in tt]
curve = [psurf(f[1], f[2]) for f in f2]
plot!(curve; line=(:black,))
plot!(curve; line=(:black,1))
tt= [0.025*i for i in 1:121]
f1 = [[t, _fold(t, pi, 2*pi)] for t in tt]
for f in f1
plot!([psurf(f[1], f[2]-k*0.01*(4-f[1])) for k in 1:21];
line=(:black,1))
## find bottom edge (t,θ) again
tt = range(0, 3, 120)
f1 = [(t, _fold(psurf, t, pi, 2*pi)) for t in range(trange..., 100)]
# shade bottom by adding bigger density of lines near bottom
for (i,f) ∈ enumerate(f1)
λ = iseven(i) ? 6 : 4 # adjust density by have some lines only extend to 6
isnan(f[1]) || isnan(f[2]) && continue
curve = [psurf(f[1], θ) for θ in range(f[2] - 0.2*(λ - f[1]), f[2], 20)]
plot!(curve; line=(:black, 1))
end
tt= [0.05*i for i in 1:61]
f1 = [[t, _fold(t, pi, 2*pi)] for t in tt]
for f in f1
plot!([psurf( f[1], f[2]-k*0.01*(6-f[1]) )
for k in 1:21]; line=(:black, 1))
end
#=
ts = 0:.1:2.95
θs = range(pi + pi/4, 3pi/2, 25)
for ti in ts
plot!([psurf(ti,θ) for θ in θs]; line=(:black, 1))
end
θs = range(pi + pi/6, 3pi/2, 25)
for ti in ts
plot!([psurf(ti +0.05,θ) for θ in θs]; line=(:black, 1))
end
=#
current()
#ts = range(0, 3, 100)
#θs = range(0, 2pi, 100)
#contour(ts, θs, (t,z) -> jac(psurf,t,z), levels=[0])
end
plt
```
@@ -501,37 +495,13 @@ plt = let
# plot surface of revolution around x axis between [0, 3]
# best if r(t) descreases
rad(x) = 2/(1 + exp(x))#2/(2.0+x)
rad(x) = 2/(1 + exp(x))
trange = (0, 3)
θrange = (0, 2pi)
viewp = [2,-2,1]
##
unitize(x) = x / norm(x)
"""Orthogonal projection along the vector viewp"""
function make_Pmat(viewp)
a = unitize( [-viewp[2], viewp[1], 0] )
b = unitize( [-viewp[3]*viewp[1],
-viewp[3]*viewp[2],
viewp[1]^2 + viewp[2]^2] )
collect(zip(a,b))
end
#linear projection of R^3 onto R^2
function proj(X, viewp)
Pmat = make_Pmat(viewp)
x=sum([Pmat[i][1]*X[i] for i in 1:3])
y=sum([Pmat[i][2]*X[i] for i in 1:3])
(x, y) # a point
end
proj(X) = proj(X, viewp)
# discrete determinant of Jacobian; area multiplier?
function jac(X, u, v)
ϵ = 0.000001
A = map((p,q) -> (p-q)/ϵ, X(u+ϵ/2, v), X(u-ϵ/2, v))
B = map((p,q) -> (p-q)/ϵ, X(u, v+ϵ/2), X(u, v-ϵ/2))
return A[1]*B[2]-A[2]*B[1]
end
proj(X) = _proj(X, viewp)
# surface of revolution
@@ -542,67 +512,64 @@ plt = let
psurf(t,z) = proj(surf(t,z))
psurf2(t, z) = proj(surf2(t,z))
bisect(f, a, b) = find_zero(f, (a,b), Bisection())
_fold(t, zmin, zmax) = bisect(z -> jac(psurf, t, z), zmin, zmax)
# create shape holding project disc
drawdiscF(t) = Shape(invert([psurf(t, 2*i*pi/100) for i in 1:101])...)
drawdiscI(t) = Shape([psurf2(t, 2*i*pi/100) for i in 1:101])
# project a line between two points
pline!(p,q; kwargs...) = plot!([proj(p),proj(q)];
line_style..., kwargs...)
α = 1.0
line_style = (; line=(:black, 1))
plot(; empty_style..., aspect_ratio=:equal)
# by layering, we get x-axis as desired
pline!([-1,0,0], [0,0,0])
plot!(pline(viewp, [-1,0,0], [0,0,0]); line_style...)
plot!(drawdiscF(0); fill =(:lightgray, α))
plot!(drawdiscI(0); fill=(:white, .5))
pline!([0,0,0], [1,0,0])
plot!(pline(viewp, [0,0,0], [1,0,0]); line_style...)
plot!(drawdiscF(1); fill =(:black, α)) # black to lightgray gives thickness
plot!(drawdiscI(1); fill=(:white, .5))
plot!(drawdiscF(1.1); fill=(:lightgray, α))
plot!(drawdiscI(1.1); fill=(:white, .5))
pline!([1.1,0,0], [2,0,0])
plot!(pline(viewp, [1.1,0,0], [2,0,0]); line_style...)
plot!(drawdiscF(2); fill=(:lightgray, α))
plot!(drawdiscI(2); fill=(:white, .5))
pline!([2,0,0], [3,0,0])
plot!(pline(viewp, [2,0,0], [3,0,0]); line_style...)
plot!(drawdiscF(3); fill=(:lightgray, α))
plot!(drawdiscI(3); fill=(:white, .5))
pline!([3,0,0], [4,0,0]; arrow=true, side=:head)
pline!([0,0,0], [0,0,1.25]; arrow=true, side=:head)
plot!(pline(viewp, [3,0,0], [4,0,0]); line_style..., arrow=true, side=:head)
plot!(pline(viewp, [0,0,0], [0,0,1.25]); line_style..., arrow=true, side=:head)
tt = range(0, pi, 30)
## bounding curves
### main spine
tt = range(trange..., 30)
curve = [psurf(t, pi/2) for t in tt]
plot!(curve; line=(:black, 2))
f1 = [[t, _fold(t, 0, pi)] for t in tt]
### the folds
f1 = [(t, _fold(psurf, t, 0, pi)) for t in tt]
curve = [psurf(f[1], f[2]) for f in f1]
plot!(curve; line=(:black,))
f2 = [[t, _fold(t, pi, 2*pi)] for t in tt]
f2 = [(t, _fold(psurf, t, pi, 2*pi)) for t in tt]
curve = [psurf(f[1], f[2]) for f in f2]
plot!(curve; line=(:black,))
## add shading
### find bottom edge (t,θ) again
f1 = [[t, _fold(psurf, t, pi, 2*pi)] for t in range(trange..., 120)]
### shade bottom by adding bigger density of lines near bottom
for (i,f) ∈ enumerate(f1)
λ = iseven(i) ? 6 : 4 # adjust density by have some lines only extend to 6
isnan(f[1]) || isnan(f[2]) && continue
tt= [0.025*i for i in 1:121]
f1 = [[t, _fold(t, pi, 2*pi)] for t in tt]
for f in f1
plot!([psurf(f[1], f[2]-k*0.01*(4-f[1])) for k in 1:21];
line=(:black,1))
end
tt= [0.05*i for i in 1:61]
f1 = [[t, _fold(t, pi, 2*pi)] for t in tt]
for f in f1
plot!([psurf( f[1], f[2]-k*0.01*(6-f[1]) )
for k in 1:21]; line=(:black, 1))
curve = [psurf(f[1], θ) for θ in range(f[2] - 0.2*(λ - f[1]), f[2], 20)]
plot!(curve; line=(:black, 1))
end
current()
@@ -686,44 +653,12 @@ Let $h$ be the distance from the apex to the base. Consider cones with the prope
plt = let
gr()
rad(t) = 3/2 - t
trange = (0, 3/2)
θrange = (0, 2pi)
viewp = [2,-1/1.5,1/2+.2]
empty_style = (xaxis=([], false),
yaxis=([], false),
framestyle=:origin,
legend=false)
axis_style = (arrow=true, side=:head, line=(:gray, 1))
unitize(x) = x / norm(x)
"""Orthogonal projection along the vector viewp"""
function make_Pmat(viewp)
a = unitize( [-viewp[2], viewp[1], 0] )
b = unitize( [-viewp[3]*viewp[1],
-viewp[3]*viewp[2],
viewp[1]^2 + viewp[2]^2] )
collect(zip(a,b))
end
#linear projection of R^3 onto R^2
function proj(X, viewp)
Pmat = make_Pmat(viewp)
x=sum([Pmat[i][1]*X[i] for i in 1:3])
y=sum([Pmat[i][2]*X[i] for i in 1:3])
(x, y) # a point
end
proj(X) = proj(X, viewp)
drawdiscF(t) = Shape([psurf(t, 2*i*pi/100) for i in 1:101])
# discrete determinant of Jacobian; area multiplier?
function jac(X, u, v)
ϵ = 0.000001
A = map((p,q) -> (p-q)/ϵ, X(u+ϵ/2, v), X(u-ϵ/2, v))
B = map((p,q) -> (p-q)/ϵ, X(u, v+ϵ/2), X(u, v-ϵ/2))
return A[1]*B[2]-A[2]*B[1]
end
##
proj(X) = _proj(X, viewp)
# our surface
R, r, rho = 1, 1/4, 1/4
f(t) = (R-r) * cos(t) + rho * cos((R-r)/r * t)
@@ -731,6 +666,17 @@ plt = let
surf(t, θ) = (rad(t)*f(θ), rad(t)*g(θ), t)
psurf(t,θ) = proj(surf(t,θ))
empty_style = (xaxis=([], false),
yaxis=([], false),
framestyle=:origin,
legend=false)
axis_style = (arrow=true, side=:head, line=(:gray, 1))
drawdiscF(t) = Shape([psurf(t, 2*i*pi/100) for i in 1:101])
plot(; empty_style..., aspect_ratio=:equal)
for (i,t) in enumerate(range(0, 3/2, 30))
plot!(drawdiscF(t); fill=(:gray,1), line=(:black,1))
@@ -943,38 +889,37 @@ plt = let
rad2(t) = 1/2
viewp = [2,-2,1]
##
function _proj(X, v)
# a is ⟂ to v and b is v × a
vx, vy, vz = v
a = [-vy, vx, 0]
b = cross([vx,vy,vz], a)
a, b = a/norm(a), b/norm(b)
return (a ⋅ X, b ⋅ X)
end
# project a curve in R3 onto R2
pline(viewp, ps...) = [_proj(p, viewp) for p in ps]
# determinant of Jacobian; area multiplier
# det(J); used to identify folds
function jac(X, u, v)
return det(ForwardDiff.jacobian(xs -> collect(X(xs...)), [u,v]))
end
function _fold(F, t, θmin, θmax)
λ = θ -> jac(F, t, θ) # F is projected surface, psurf
iszero(λ(θmin)) && return θmin
iszero(λ(θmax)) && return θmax
return solve(ZeroProblem(λ, (θmin, θmax)))
end
##
unitize(x) = x / norm(x)
"""Orthogonal projection along the vector viewp"""
function make_Pmat(viewp)
a = unitize( [-viewp[2], viewp[1], 0] )
b = unitize( [-viewp[3]*viewp[1],
-viewp[3]*viewp[2],
viewp[1]^2 + viewp[2]^2] )
collect(zip(a,b))
end
#linear projection of R^3 onto R^2
function proj(X, viewp)
Pmat = make_Pmat(viewp)
x=sum([Pmat[i][1]*X[i] for i in 1:3])
y=sum([Pmat[i][2]*X[i] for i in 1:3])
(x, y) # a point
end
proj(X) = proj(X, viewp)
# discrete determinant of Jacobian; area multiplier?
function jac(X, u, v)
ϵ = 0.000001
A = map((p,q) -> (p-q)/ϵ, X(u+ϵ/2, v), X(u-ϵ/2, v))
B = map((p,q) -> (p-q)/ϵ, X(u, v+ϵ/2), X(u, v-ϵ/2))
return A[1]*B[2]-A[2]*B[1]
end
proj(X) = _proj(X, viewp)
# surface of revolution about the z axis
surf(t, z) = (rad(t)*cos(z), rad(t)*sin(z), t)
surf2(t, z) = (rad2(t)*cos(z), rad2(t)*sin(z), t)
@@ -983,15 +928,12 @@ plt = let
psurf2(t, z) = proj(surf2(t,z))
bisect(f, a, b) = find_zero(f, (a,b), Bisection())
_fold(t, zmin, zmax) = bisect(z -> jac(psurf, t, z), zmin, zmax)
_foldz(z, tmin, tmax) = bisect(t -> jac(psurf, t, z), tmin, tmax)
# create shape holding project disc
drawdiscF(t) = Shape([psurf(t, 2*i*pi/100) for i in 1:101])
drawdiscI(t) = Shape([psurf2(t, 2*i*pi/100) for i in 1:101])
# project a line between two points
pline!(p,q; kwargs...) = plot!([proj(p),proj(q)];
line_style..., kwargs...)
α = 1.0
line_style = (; line=(:black, 1))
@@ -1014,17 +956,8 @@ plt = let
plot!(drawdiscI(x₀); fill=(:white,1.0), line=(:black,1))
z0 = 3pi/2 - δ
pline!(surf(t0, z0), surf(-t0, z0); line=(:black, 1))
pline!(surf(t0, z0+pi), surf(-t0, z0+pi); line=(:black, 1))
# boundary of sphere
z0 = 3pi/2 - δ
curve = [psurf(t, z0) for t in range(-t0, t0, 100)]
plot!(curve; line=(:black,3))
z0 = 3pi/2 - δ + pi
curve = [psurf(t, z0) for t in range(-t0, t0, 100)]
plot!(curve; line=(:black,3))
plot!(pline(viewp, surf(t0, z0), surf(-t0, z0)); line=(:black, 1))
plot!(pline(viewp, surf(t0, z0+pi), surf(-t0, z0+pi)); line=(:black, 1))
# caps
curve = [psurf(t0, θ) for θ in range(0, 2pi, 100)]
@@ -1033,6 +966,17 @@ plt = let
plot!(curve, line=(:black, 2))
## folds
tθs = [(t, _fold(psurf, t, 0,pi)) for t in range(-t0, t0, 50)]
curve = [psurf(t, θ) for (t,θ) ∈ tθs]
plot!(curve, line=(:black, 3))
tθs = [(t, _fold(psurf, t, pi, 2pi)) for t in range(-t0, t0, 50)]
curve = [psurf(t, θ) for (t,θ) ∈ tθs]
plot!(curve, line=(:black, 3))
# Shade lines
δ = pi/6
Δₜ = (4pi/2 - (3pi/2 - δ))/(2*25)
@@ -1046,7 +990,7 @@ plt = let
end
#=
f1 = [[t, _fold(t, 0, pi/2)] for t in range(-0.5, -0.1, 26)]
f1 = [[t, _fold(psurf, t, 0, pi/2)] for t in range(-0.5, -0.1, 26)]
for f in f1
plot!([psurf( f[1], f[2]-k*0.01*(6-f[1]) )
for k in 1:21]; line=(:black, 1))

View File

@@ -72,7 +72,7 @@ The definition says three things
* The value of the limit is the same as $f(c)$.
The defined speaks to continuity at a point, we can extend it to continuity over an interval $(a,b)$ by saying:
The definition speaks to continuity at a point, we can extend it to continuity over an interval $(a,b)$ by saying:
::: {.callout-note icon=false}
## Definition of continuity over an open interval
@@ -130,9 +130,9 @@ There are various reasons why a function may not be continuous.
$$
f(x) = \begin{cases}
-1 & x < 0 \\
0 & x = 0 \\
1 & x > 0
-1 &~ x < 0 \\
0 &~ x = 0 \\
1 &~ x > 0
\end{cases}
$$
@@ -148,25 +148,57 @@ is implemented by `Julia`'s `sign` function. It has a value at $0$, but no limit
plot([-1,-.01], [-1,-.01], legend=false, color=:black)
plot!([.01, 1], [.01, 1], color=:black)
scatter!([0], [1/2], markersize=5, markershape=:circle)
ts = range(0, 2pi, 100)
C = Shape(0.02 * sin.(ts), 0.03 * cos.(ts))
plot!(C, fill=(:white,1), line=(:black, 1))
```
is not continuous at $x=0$. It has a limit of $0$ at $0$, a function value $f(0) =1/2$, but the limit and the function value are not equal.
* The `floor` function, which rounds down to the nearest integer, is also not continuous at the integers, but is right continuous at the integers, as, for example, $\lim_{x \rightarrow 0+} f(x) = f(0)$. This graph emphasizes the right continuity by placing a point for the value of the function when there is a jump:
* The `floor` function, which rounds down to the nearest integer, is also not continuous at the integers, but is right continuous at the integers, as, for example, $\lim_{x \rightarrow 0+} f(x) = f(0)$. This graph emphasizes the right continuity by placing a filled marker for the value of the function when there is a jump and an open marker where the function is not that value.
```{julia}
#| hold: true
#| echo: false
x = [0,1]; y=[0,0]
plt = plot(x.-2, y.-2, color=:black, legend=false)
plot!(plt, x.-1, y.-1, color=:black)
plot!(plt, x.-0, y.-0, color=:black)
plot!(plt, x.+1, y.+1, color=:black)
plot!(plt, x.+2, y.+2, color=:black)
scatter!(plt, [-2,-1,0,1,2], [-2,-1,0,1,2], markersize=5, markershape=:circle)
plt = let
empty_style = (xticks=-4:4, yticks=-4:4,
framestyle=:origin,
legend=false)
axis_style = (arrow=true, side=:head, line=(:gray, 1))
text_style = (10,)
fn_style = (;line=(:black, 3))
fn2_style = (;line=(:red, 4))
mark_style = (;line=(:gray, 1, :dot))
domain_style = (;fill=(:orange, 0.35), line=nothing)
range_style = (; fill=(:blue, 0.35), line=nothing)
ts = range(0, 2pi, 100)
xys = sincos.(ts)
xys = [.1 .* xy for xy in xys]
plot(; empty_style..., aspect_ratio=:equal)
plot!([-4.25,4.25], [0,0]; axis_style...)
plot!([0,0], [-4.25, 4.25]; axis_style...)
for k in -4:4
P,Q = (k,k),(k+1,k)
plot!([P,Q], line=(:black,1))
S = Shape([k .+ xy for xy in xys])
plot!(S; fill=(:black,))
S = Shape([(k+1,k) .+ xy for xy in xys])
plot!(S; fill=(:white,), line=(:black,1))
end
current()
end
plt
```
```{julia}
#| echo: false
plotly()
nothing
```
* The function $f(x) = 1/x^2$ is not continuous at $x=0$: $f(x)$ is not defined at $x=0$ and $f(x)$ has no limit at $x=0$ (in the usual sense).
@@ -176,8 +208,8 @@ plt
$$
f(x) =
\begin{cases}
0 & \text{if } x \text{ is irrational,}\\
1 & \text{if } x \text{ is rational.}
0 &~ \text{if } x \text{ is irrational,}\\
1 &~ \text{if } x \text{ is rational.}
\end{cases}
$$
@@ -192,8 +224,8 @@ Let a function be defined by cases:
$$
f(x) = \begin{cases}
3x^2 + c & x \geq 0,\\
2x-3 & x < 0.
3x^2 + c &~ x \geq 0,\\
2x-3 &~ x < 0.
\end{cases}
$$
@@ -383,8 +415,8 @@ Let $f(x)$ be defined by
$$
f(x) = \begin{cases}
c + \sin(2x - \pi/2) & x > 0\\
3x - 4 & x \leq 0.
c + \sin(2x - \pi/2) &~ x > 0\\
3x - 4 &~ x \leq 0.
\end{cases}
$$
@@ -423,12 +455,22 @@ Consider the function $f(x)$ given by the following graph
```{julia}
#| hold: true
#| echo: false
xs = range(0, stop=2, length=50)
plot(xs, [sqrt(1 - (x-1)^2) for x in xs], legend=false, xlims=(0,4))
plot!([2,3], [1,0])
scatter!([3],[0], markersize=5)
plot!([3,4],[1,0])
scatter!([4],[0], markersize=5)
let
xs = range(0, stop=2, length=50)
plot(xs, [sqrt(1 - (x-1)^2) for x in xs];
line=(:black,1),
legend=false, xlims=(-0.1,4.1))
plot!([2,3], [1,0]; line=(:black,1))
plot!([3,4],[1,0]; line=(:black,1))
scatter!([(0,0)], markersize=5, markercolor=:black)
scatter!([(2,0)], markersize=5, markercolor=:white)
scatter!([(2, 1)], markersize=5; markercolor=:black)
scatter!([(3,0)], markersize=5; markercolor=:black)
scatter!([(3,1)], markersize=5; markercolor=:white)
scatter!([(4,0)], markersize=5; markercolor=:black)
end
```
The function $f(x)$ is continuous at $x=1$?
@@ -513,3 +555,29 @@ choices = ["Can't tell",
answ = 1
radioq(choices, answ)
```
###### Question
A parametric equation is specified by a parameterization $(f(t), g(t)), a \leq t \leq b$. The parameterization will be continuous if and only if each function is continuous.
Suppose $k_x$ and $k_y$ are positive integers and $a, b$ are positive numbers, will the [Lissajous](https://en.wikipedia.org/wiki/Parametric_equation#Lissajous_Curve) curve given by $(a\cos(k_x t), b\sin(k_y t))$ be continuous?
```{julia}
#| hold: true
#| echo: false
yesnoq(true)
```
Here is a sample graph for $a=1, b=2, k_x=3, k_y=4$:
```{julia}
#| hold: true
a,b = 1, 2
k_x, k_y = 3, 4
plot(t -> a * cos(k_x *t), t-> b * sin(k_y * t), 0, 4pi)
```

View File

@@ -17,9 +17,9 @@ using SymPy
---
![Between points M and M lies an F](figures/ivt.jpg){width=40%}
![Between points M and M lies an F for a continuous curve. [L'Hospitals](https://ia801601.us.archive.org/26/items/infinimentpetits1716lhos00uoft/infinimentpetits1716lhos00uoft.pdf) figure 55.](figures/ivt.jpg){width=40%}
Continuity for functions is a valued property which carries implications. In this section we discuss two: the intermediate value theorem and the extreme value theorem. These two theorems speak to some fundamental applications of calculus: finding zeros of a function and finding extrema of a function. [L'Hospitals](https://ia801601.us.archive.org/26/items/infinimentpetits1716lhos00uoft/infinimentpetits1716lhos00uoft.pdf) figure 55, above, suggests why.
Continuity for functions is a valued property which carries implications. In this section we discuss two: the intermediate value theorem and the extreme value theorem. These two theorems speak to some fundamental applications of calculus: finding zeros of a function and finding extrema of a function.
## Intermediate Value Theorem
@@ -38,7 +38,7 @@ If $f$ is continuous on $[a,b]$ with, say, $f(a) < f(b)$, then for any $y$ with
#| echo: false
#| cache: true
### {{{IVT}}}
plt = let
let
gr()
# IVT
empty_style = (xaxis=([], false),
@@ -68,7 +68,7 @@ plt = let
plot!([(a,0),(a,f(a))]; line=(:black, 1, :dash))
plot!([(b,0),(b,f(b))]; line=(:black, 1, :dash))
m = f(a/2 + b/2)
m = f(a/2 + b/2) + 1.5
plot!([a, b], [m,m]; line=(:black, 1, :dashdot))
δx = 0.03
@@ -76,10 +76,10 @@ plt = let
domain_style...)
plot!(Shape((a-.1) .+ 2δx * [-1,1,1,-1], [f(a),f(a),f(b), f(b)]);
range_style...)
plot!(Shape((a-.1) .+ δx/2 * [-1,1,1,-1], [y0,y0,y1,y1]);
plot!(Shape((a-.1) .+ 2δx/3 * [-1,1,1,-1], [y0,y0,y1,y1]);
range_style...)
zs = find_zeros(f, (a,b))
zs = find_zeros(x -> f(x) - m, (a,b))
c = zs[2]
plot!([(c,0), (c,f(c))]; line=(:black, 1, :dashdot))
@@ -89,52 +89,11 @@ plt = let
(c, 0, text(L"c", 12, :top)),
(a-.1, f(a), text(L"f(a)", 12, :right)),
(a-.1, f(b), text(L"f(b)", 12, :right)),
(b, m, text(L"y", 12, :left)),
(a-0.2, m, text(L"y", 12, :right)),
])
end
plt
#=
function IVT_graph(n)
f(x) = sin(pi*x) + 9x/10
a,b = [0,3]
xs = range(a,stop=b, length=50)
## cheat -- pick an x, then find a y
Δ = .2
x = range(a + Δ, stop=b - Δ, length=6)[n]
y = f(x)
plt = plot(f, a, b, legend=false, size=fig_size)
plot!(plt, [0,x,x], [f(x),f(x),0], color=:orange, linewidth=3)
plt
end
n = 6
anim = @animate for i=1:n
IVT_graph(i)
end
imgfile = tempname() * ".gif"
gif(anim, imgfile, fps = 1)
caption = L"""
Illustration of intermediate value theorem. The theorem implies that any randomly chosen $y$
value between $f(a)$ and $f(b)$ will have at least one $x$ in $[a,b]$
with $f(x)=y$.
"""
plotly()
ImageFile(imgfile, caption)
=#
```
```{julia}
#| echo: false
plotly()
@@ -145,7 +104,7 @@ Illustration of the intermediate value theorem. The theorem implies that any ran
:::
In the early years of calculus, the intermediate value theorem was intricately connected with the definition of continuity, now it is a consequence.
In the early years of calculus, the intermediate value theorem was intricately connected with the definition of continuity, now it is an important consequence.
The basic proof starts with a set of points in $[a,b]$: $C = \{x \text{ in } [a,b] \text{ with } f(x) \leq y\}$. The set is not empty (as $a$ is in $C$) so it *must* have a largest value, call it $c$ (this might seem obvious, but it requires the completeness property of the real numbers). By continuity of $f$, it can be shown that $\lim_{x \rightarrow c-} f(x) = f(c) \leq y$ and $\lim_{y \rightarrow c+}f(x) =f(c) \geq y$, which forces $f(c) = y$.
@@ -157,18 +116,6 @@ The basic proof starts with a set of points in $[a,b]$: $C = \{x \text{ in } [a,
Suppose we have a continuous function $f(x)$ on $[a,b]$ with $f(a) < 0$ and $f(b) > 0$. Then as $f(a) < 0 < f(b)$, the intermediate value theorem guarantees the existence of a $c$ in $[a,b]$ with $f(c) = 0$. This was a special case of the intermediate value theorem proved by Bolzano first. Such $c$ are called *zeros* of the function $f$.
We use this fact when building a "sign chart" of a polynomial function. Between any two consecutive real zeros the polynomial can not change sign. (Why?) So a "test point" can be used to determine the sign of the function over an entire interval.
The `sign_chart` function from `CalculusWithJulia` uses this to indicate where an *assumed* continuous function changes sign:
```{julia}
f(x) = sin(x + x^2) + x/2
sign_chart(f, -3, 3)
```
The intermediate value theorem can find the sign of the function *between* adjacent zeros, but how are the zeros identified?
Here, we use the Bolzano theorem to give an algorithm - the *bisection method* - to locate a value $c$ in $[a,b]$ with $f(c) = 0$ under the assumptions:
* $f$ is continuous on $[a,b]$
@@ -178,7 +125,7 @@ Here, we use the Bolzano theorem to give an algorithm - the *bisection method* -
::: {.callout-note}
#### Between
The bisection method is used to find a zero, $c$, of $f(x)$ *between* two values, $a$ and $b$. The method is guaranteed to work under assumptions, the most important being the continuous function having different signs at $a$ and $b$.
The bisection method is used to find a zero, $c$, of $f(x)$ *between* two values, $a$ and $b$. The method is guaranteed to work under the assumption of a continuous function having different signs at $a$ and $b$.
:::
@@ -305,7 +252,7 @@ sin(c)
(Even `1pi` itself is not a "zero" due to floating point issues.)
### The `find_zero` function.
### The `find_zero` function to solve `f(x) = 0`
The `Roots` package has a function `find_zero` that implements the bisection method when called as `find_zero(f, (a,b))` where $[a,b]$ is a bracket. Its use is similar to `simple_bisection` above. This package is loaded when `CalculusWithJulia` is. We illlustrate the usage of `find_zero` in the following:
@@ -315,8 +262,8 @@ The `Roots` package has a function `find_zero` that implements the bisection met
xstar = find_zero(sin, (3, 4))
```
:::{.callout-warning}
## Warning
:::{.callout-note}
## Action template
Notice, the call `find_zero(sin, (3, 4))` again fits the template `action(function, args...)` that we see repeatedly. The `find_zero` function can also be called through `fzero`. The use of `(3, 4)` to specify the interval is not necessary. For example `[3,4]` would work equally as well. (Anything where `extrema` is defined works.)
:::
@@ -396,6 +343,10 @@ It appears (and a plot over $[0,1]$ verifies) that there is one zero between $-2
find_zero(x^3 - x + 1, (-2, -1))
```
#### The `find_zero` function to solve `f(x) = c`
Solving `f(x) = c` is related to solving `h(x) = 0`. The key is to make a new function using the difference of the two sides: `h(x) = f(x) - c`.
##### Example
Solve for a value of $x$ where `erfc(x)` is equal to `0.5`.
@@ -415,6 +366,40 @@ find_zero(h, (-Inf, Inf)) # as wide as possible in this case
```
##### Example: Inverse functions
If $f(x)$ is *monotonic* and *continuous* over an interval $[a,b]$ then it has an *inverse function*. That is for any $y$ between $f(a)$ and $f(b)$ we can find an $x$ satisfying $y = f(x)$ with $a \leq x \leq b$. This is due, of course, to both the intermediate value theorem (which guarantees an $x$) and monotonicity (which guarantees just one $x$).
To see how we can *numerically* find an inverse function using `find_zero`, we have this function:
```{julia}
function inverse_function(f, a, b, args...; kwargs...)
fa, fb = f(a), f(b)
m, M = fa < fb ? (fa, fb) : (fb, fa)
y -> begin
@assert m ≤ y ≤ M
find_zero(x ->f(x) - y, (a,b), args...; kwargs...)
end
end
```
The check on `fa < fb` is due to the possibility that $f$ is increasing (in which case `fa < fb`) or decreasing (in which case `fa > fb`).
To see this used, we consider the monotonic function $f(x) = x - \sin(x)$ over $[0, 5\pi]$. To graph, we have:
```{julia}
f(x) = x - sin(x)
a, b = 0, 5pi
plot(inverse_function(f, a, b), f(a), f(b); aspect_ratio=:equal)
```
(We plot over the range $[f(a), f(b)]$ here, as we can guess $f(x)$ is *increasing*.)
#### The `find_zero` function to solve `f(x) = g(x)`
Solving `f(x) = g(x)` is related to solving `h(x) = 0`. The key is to make a new function using the difference of the two sides: `h(x) = f(x) - g(x)`.
##### Example
@@ -455,36 +440,6 @@ find_zero(cos(x) ~ x, (0, 2))
[![Intersection of two curves as illustrated by Canadian artist Kapwani Kiwanga.](figures/intersection-biennale.jpg)](https://www.gallery.ca/whats-on/touring-exhibitions-and-loans/around-the-world/canada-pavilion-at-the-venice-biennale/kapwani-kiwanga-trinket){width=40%}
##### Example: Inverse functions
If $f(x)$ is *monotonic* and *continuous* over an interval $[a,b]$ then it has an *inverse function*. That is for any $y$ between $f(a)$ and $f(b)$ we can find an $x$ satisfying $y = f(x)$ with $a \leq x \leq b$. This is due, of course, to both the intermediate value theorem (which guarantees an $x$) and monotonicity (which guarantees just one $x$).
To see how we can *numerically* find an inverse function using `find_zero`, we have this function:
```{julia}
function inverse_function(f, a, b, args...; kwargs...)
fa, fb = f(a), f(b)
m, M = fa < fb ? (fa, fb) : (fb, fa)
y -> begin
@assert m ≤ y ≤ M
find_zero(x ->f(x) - y, (a,b), args...; kwargs...)
end
end
```
The check on `fa < fb` is due to the possibility that $f$ is increasing (in which case `fa < fb`) or decreasing (in which case `fa > fb`).
To see this used, we consider the monotonic function $f(x) = x - \sin(x)$ over $[0, 5\pi]$. To graph, we have:
```{julia}
f(x) = x - sin(x)
a, b = 0, 5pi
plot(inverse_function(f, a, b), f(a), f(b); aspect_ratio=:equal)
```
(We plot over the range $[f(a), f(b)]$ here, as we can guess $f(x)$ is *increasing*.)
##### Example
@@ -679,7 +634,7 @@ Geometry will tell us that $\cos(x) - x/p$ for *one* $x$ in $[0, \pi/2]$ wheneve
#| hold: true
f(x, p=1) = cos(x) - x/p
I = (0, pi/2)
find_zero(f, I), find_zero(f, I, p=2)
find_zero(f, I), find_zero(f, I; p=2)
```
The second number is the solution when `p=2`.
@@ -752,7 +707,7 @@ f.(zs)
The `find_zero` function in the `Roots` package is an interface to one of several methods. For now we focus on the *bracketing* methods, later we will see others. Bracketing methods, among others, include `Roots.Bisection()`, the basic bisection method though with a different sense of "middle" than $(a+b)/2$ and used by default above; `Roots.A42()`, which will typically converge much faster than simple bisection; `Roots.Brent()` for the classic method of Brent, and `FalsePosition()` for a family of *regula falsi* methods. These can all be used by specifying the method in a call to `find_zero`.
Alternatively, `Roots` implements the `CommonSolve` interface popularized by its use in the `DifferentialEquations.jl` ecosystem, a wildly successful area for `Julia`. The basic setup involves two steps: setup a "problem;" solve the problem.
Alternatively, `Roots` implements the `CommonSolve` interface popularized by its use in the `DifferentialEquations.jl` ecosystem, a wildly successful area for `Julia`. The basic setup involves two steps: setup a "problem"; solve the problem.
To set up a problem we call `ZeroProblem` with the function and an initial interval, as in:
@@ -822,6 +777,74 @@ nothing
[![Elevation profile of the Hardrock 100 ultramarathon. Treating the elevation profile as a function, the absolute maximum is just about 14,000 feet and the absolute minimum about 7600 feet. These are of interest to the runner for different reasons. Also of interest would be each local maxima and local minima - the peaks and valleys of the graph - and the total elevation climbed - the latter so important/unforgettable its value makes it into the chart's title.
](figures/hardrock-100.jpeg)](https://hardrock100.com){width=50%}
This figure shows the two concepts as well.
::: {#fig-absolute-relative}
```{julia}
#| echo: false
plt = let
gr()
empty_style = (xaxis=([], false),
yaxis=([], false),
framestyle=:origin,
legend=false)
axis_style = (arrow=true, side=:head, line=(:gray, 1))
p(x) = (x-1)*(x-2)*(x-3)*(x-4) + x/2 + 2
a, b = 0.25, 4.5
z₁, z₂, z₃ = zs = find_zeros(x -> ForwardDiff.derivative(p,x), (a, b))
a = -0.0
plot(; empty_style...)
plot!(p, a, b; line=(:black, 2))
plot!([a,b+0.25], [0,0]; axis_style...)
plot!([a,a] .+ .1, [-1, p(0)]; axis_style...)
δ = .5
ts = range(0, 2pi, 100)
for z in zs
plot!([z-δ,z+δ],[p(z),p(z)]; line=(:black, 1))
C = Shape(z .+ 0.03 * sin.(ts), p(z) .+ 0.3 * cos.(ts))
plot!(C; fill=(:periwinkle, 1), line=(:black, 1))
end
for z in (a,b)
C = Shape(z .+ 0.03 * sin.(ts), p(z) .+ 0.3 * cos.(ts))
plot!(C; fill=(:black, 1), line=(:black, 1))
end
κ = 0.33
annotate!([
(a, 0, text(L"a", :top)),
(b,0, text(L"b", :top)),
(a + κ/5, p(a), text(raw"absolute max", 10, :left)),
(z₁, p(z₁)-κ, text(raw"absolute min", 10, :top)),
(z₂, p(z₂) + κ, text(raw"relative max", 10, :bottom)),
(z₃, p(z₃) - κ, text(raw"relative min", 10, :top)),
(b, p(b) + κ, text(raw"endpoint", 10, :bottom))
])
current()
end
plt
```
```{julia}
#| echo: false
plotly()
nothing
```
Figure illustrating absolute and relative minima for a function $f(x)$ over $I=[a,b]$. The leftmost point has a $y$ value, $f(a)$, which is an absolute maximum of $f(x)$ over $I$. The three points highlighted between $a$ and $b$ are all relative extrema. The first one is *also* the absolute minimum over $I$. The endpoint is not considered a relative maximum for technical reasons --- there is no interval around $b$, it being on the boundary of $I$.
:::
The extreme value theorem discusses an assumption that ensures absolute maximum and absolute minimum values exist.
::: {.callout-note icon=false}
@@ -849,7 +872,7 @@ The function $f(x) = \sqrt{1-x^2}$ is continuous on the interval $[-1,1]$ (in th
##### Example
The function $f(x) = x \cdot e^{-x}$ on the closed interval $[0, 5]$ is continuous. Hence it has an absolute maximum, which a graph shows to be $0.4$. It has an absolute minimum, clearly the value $0$ occurring at the endpoint.
The function $f(x) = x \cdot e^{-x}$ on the closed interval $[0, 5]$ is continuous. Hence it has an absolute maximum, which a graph shows to be about $0.4$ and occurring near $x=1$. It has an absolute minimum, clearly the value $0$ occurring at the endpoint.
```{julia}
@@ -886,7 +909,7 @@ A New York Times [article](https://www.nytimes.com/2016/07/30/world/europe/norwa
## Continuity and closed and open sets
We comment on two implications of continuity that can be generalized to more general settings.
We comment on two implications of continuity that can be generalized.
The two intervals $(a,b)$ and $[a,b]$ differ as the latter includes the endpoints. The extreme value theorem shows this distinction can make a big difference in what can be said regarding *images* of such interval.
@@ -1212,7 +1235,7 @@ radioq(choices, answ, keep_order=true)
###### Question
The extreme value theorem has two assumptions: a continuous function and a *closed* interval. Which of the following examples fails to satisfy the consequence of the extreme value theorem because the function is not continuous?
The extreme value theorem has two assumptions: a continuous function and a *closed* interval. Which of the following examples fails to satisfy the consequence of the extreme value theorem because the function is defined on $I$ but is not continuous on $I$?
```{julia}
@@ -1227,6 +1250,170 @@ answ = 4
radioq(choices, answ, keep_order=true)
```
###### Question
The extreme value theorem is true when $f$ is a continuous function on an interval $I$ *and* $I=[a,b]$ is a *closed* interval. Which of these illustrates why it doesn't apply as $f$ is not continuous on $I$ but is defined on $I$?
```{julia}
#| hold: true
#| echo: false
let
gr()
empty_style = (xaxis=([], false),
yaxis=([], false),
framestyle=:origin,
legend=false)
axis_style = (arrow=true, side=:head, line=(:gray, 1))
ts = range(0, 2pi, 100)
# defined on I; not continuous on I
p1 = plot(;empty_style..., aspect_ratio=:equal)
title!(p1, "(a)")
plot!(p1, x -> 1 - abs(2x), -1, 1, color=:black)
plot!(p1, zero; line=(:black, 1), arrow=true, side=:head)
C = Shape(0.03 .* sin.(ts), 1 .+ 0.03 .* cos.(ts))
plot!(p1, C, fill=(:white, 1), line=(:black,1))
C = Shape(0.03 .* sin.(ts), - 0.25 .+ 0.03 .* cos.(ts))
plot!(p1, C, fill=(:black,1))
annotate!(p1, [
(-1,0,text(L"a", :top)),
(1,0,text(L"b", :top))
])
# not defined on I
p2 = plot(;empty_style...)
title!(p2, "(b)")
plot!(p2, x -> 1/(1-x), 0, .95, color=:black)
plot!(p2, x-> -1/(1-x), 1.05, 2, color=:black)
plot!(p2, zero; axis_style...)
annotate!(p2,[
(0,0,text(L"a", :top)),
(2, 0, text(L"b", :top))
])
# not continuous on I
p3 = plot(;empty_style...)
title!(p3, "(c)")
plot!(p3, x -> 1/(1-x), 0, .95, color=:black)
ylims!((-0.25, 1/(1 - 0.96)))
plot!(p3, [0,1.05],[0,0]; axis_style...)
vline!(p3, [1]; line=(:black, 1, :dash))
annotate!(p3,[
(0,0,text(L"a", :top)),
(1, 0, text(L"b", :top))
])
# continuous
p4 = plot(;empty_style...)
title!(p4, "(d)")
f(x) = x^x
a, b = 0, 2
plot!(p4, f, a, b; line=(:black,1))
ylims!(p4, (-.25, f(b)))
plot!(p4, [a-.1, b+.1], [0,0]; axis_style...)
scatter!([0,2],[ f(0),f(2)]; marker=(:circle,:black))
annotate!([
(a, 0, text(L"a", :top)),
(b, 0, text(L"b", :top))
])
l = @layout[a b; c d]
p = plot(p1, p2, p3, p4, layout=l)
imgfile = tempname() * ".png"
savefig(p, imgfile)
hotspotq(imgfile, (0,1/2), (1/2,1))
end
```
The extreme value theorem is true when $f$ is a continuous function on an interval $I$ and $I=[a,b]$ is a *closed* interval. Which of these illustrates when the theorem's assumptions are true?
```{julia}
#| hold: true
#| echo: false
## come on; save this figure...
let
gr()
empty_style = (xaxis=([], false),
yaxis=([], false),
framestyle=:origin,
legend=false)
axis_style = (arrow=true, side=:head, line=(:gray, 1))
ts = range(0, 2pi, 100)
# defined on I; not continuous on I
p1 = plot(;empty_style..., aspect_ratio=:equal)
title!(p1, "(a)")
plot!(p1, x -> 1 - abs(2x), -1, 1, color=:black)
plot!(p1, zero; line=(:black, 1), arrow=true, side=:head)
C = Shape(0.03 .* sin.(ts), 1 .+ 0.03 .* cos.(ts))
plot!(p1, C, fill=(:white, 1), line=(:black,1))
C = Shape(0.03 .* sin.(ts), - 0.25 .+ 0.03 .* cos.(ts))
plot!(p1, C, fill=(:black,1))
annotate!(p1, [
(-1,0,text(L"a", :top)),
(1,0,text(L"b", :top))
])
# not defined on I
p2 = plot(;empty_style...)
title!(p2, "(b)")
plot!(p2, x -> 1/(1-x), 0, .95, color=:black)
plot!(p2, x-> -1/(1-x), 1.05, 2, color=:black)
plot!(p2, zero; axis_style...)
annotate!(p2,[
(0,0,text(L"a", :top)),
(2, 0, text(L"b", :top))
])
# not continuous on I
p3 = plot(;empty_style...)
title!(p3, "(c)")
plot!(p3, x -> 1/(1-x), 0, .95, color=:black)
ylims!((-0.1, 1/(1 - 0.96)))
plot!(p3, [0,1.05],[0,0]; axis_style...)
vline!(p3, [1]; line=(:black, 1, :dash))
annotate!(p3,[
(0,0,text(L"a", :top)),
(1, 0, text(L"b", :top))
])
# continuous
p4 = plot(;empty_style...)
title!(p4, "(d)")
f(x) = x^x
a, b = 0, 2
ylims!(p4, (-.25, f(b)))
plot!(p4, f, a, b; line=(:black,1))
plot!(p4, [a-.1, b+.1], [0,0]; axis_style...)
scatter!([0,2],[ f(0),f(2)]; marker=(:circle,:black))
annotate!([
(a, 0, text(L"a", :top)),
(b, 0, text(L"b", :top))
])
l = @layout[a b; c d]
p = plot(p1, p2, p3, p4, layout=l)
imgfile = tempname() * ".png"
savefig(p, imgfile)
hotspotq(imgfile, (1/2,1), (0,1/2))
end
```
```{julia}
#| echo: false
plotly();
```
###### Question
@@ -1318,28 +1505,3 @@ The zeros of the equation $\cos(x) \cdot \cosh(x) = 1$ are related to vibrations
val = maximum(find_zeros(x -> cos(x) * cosh(x) - 1, (0, 6pi)))
numericq(val)
```
###### Question
A parametric equation is specified by a parameterization $(f(t), g(t)), a \leq t \leq b$. The parameterization will be continuous if and only if each function is continuous.
Suppose $k_x$ and $k_y$ are positive integers and $a, b$ are positive numbers, will the [Lissajous](https://en.wikipedia.org/wiki/Parametric_equation#Lissajous_Curve) curve given by $(a\cos(k_x t), b\sin(k_y t))$ be continuous?
```{julia}
#| hold: true
#| echo: false
yesnoq(true)
```
Here is a sample graph for $a=1, b=2, k_x=3, k_y=4$:
```{julia}
#| hold: true
a,b = 1, 2
k_x, k_y = 3, 4
plot(t -> a * cos(k_x *t), t-> b * sin(k_y * t), 0, 4pi)
```

View File

@@ -36,27 +36,38 @@ colors = [:black, :blue, :orange, :red, :green, :orange, :purple]
## Area of parabola
function make_triangle_graph(n)
title = "Area of parabolic cup ..."
n==1 && (title = "\${Area = }1/2\$")
n==2 && (title = "\${Area = previous }+ 1/8\$")
n==3 && (title = "\${Area = previous }+ 2\\cdot(1/8)^2\$")
n==4 && (title = "\${Area = previous }+ 4\\cdot(1/8)^3\$")
n==5 && (title = "\${Area = previous }+ 8\\cdot(1/8)^4\$")
n==6 && (title = "\${Area = previous }+ 16\\cdot(1/8)^5\$")
n==7 && (title = "\${Area = previous }+ 32\\cdot(1/8)^6\$")
n==1 && (title = L"Area $= 1/2$")
n==2 && (title = L"Area $=$ previous $+\; \frac{1}{8}$")
n==3 && (title = L"Area $=$ previous $+\; 2\cdot\frac{1}{8^2}$")
n==4 && (title = L"Area $=$ previous $+\; 4\cdot\frac{1}{8^3}$")
n==5 && (title = L"Area $=$ previous $+\; 8\cdot\frac{1}{8^4}$")
n==6 && (title = L"Area $=$ previous $+\; 16\cdot\frac{1}{8^5}$")
n==7 && (title = L"Area $=$ previous $+\; 32\cdot\frac{1}{8^6}$")
plt = plot(f, 0, 1, legend=false, size = fig_size, linewidth=2)
annotate!(plt, [(0.05, 0.9, text(title,:left))]) # if in title, it grows funny with gr
n >= 1 && plot!(plt, [1,0,0,1, 0], [1,1,0,1,1], color=colors[1], linetype=:polygon, fill=colors[1], alpha=.2)
n == 1 && plot!(plt, [1,0,0,1, 0], [1,1,0,1,1], color=colors[1], linewidth=2)
plt = plot(f, 0, 1;
legend=false,
size = fig_size,
linewidth=2)
annotate!(plt, [
(0.05, 0.9, text(title,:left))
]) # if in title, it grows funny with gr
n >= 1 && plot!(plt, [1,0,0,1, 0], [1,1,0,1,1];
color=colors[1], linetype=:polygon,
fill=colors[1], alpha=.2)
n == 1 && plot!(plt, [1,0,0,1, 0], [1,1,0,1,1];
color=colors[1], linewidth=2)
for k in 2:n
xs = range(0,stop=1, length=1+2^(k-1))
ys = map(f, xs)
k < n && plot!(plt, xs, ys, linetype=:polygon, fill=:black, alpha=.2)
xs = range(0, stop=1, length=1+2^(k-1))
ys = f.(xs)
k < n && plot!(plt, xs, ys;
linetype=:polygon, fill=:black, alpha=.2)
if k == n
plot!(plt, xs, ys, color=colors[k], linetype=:polygon, fill=:black, alpha=.2)
plot!(plt, xs, ys, color=:black, linewidth=2)
plot!(plt, xs, ys;
color=colors[k], linetype=:polygon, fill=:black, alpha=.2)
plot!(plt, xs, ys;
color=:black, linewidth=2)
end
end
plt
@@ -1041,18 +1052,34 @@ $$
Why? We can express the function $e^{\csc(x)}/e^{\cot(x)}$ as the above function plus the polynomial $1 + x/2 + x^2/8$. The above is then the sum of two functions whose limits exist and are finite, hence, we can conclude that $M = 0 + 1$.
### The [squeeze](http://en.wikipedia.org/wiki/Squeeze_theorem) theorem
### The squeeze theorem
Sometimes limits can be found by bounding more complicated functions by easier functions.
::: {.callout-note icon=false}
## The [squeeze theorem](http://en.wikipedia.org/wiki/Squeeze_theorem)
Fix $c$ in $I=(a,b)$. Suppose for all $x$ in $I$, except possibly $c$, there are two functions $l$ and $u$, satisfying:
We note one more limit law. Suppose we wish to compute $\lim_{x \rightarrow c}f(x)$ and we have two other functions, $l$ and $u$, satisfying:
* $l(x) \leq f(x) \leq u(x)$.
* These limits exist and are equal:
$$
L = \lim_{x \rightarrow c} l(x) = \lim_{x \rightarrow c} u(x).
$$
* for all $x$ near $c$ (possibly not including $c$) $l(x) \leq f(x) \leq u(x)$.
* These limits exist and are equal: $L = \lim_{x \rightarrow c} l(x) = \lim_{x \rightarrow c} u(x)$.
Then
$$
\lim_{x\rightarrow c} f(x) = L.
$$
Then the limit of $f$ must also be $L$.
:::
The figure shows a usage of the squeeze theorem to show $\sin(x)/x \rightarrow 1$ as $\cos(x) \leq \sin(x)x \leq 1$ for $x$ close to $0$.
```{julia}
#| hold: true
@@ -1088,8 +1115,71 @@ ImageFile(imgfile, caption)
The formal definition of a limit involves clarifying what it means for $f(x)$ to be "close to $L$" when $x$ is "close to $c$". These are quantified by the inequalities $0 < \lvert x-c\rvert < \delta$ and the $\lvert f(x) - L\rvert < \epsilon$. The second does not have the restriction that it is greater than $0$, as indeed $f(x)$ can equal $L$. The order is important: it says for any idea of close for $f(x)$ to $L$, an idea of close must be found for $x$ to $c$.
The key is identifying a value for $\delta$ for a given value of $\epsilon$.
::: {#fig-limit-e-d}
```{julia}
#| echo: false
plt = let
gr()
f(x) = (x+1)^2 -1
a, b = -1/4, 2
c = 1
L = f(c)
δ = 0.2
ϵ = 3sqrt(δ)
plot(; empty_style...)#, aspect_ratio=:equal)
plot!(f, a, b; line=(:black, 2))
plot!([a,b],[0,0]; axis_style...)
plot!([0,0], [f(a), f(2)]; axis_style...)
plot!([c, c, 0], [0, f(c), f(c)]; line=(:black, 1, :dash))
plot!([c-δ, c-δ, 0], [0, f(c-δ), f(c-δ)]; line=(:black, 1, :dashdot))
plot!([c+δ, c+δ, 0], [0, f(c+δ), f(c+δ)]; line=(:black, 1, :dashdot))
S = Shape([0,b,b,0],[L-ϵ,L-ϵ,L+ϵ,L+ϵ])
plot!(S; fill=(:lightblue, 0.25), line=(nothing,))
domain_color=:red
range_color=:blue
S = Shape([c-δ, c+δ, c+δ, c-δ], 0.05*[-1,-1,1,1])
plot!(S, fill=(domain_color,0.4), line=nothing)
m,M = f(c-δ), f(c+δ)
T = Shape(0.015 * [-1,1,1,-1], [m,m,M,M])
plot!(T, fill=(range_color, 0.4), line=nothing)
C = Plots.scale(Shape(:circle), 0.02, 0.1)
plot!(Plots.translate(C, c, L), fill=(:white,1,0), line=(:black, 1))
plot!(Plots.translate(C, c, 0), fill=(:white,1,0), line=(domain_color, 1))
plot!(Plots.translate(C, 0, L), fill=(:white,1,0), line=(range_color, 1))
annotate!([
(c, 0, text(L"c", :top)),
(c-δ, 0, text(L"c - \delta", 10, :top)),
(c+δ, 0, text(L"c + \delta", 10, :top)),
(0, L, text(L"L", :right)),
(0, L+ϵ, text(L"L + \epsilon", 10, :right)),
(0, L-ϵ, text(L"L - \epsilon", 10, :right)),
])
end
plt
```
```{julia}
#| echo: false
plotly()
nothing
```
Figure illustrating requirements of $\epsilon-\delta$ definition of the limit. The image (shaded red on $y$ axis) of the $x$ within $\delta$ of $c$ (except for $c$ and shaded blue on the $x$ axis) must stay within the bounds of $L-\epsilon$ and $L+ \epsilon$, where $\delta$ may be chosen based on $\epsilon$ but needs to be chosen for every positive $\epsilon$, not just a fixed one as in this figure.
:::
A simple case is the linear case. Consider the function $f(x) = 3x + 2$. Verify that the limit at $c=1$ is $5$.
@@ -1117,64 +1207,78 @@ These lines produce a random $\epsilon$, the resulting $\delta$, and then verify
(The random numbers are technically in $[0,1)$, so in theory `epsilon` could be `0`. So the above approach would be more solid if some guard, such as `epsilon = max(eps(), rand())`, was used. As the formal definition is the domain of paper-and-pencil, we don't fuss.)
In this case, $\delta$ is easy to guess, as the function is linear and has slope $3$. This basically says the $y$ scale is 3 times the $x$ scale. For non-linear functions, finding $\delta$ for a given $\epsilon$ can be a challenge. For the function $f(x) = x^3$, illustrated below, a value of $\delta=\epsilon^{1/3}$ is used for $c=0$:
In this case, $\delta$ is easy to guess, as the function is linear and has slope $3$. This basically says the $y$ scale is 3 times the $x$ scale. For non-linear functions, finding $\delta$ for a given $\epsilon$ can be more of a challenge.
```{julia}
#| hold: true
#| echo: false
#| cache: true
## {{{ limit_e_d }}}
gr()
function make_limit_e_d(n)
f(x) = x^3
##### Example
xs = range(-.9, stop=.9, length=50)
ys = map(f, xs)
We show using the definition that for any fixed $a$ and $n$:
$$
\lim_{x \rightarrow a} x^n = a^n.
$$
This proof uses a bound based on properties of the absolute value.
plt = plot(f, -.9, .9, legend=false, size=fig_size)
if n == 0
nothing
else
k = div(n+1,2)
epsilon = 1/2^k
delta = cbrt(epsilon)
if isodd(n)
plot!(plt, xs, 0*xs .+ epsilon, color=:orange)
plot!(plt, xs, 0*xs .- epsilon, color=:orange)
else
plot!(delta * [-1, 1], epsilon * [ 1, 1], color=:orange)
plot!(delta * [ 1, -1], epsilon * [-1,-1], color=:orange)
plot!(delta * [-1, -1], epsilon * [-1, 1], color=:red)
plot!(delta * [ 1, 1], epsilon * [-1, 1], color=:red)
end
end
plt
end
We look at $f(x) - L = x^n - a^n = (x-a)(x^{n-1} + x^{n-2}a + \cdots + x^1a^{n-1} + a^n)$.
Taking absolute values gives an inequality by the triangle inequality:
n = 11
anim = @animate for i=1:n
make_limit_e_d(i-1)
end
$$
\lvert x^n - a^n\rvert \leq \lvert x-a\rvert\cdot
\left(
\lvert x^{n-1}\rvert +
\lvert x^{n-2}\rvert\lvert a\rvert +
\cdots +
\lvert x^1\rvert\lvert a^{n-1}\rvert +
\lvert a^n\rvert
\right).
$$
imgfile = tempname() * ".gif"
gif(anim, imgfile, fps = 1)
Now, for a given $\epsilon>0$ we seek a $\delta>0$ satisfying the properties of the limit definition for $f(x) = x^n$ and $L=a^n$. For now, assume $\delta < 1$. Then we can assume $\lvert x-a\rvert < \delta$ and
$$
\lvert x\rvert = \lvert x - a + a\rvert \leq \lvert x-a\rvert + \lvert a\rvert < 1 + \lvert a\rvert
$$
caption = L"""
This says then
Demonstration of $\epsilon$-$\delta$ proof of $\lim_{x \rightarrow 0}
x^3 = 0$. For any $\epsilon>0$ (the orange lines) there exists a
$\delta>0$ (the red lines of the box) for which the function $f(x)$
does not leave the top or bottom of the box (except possibly at the
edges). In this example $\delta^3=\epsilon$.
$$
\begin{align*}
\lvert x^n - a^n\rvert
&\leq
\lvert x-a\rvert\cdot \left(
\lvert x\rvert^{n-1} +
\lvert x\rvert^{n-2}\lvert a\rvert +
\cdots +
\lvert x\rvert^1\lvert a^{n-1}\rvert +
\lvert a^n\rvert
\right)\\
%%
&\leq \lvert x - a\rvert
\cdot \left(
(\lvert a\rvert+1)^{n-1} +
(\lvert a\rvert+1)^{n-2}\lvert a\rvert
+ \cdots +
(\lvert a\rvert+1)^1 \lvert a^{n-1}\rvert +
\lvert a^n \rvert
\right)\\
&\leq \lvert x-a\rvert \cdot C,
\end{align*}
$$
where $C$ is just some constant not depending on $x$, just $a$ and $n$.
Now if $\delta < 1$ and $\delta < \epsilon/C$ and if
$0 < \lvert x - a \rvert < \delta$ then
$$
\lvert f(x) - L \rvert =
\lvert x^n - a^n\rvert \leq \lvert x-a\rvert \cdot C \leq \delta\cdot C < \frac{\epsilon}{C} \cdot C = \epsilon.
$$
With this result, the rules of limits can immediately extend this to any polynomial, $p(x),$ it follows that $\lim_{x \rightarrow c} p(x) = p(a)$. (Because $c_n x^n \rightarrow c_n a^n$ and the sum of two functions with a limit has the limit of the sums.) Based on this, we will say later that any polynomial is *continuous* for all $x$.
"""
plotly()
ImageFile(imgfile, caption)
```
## Questions
@@ -1290,26 +1394,26 @@ let
title!(p1, "(a)")
plot!(p1, x -> x^2, 0, 2, color=:black)
plot!(p1, zero, linestyle=:dash)
annotate!(p1,[(1,0,"a")])
annotate!(p1,[(1,0,text(L"a",:top))])
p2 = plot(;axis=nothing, legend=false)
title!(p2, "(b)")
plot!(p2, x -> 1/(1-x), 0, .95, color=:black)
plot!(p2, x-> -1/(1-x), 1.05, 2, color=:black)
plot!(p2, zero, linestyle=:dash)
annotate!(p2,[(1,0,"a")])
annotate!(p2,[(1,0,text(L"a",:top))])
p3 = plot(;axis=nothing, legend=false)
title!(p3, "(c)")
plot!(p3, sinpi, 0, 2, color=:black)
plot!(p3, zero, linestyle=:dash)
annotate!(p3,[(1,0,"a")])
annotate!(p3,[(1,0,text("a",:top))])
p4 = plot(;axis=nothing, legend=false)
title!(p4, "(d)")
plot!(p4, x -> x^x, 0, 2, color=:black)
plot!(p4, zero, linestyle=:dash)
annotate!(p4,[(1,0,"a")])
annotate!(p4,[(1,0,text(L"a",:top))])
l = @layout[a b; c d]
p = plot(p1, p2, p3, p4, layout=l)
@@ -1520,8 +1624,8 @@ Take
$$
f(x) = \begin{cases}
0 & x \neq 0\\
1 & x = 0
0 &~ x \neq 0\\
1 &~ x = 0
\end{cases}
$$

View File

@@ -32,22 +32,24 @@ Let's begin with a function that is just problematic. Consider
$$
f(x) = \sin(1/x)
f(x) = \sin(\frac{1}{x})
$$
As this is a composition of nice functions it will have a limit everywhere except possibly when $x=0$, as then $1/x$ may not have a limit. So rather than talk about where it is nice, let's consider the question of whether a limit exists at $c=0$.
@fig-sin-1-over-x shows the issue:
A graph shows the issue:
:::{#fig-sin-1-over-x}
```{julia}
#| hold: true
#| echo: false
f(x) = sin(1/x)
plot(f, range(-1, stop=1, length=1000))
```
Graph of the function $f(x) = \sin(1/x)$ near $0$. It oscillates infinitely many times around $0$.
:::
The graph oscillates between $-1$ and $1$ infinitely many times on this interval - so many times, that no matter how close one zooms in, the graph on the screen will fail to capture them all. Graphically, there is no single value of $L$ that the function gets close to, as it varies between all the values in $[-1,1]$ as $x$ gets close to $0$. A simple proof that there is no limit, is to take any $\epsilon$ less than $1$, then with any $\delta > 0$, there are infinitely many $x$ values where $f(x)=1$ and infinitely many where $f(x) = -1$. That is, there is no $L$ with $|f(x) - L| < \epsilon$ when $\epsilon$ is less than $1$ for all $x$ near $0$.
@@ -65,11 +67,10 @@ The following figure illustrates:
```{julia}
#| hold: true
f(x) = x * sin(1/x)
plot(f, -1, 1)
plot!(abs)
plot!(x -> -abs(x))
plot(f, -1, 1; label="f")
plot!(abs; label="|.|")
plot!(x -> -abs(x); label="-|.|")
```
The [squeeze](http://en.wikipedia.org/wiki/Squeeze_theorem) theorem of calculus is the formal reason $f$ has a limit at $0$, as both the upper function, $|x|$, and the lower function, $-|x|$, have a limit of $0$ at $0$.
@@ -181,21 +182,38 @@ Consider this funny graph:
```{julia}
#| hold: true
#| echo: false
xs = range(0,stop=1, length=50)
plot(x->x^2, -2, -1, legend=false)
let
xs = range(0,stop=1, length=50)
plot(; legend=false, aspect_ratio=true,
xticks = -4:4)
plot!([(-4, -1.5),(-2,4)]; line=(:black,1))
plot!(x->x^2, -2, -1; line=(:black,1))
plot!(exp, -1,0)
plot!(x -> 1-2x, 0, 1)
plot!(sqrt, 1, 2)
plot!(x -> 1-x, 2,3)
S = Plots.scale(Shape(:circle), 0.05)
plot!(Plots.translate(S, -4, -1.5); fill=(:black,))
plot!(Plots.translate(S, -1, (-1)^2); fill=(:white,))
plot!(Plots.translate(S, -1, exp(-1)); fill=(:black,))
plot!(Plots.translate(S, 1, 1 - 2(1)); fill=(:black,))
plot!(Plots.translate(S, 1, sqrt(1)); fill=(:white,))
plot!(Plots.translate(S, 2, sqrt(2)); fill=(:white,))
plot!(Plots.translate(S, 2, 1 - (2)); fill=(:black,))
plot!(Plots.translate(S, 3, 1 - (3)); fill=(:black,))
end
```
Describe the limits at $-1$, $0$, and $1$.
* At $-1$ we see a jump, there is no limit but instead a left limit of 1 and a right limit appearing to be $1/2$.
* At $0$ we see a limit of $1$.
* Finally, at $1$ again there is a jump, so no limit. Instead the left limit is about $-1$ and the right limit $1$.
* At $-1$ we see a jump, there is no limit but instead a left limit of 1 and a right limit appearing to be $1/2$.
* At $0$ we see a limit of $1$.
* Finally, at $1$ again there is a jump, so no limit. Instead the left limit is about $-1$ and the right limit $1$.
## Limits at infinity

View File

@@ -1,3 +1,7 @@
---
engine: julia
---
# Precalculus Concepts
The mathematical topics in this chapter come from pre-calculus. However, much of the `Julia` usage needed for the rest of the notes are introduced.

View File

@@ -4,6 +4,7 @@ DataFrames = "a93c6f00-e57d-5684-b7b6-d8193f3e46c0"
IJulia = "7073ff75-c697-5162-941a-fcdaad2a7d2a"
IntervalArithmetic = "d1acc4aa-44c8-5952-acd4-ba5d80a2a253"
LaTeXStrings = "b964fa9f-0449-5b57-a5c2-d3ea65f4040f"
Latexify = "23fbe1c1-3f47-55db-b15f-69d7ec21a316"
Markdown = "d6f4376e-aef5-505a-96c1-9c027394607a"
Measures = "442fdcdd-2543-5da2-b0f3-8c86c306513e"
Mustache = "ffc61752-8dc7-55ee-8c37-f3e9cdd09e70"

View File

@@ -42,7 +42,7 @@ For these examples, the domain of both $f(x)$ and $g(x)$ is all real values of $
In general the range is harder to identify than the domain, and this is the case for these functions too. For $f(x)$ we may know the $\cos$ function is trapped in $[-1,1]$ and it is intuitively clear than all values in that set are possible. The function $h(x)$ would have range $[0,\infty)$. The $s(x)$ function is either $-1$ or $1$, so only has two possible values in its range. What about $g(x)$? It is a parabola that opens upward, so any $y$ values below the $y$ value of its vertex will not appear in the range. In this case, the symmetry indicates that the vertex will be at $(1/2, -1/4)$, so the range is $[-1/4, \infty)$.
::: {#fig-domain-range layout-nrow=2}
::: {#fig-domain-range-1}
```{julia}
#| echo: false
plt = let
@@ -70,7 +70,7 @@ plt = let
Δy = (y1-y0)/60
Δx = (b - a)/75
plot(; aspect_ratio=:equal, empty_style...)
plot(; empty_style...) # aspect_ratio=:equal,
plot!([-.25,3.25],[0,0]; axis_style...)
plot!([0,0], [min(-2Δy, y0 - Δy), y1 + 4Δy]; axis_style... )
@@ -83,9 +83,13 @@ plt = let
plot!([a, b], [f(a), f(a)]; mark_style...)
plot!([a, b], [f(b), f(b)]; mark_style...)
end
plot
plt
```
Figure of the plot of a function over an interval $[a,b]$ highlighting the domain and the range.
:::
::: {#fig-domain-range-2}
```{julia}
#| echo: false
plt = let
@@ -124,7 +128,7 @@ plt = let
plot!(Shape(Δx*[-1,1,1,-1], [-5, -5,-1,-1]); range_style...)
plot!(Shape(Δx*[-1,1,1,-1], [ 5, 5,1,1]); range_style...)
end
plot
plt
```
@@ -134,7 +138,7 @@ plotly()
nothing
```
The top figure shows the domain and range of the function as highlighted intervals. The bottom figure shows that the domain may be a collection of intervals. (In this case the $\sec$ function is not defined at $\pi/2 + k \pi$ for integer $k$) and the range may be a collection of intervals. (In this case, the $\sec$ function never have a value in $(-1,1)$.
This figure shows that the domain of a function may be a collection of intervals. (In this case the $\sec$ function is not defined at $\pi/2 + k \pi$ for integer $k$) and the range may be a collection of intervals. (In this case, the $\sec$ function never has a value in $(-1,1)$.
:::
@@ -176,7 +180,7 @@ For typical cases like the three above, there isn't really much new to learn.
:::{.callout-note}
## Note
## The equals sign is used differently between math and Julia
The equals sign in `Julia` always indicates either an assignment or a mutation of the object on the left side. The definition of a function above is an *assignment*, in that a function is added (or modified) in a table holding the methods associated with the function's name.
The equals sign restricts the expressions available on the *left*-hand side to a) a variable name, for assignment; b) mutating an object at an index, as in `xs[1]`; c) mutating a property of a struct; or d) a function assignment following this form `function_name(args...)`.
@@ -216,7 +220,10 @@ $$
f(x) = 5/9 \cdot (x - 32)
$$
In fact, the graph of a function $f(x)$ is simply defined as the graph of the equation $y=f(x)$. There is a distinction in `Julia` as a command such as
In fact, the graph of a function $f(x)$ is simply defined as the graph of the equation $y=f(x)$.
There **is** a distinction in `Julia`. The last command here
```{julia}
@@ -236,13 +243,65 @@ f(72) ## room temperature
will create a function object with a value of `x` determined at a later time - the time the function is called. So the value of `x` defined when the function is created is not important here (as the value of `x` used by `f` is passed in as an argument).
Within `Julia`, we make note of the distinction between a function object versus a function call. In the definition `f(x)=cos(x)`, the variable `f` refers to a function object, whereas the expression `f(pi)` is a function call. This mirrors the math notation where an $f$ is used when properties of a function are being emphasized (such as $f \circ g$ for composition) and $f(x)$ is used when the values related to the function are being emphasized (such as saying "the plot of the equation $y=f(x)$).
Within `Julia`, we make note of the distinction between a function object versus a function call. In the definition `f(x)=cos(x)`, the variable `f` refers to a function object, whereas the expression `f(pi)` is a function call, resulting in a value. This mirrors the math notation where an $f$ is used when properties of a function are being emphasized (such as $f \circ g$ for composition) and $f(x)$ is used when the values related to the function are being emphasized (such as saying "the plot of the equation $y=f(x)$).
Distinguishing these three related but different concepts -- equations and expressions, function objects, and function calls -- is important when modeling mathematics on the computer.
Distinguishing these related but different concepts --- expressions, equations, values from function calls, and function objects --- is important when modeling mathematics on the computer.
::: {#fig-kidney}
```{julia}
#| echo: false
plt = let
gr()
# two kidneys and an arrow
ts = range(0, 2pi, 200)
a = 1
x(t) = 6a*cos(t) + sin(t) - 4a*cos(t)^5
y(t) = 4a*sin(t)^3
S = Shape(x.(ts), y.(ts))
y1(t) = 6a*cos(t) -3sin(t) - 4a*cos(t)^5
x1(t) = 4a*sin(t)^3
T = Shape(x1.(ts), y1.(ts))
T = Plots.translate(T, 10, 0)
plot(; empty_style...)
plot!(S, fill=(:gray, 0.2), line=(:black, 1))
plot!(T, fill=(:gray, 0.2), line=(:black, 1))
P = (0,0)
Q = (10, 0)
scatter!([P,Q], marker=(:circle,))
ts = reverse(range(pi/4+.1, 3pi/4-.1, 100))
plot!(5 .+ 5*sqrt(2)*cos.(ts), -5 .+ 5*sqrt(2)*sin.(ts);
line=(:black, 1), arrow=true, side=:head)
### Cases
annotate!([
(P..., text(L"x", :top)),
(Q..., text(L"f(x)", :top)),
(5, 3, text(L"f", :top)),
(0, -6, text("Domain")),
(10, -6, text("Range"))
])
end
plt
```
```{julia}
#| echo: false
plotly()
nothing
```
Common illustration of an abstract function mapping a value $x$ in the domain to a value $y=f(x)$ in the range. In `Julia`, the values are named, as with `x`, or computed, as with `f(x)`. The function *object* `f` is like the named arrow, which is the name assigned to a particular mapping.
:::
### Cases, the ternary operator
The definition of $s(x)$ above has two cases:
@@ -256,7 +315,7 @@ s(x) =
\end{cases}
$$
We learn to read this as: when $x$ is less than $0$, then the answer is $-1$. If $x$ is greater than $0$ the answer is $1.$ Often - but not in this example - there is an "otherwise" case to catch those values of $x$ that are not explicitly mentioned. As there is no such "otherwise" case here, we can see that this function has no definition when $x=0$. This function is often called the "sign" function and is also defined by $\lvert x\rvert/x$. (`Julia`'s `sign` function actually defines `sign(0)` to be `0`.)
We learn to read this as: when $x$ is less than $0$, then the answer is $-1$. If $x$ is greater than $0$ the answer is $1.$ Often--but not in this example--there is an "otherwise" case to catch those values of $x$ that are not explicitly mentioned. As there is no such "otherwise" case here, we can see that this function has no definition when $x=0$. This function is often called the "sign" function and is also defined by $\lvert x\rvert/x$. (`Julia`'s `sign` function defines `sign(0)` to be `0`.)
How do we create conditional statements in `Julia`? Programming languages generally have "if-then-else" constructs to handle conditional evaluation. In `Julia`, the following code will handle the above condition:
@@ -298,7 +357,7 @@ For example, here is one way to define an absolute value function:
abs_val(x) = x >= 0 ? x : -x
```
The condition is `x >= 0` - or is `x` non-negative? If so, the value `x` is used, otherwise `-x` is used.
The condition is `x >= 0`--or is `x` non-negative? If so, the value `x` is used, otherwise `-x` is used.
Here is a means to implement a function which takes the larger of `x` or `10`:
@@ -311,16 +370,16 @@ bigger_10(x) = x > 10 ? x : 10.0
(This could also utilize the `max` function: `f(x) = max(x, 10.0)`.)
Or similarly, a function to represent a cell phone plan where the first $500$ minutes are $20$ dollars and every additional minute is $5$ cents:
Or similarly, a function to represent a cell phone plan where the first $5$ Gb of data are $11$ dollars and every additional GB is $3.50$:
```{julia}
cellplan(x) = x < 500 ? 20.0 : 20.0 + 0.05 * (x-500)
cellplan(x) = x <= 5 ? 11.0 : 11.0 + 3.50 * (x-5)
```
:::{.callout-warning}
## Warning
Type stability. These last two definitions used `10.0` and `20.0` instead of the integers `10` and `20` for the answer. Why the extra typing? When `Julia` can predict the type of the output from the type of inputs, it can be more efficient. So when possible, we help out and ensure the output is always the same type.
Type stability. These last two definitions used `10.0` and `11.0` instead of the integers `10` and `11` for the answer. Why the extra typing? When `Julia` can predict the type of the output from the type of inputs, it can be more efficient. So when possible, we help out and ensure the output is always the same type.
:::
@@ -391,7 +450,7 @@ $$
f(x) = \left(\frac{g}{k v_0\cos(\theta)} + \tan(\theta) \right) x + \frac{g}{k^2}\ln\left(1 - \frac{k}{v_0\cos(\theta)} x \right)
$$
Here $g$ is the gravitational constant $9.8$ and $v_0$, $\theta$ and $k$ parameters, which we take to be $200$, $45$ degrees and $1/2$ respectively. With these values, the above function can be computed when $x=100$ with:
Here $g$ is the gravitational constant $9.8$ and $v_0$, $\theta$, and $k$ parameters, which we take to be $200$, $45$ degrees and $1/2$ respectively. With these values, the above function can be computed when $x=100$ with:
```{julia}
@@ -541,7 +600,12 @@ $$
f(x) = m \cdot x + b, \quad g(x) = y_0 + m \cdot (x - x_0).
$$
Both functions use the variable $x$, but there is no confusion, as we learn that this is just a dummy variable to be substituted for and so could have any name. Both also share a variable $m$ for a slope. Where does that value come from? In practice, there is a context that gives an answer. Despite the same name, there is no expectation that the slope will be the same for each function if the context is different. So when parameters are involved, a function involves a rule and a context to give specific values to the parameters. Euler had said initially that functions composed of "the variable quantity and numbers or constant quantities." The term "variable," we still use, but instead of "constant quantities," we use the name "parameters."
Both functions use the variable $x$, but there is no confusion, as we
learn that this is just a dummy variable to be substituted for and so
could have any name. Both also share a variable $m$ for a slope.
Where does the value for $m$ come from?
In practice, there is a context that gives an answer. Despite the same name, there is no expectation that the slope will be the same for each function if the context is different. So when parameters are involved, a function involves a rule and a context to give specific values to the parameters. Euler had said initially that functions composed of "the variable quantity and numbers or constant quantities." The term "variable," we still use, but instead of "constant quantities," we use the name "parameters." In computer language, instead of context, we use the word *scope*.
Something similar is also true with `Julia`. Consider the example of writing a function to model a linear equation with slope $m=2$ and $y$-intercept $3$. A typical means to do this would be to define constants, and then use the familiar formula:
@@ -580,7 +644,11 @@ So the `b` is found from the currently stored value. This fact can be exploited.
How `Julia` resolves what a variable refers to is described in detail in the manual page [Scope of Variables](https://docs.julialang.org/en/v1/manual/variables-and-scoping/). In this case, the function definition finds variables in the context of where the function was defined, the main workspace, and not where it is called. As seen, this context can be modified after the function definition and prior to the function call. It is only when `b` is needed, that the context is consulted, so the most recent binding is retrieved. Contexts allow the user to repurpose variable names without there being name collision. For example, we typically use `x` as a function argument, and different contexts allow this `x` to refer to different values.
Mostly this works as expected, but at times it can be complicated to reason about. In our example, definitions of the parameters can be forgotten, or the same variable name may have been used for some other purpose. The potential issue is with the parameters, the value for `x` is straightforward, as it is passed into the function. However, we can also pass the parameters, such as $m$ and $b$, as arguments. For parameters, one suggestion is to use [keyword](https://docs.julialang.org/en/v1/manual/functions/#Keyword-Arguments) arguments. These allow the specification of parameters, but also give a default value. This can make usage explicit, yet still convenient. For example, here is an alternate way of defining a line with parameters `m` and `b`:
Mostly this works as expected, but at times it can be complicated to reason about. In our example, definitions of the parameters can be forgotten, or the same variable name may have been used for some other purpose. The potential issue is with the parameters, the value for `x` is straightforward, as it is passed into the function.
However, we can also pass the parameters, such as $m$ and $b$, as arguments. There are different styles employed.
For parameters, one suggestion is to use [keyword](https://docs.julialang.org/en/v1/manual/functions/#Keyword-Arguments) arguments. These allow the specification of parameters, but also give a default value. This can make usage explicit, yet still convenient. For example, here is an alternate way of defining a line with parameters `m` and `b`:
```{julia}
@@ -604,7 +672,7 @@ During this call, values for `m` and `b` are found from how the function is call
mxplusb(0; m=3, b=2)
```
Keywords are used to mark the parameters whose values are to be changed from the default. Though one can use *positional arguments* for parameters - and there are good reasons to do so - using keyword arguments is a good practice if performance isn't paramount, as their usage is more explicit yet the defaults mean that a minimum amount of typing needs to be done.
Keywords are used to mark the parameters whose values are to be changed from the default. Though one can use *positional arguments* for parameters--and there are good reasons to do so--using keyword arguments is a good practice if performance isn't paramount, as their usage is more explicit yet the defaults mean that a minimum amount of typing needs to be done.
Keyword arguments are widely used with plotting commands, as there are numerous options to adjust, but typically only a handful adjusted per call. The `Plots` package whose commands we illustrate throughout these notes starting with the next section has this in its docs: `Plots.jl` follows two simple rules with data and attributes:
@@ -640,14 +708,14 @@ For example, here we use a *named tuple* to pass parameters to `f`:
```{julia}
#| hold: true
function trajectory(x ,p)
function trajectory(x, p)
g, v0, theta, k = p.g, p.v0, p.theta, p.k # unpack parameters
a = v0 * cos(theta)
(g/(k*a) + tan(theta))* x + (g/k^2) * log(1 - k/a*x)
end
p = (g=9.8, v0=200, theta = 45*pi/180, k=1/2)
p = (g=9.8, v0=200, theta=45*pi/180, k=1/2)
trajectory(100, p)
```
@@ -687,7 +755,7 @@ The right-hand sides may or may not be familiar, but it should be reasonable to
Earlier we saw the `log` function can use a second argument to express the base. This function is basically defined by `log(b,x)=log(x)/log(b)`. The `log(x)` value is the natural log, and this definition just uses the change-of-base formula for logarithms.
But not so fast, on the left side is a function with two arguments and on the right side the functions have one argument - yet they share the same name. How does `Julia` know which to use? `Julia` uses the number, order, and *type* of the positional arguments passed to a function to determine which function definition to use. This is technically known as [multiple dispatch](http://en.wikipedia.org/wiki/Multiple_dispatch) or **polymorphism**. As a feature of the language, it can be used to greatly simplify the number of functions the user must learn. The basic idea is that many functions are "generic" in that they have methods which will work differently in different scenarios.
But not so fast, on the left side is a function with two arguments and on the right side the functions have one argument--yet they share the same name. How does `Julia` know which to use? `Julia` uses the number, order, and *type* of the positional arguments passed to a function to determine which function definition to use. This is technically known as [multiple dispatch](http://en.wikipedia.org/wiki/Multiple_dispatch) or **polymorphism**. As a feature of the language, it can be used to greatly simplify the number of functions the user must learn. The basic idea is that many functions are "generic" in that they have methods which will work differently in different scenarios.
:::{.callout-warning}
@@ -696,7 +764,7 @@ Multiple dispatch is very common in mathematics. For example, we learn different
:::
`Julia` is similarly structured. `Julia` terminology would be to call the operation "`+`" a *generic function* and the different implementations *methods* of "`+`". This allows the user to just need to know a smaller collection of generic concepts yet still have the power of detail-specific implementations. To see how many different methods are defined in the base `Julia` language for the `+` operator, we can use the command `methods(+)`. As there are so many (well over $200$ when `Julia` is started), we illustrate how many different logarithm methods are implemented for "numbers:"
`Julia` is similarly structured. `Julia` terminology would be to call the operation "`+`" a *generic function* and the different implementations *methods* of "`+`". This allows the user to just need to know a smaller collection of generic concepts yet still have the power of detail-specific implementations. To see how many different methods are defined in the base `Julia` language for the `+` operator, we can use the command `methods(+)`. As there are so many (well over $100$ when `Julia` is started), we illustrate how many different logarithm methods are implemented for "numbers:"
```{julia}
@@ -743,7 +811,7 @@ Representing the area of a rectangle in terms of two variables is easy, as the f
Area(w, h) = w * h
```
But the other fact about this problem - that the perimeter is $20$ - means that height depends on width. For this question, we can see that $P=2w + 2h$ so that - as a function - `height` depends on `w` as follows:
But the other fact about this problem--that the perimeter is $20$--means that height depends on width. For this question, we can see that $P=2w + 2h$ so that--as a function--`height` depends on `w` as follows:
```{julia}
@@ -813,7 +881,7 @@ function shift_right(f; c=0)
end
```
That takes some parsing. In the body of the `shift_right` is the definition of a function. But this function has no name it is *anonymous*. But what it does should be clear - it subtracts $c$ from $x$ and evaluates $f$ at this new value. Since the last expression creates a function, this function is returned by `shift_right`.
That takes some parsing. In the body of the `shift_right` is the definition of a function. But this function has no name-it is *anonymous*. But what it does should be clear--it subtracts $c$ from $x$ and evaluates $f$ at this new value. Since the last expression creates a function, this function is returned by `shift_right`.
So we could have done something more complicated like:
@@ -833,7 +901,7 @@ The value of `c` used when `l` is called is the one passed to `shift_right`. Fun
:::
Anonymous functions can be created with the `function` keyword, but we will use the "arrow" notation, `arg->body` to create them, The above, could have been defined as:
Anonymous functions can be created with the `function` keyword, but we will use the "arrow" notation, `arg->body`, to create them, The above, could have been defined as:
```{julia}
@@ -845,7 +913,11 @@ When the `->` is seen a function is being created.
:::{.callout-warning}
## Warning
Generic versus anonymous functions. Julia has two types of functions, generic ones, as defined by `f(x)=x^2` and anonymous ones, as defined by `x -> x^2`. One gotcha is that `Julia` does not like to use the same variable name for the two types. In general, Julia is a dynamic language, meaning variable names can be reused with different types of variables. But generic functions take more care, as when a new method is defined it gets added to a method table. So repurposing the name of a generic function for something else is not allowed. Similarly, repurposing an already defined variable name for a generic function is not allowed. This comes up when we use functions that return functions as we have different styles that can be used: When we defined `l = shift_right(f, c=3)` the value of `l` is assigned an anonymous function. This binding can be reused to define other variables. However, we could have defined the function `l` through `l(x) = shift_right(f, c=3)(x)`, being explicit about what happens to the variable `x`. This would add a method to the generic function `l`. Meaning, we get an error if we tried to assign a variable to `l`, such as an expression like `l=3`. The latter style is inefficient, so is not preferred.
Generic versus anonymous functions. Julia has two types of functions, generic ones, as defined by `f(x)=x^2` and anonymous ones, as defined by `x -> x^2`. One gotcha is that `Julia` does not like to use the same variable name for the two types. In general, Julia is a dynamic language, meaning variable names can be reused with different types of variables. But generic functions take more care, as when a new method is defined it gets added to a method table. So repurposing the name of a generic function for something else is not allowed. Similarly, repurposing an already defined variable name for a generic function is not allowed.
This comes up when we use functions that return functions as we have different styles that can be used: When we defined `l = shift_right(f, c=3)` the value of `l` is assigned to name an anonymous function for later use. This binding can be reused to define other variables.
However, we could have defined the function `l` through `l(x) = shift_right(f, c=3)(x)`, being explicit about what happens to the variable `x`. This would add a method to the generic function `l`. Meaning, we get an error if we tried to assign a variable to `l`, such as an expression like `l=3`. The latter style is inefficient, so is not preferred.
:::
@@ -918,6 +990,10 @@ which picks off the values of `0` and `1` in a somewhat obscure way but less ver
The `Fix2` function is also helpful when using the `f(x, p)` form for passing parameters to a function. The result of `Base.Fix2(f, p)` is a function with its parameters fixed that can be passed along for plotting or other uses.
:::{.callout-note}
## Fix
In Julia v1.12 the `Fix` constructor can fix an arbitrary position of a variadic function.
:::
### The `do` notation
@@ -1038,9 +1114,7 @@ radioq(choices, answ)
```{julia}
#| echo: false
plt = let
gr()
empty_style = (xaxis=([], false),
yaxis=([], false),
empty_style = (xticks=-4:4, yticks=-4:4,
framestyle=:origin,
legend=false)
axis_style = (arrow=true, side=:head, line=(:gray, 1))
@@ -1067,12 +1141,19 @@ plt = let
S = Shape([(k+1,k) .+ xy for xy in xys])
plot!(S; fill=(:white,), line=(:black,1))
end
current()
end
plotly()
plt
```
```{julia}
#| echo: false
plotly()
nothing
```
The `floor` function rounds down. For example, any value in $[k,k+1)$ rounds to $k$ for integer $k$.
:::
@@ -1494,7 +1575,7 @@ Where $N(m)$ counts the number of stars brighter than magnitude $m$ *per* square
A [square degree](https://en.wikipedia.org/wiki/Square_degree) is a unit of a solid angle. An entire sphere has a solid angle of $4\pi$ and $4\pi \cdot (180/\pi)^2$ square degrees.
With this we can answer agequestions, such as:
With this we can answer questions, such as:
> How many stars can we see in the sky?

View File

@@ -15,10 +15,17 @@ plotly()
---
A (univariate) mathematical function relates or associates values of $x$ to values $y$ using the notation $y=f(x)$. A key point is a given $x$ is associated with just one $y$ value, though a given $y$ value may be associated with several different $x$ values. (Graphically, this is the vertical line test.)
A (univariate) mathematical function relates or associates values of $x$ to values $y$ using the notation $y=f(x)$. A key point is a given $x$ is associated with just one $y$ value, though a given $y$ value may be associated with several different $x$ values. (Graphically, this is the horizontal line test.)
We may conceptualize such a relation in many ways: through an algebraic rule; through the graph of $f;$ through a description of what $f$ does; or through a table of paired values, say. For the moment, let's consider a function as a rule that takes in a value of $x$ and outputs a value $y$. If a rule is given defining the function, the computation of $y$ is straightforward. A different question is not so easy: for a given value $y$ what value - or *values* - of $x$ (if any) produce an output of $y$? That is, what $x$ value(s) satisfy $f(x)=y$?
We may conceptualize such a relation in many ways:
* through an algebraic rule;
* through the graph of $f$;
* through a description of what $f$ does;
* or through a table of paired values, say.
For the moment, let's consider a function as a rule that takes in a value of $x$ and outputs a value $y$. If a rule is given defining the function, the computation of $y$ is straightforward. A different question is not so easy: for a given value $y$ what value--or *values*--of $x$ (if any) produce an output of $y$? That is, what $x$ value(s) satisfy $f(x)=y$?
*If* for each $y$ in some set of values there is just one $x$ value, then this operation associates to each value $y$ a single value $x$, so it too is a function. When that is the case we call this an *inverse* function.
@@ -80,7 +87,7 @@ p
The graph of a function is a representation of points $(x,f(x))$, so to *find* $y = f(c)$ from the graph, we begin on the $x$ axis at $c$, move vertically to the graph (the point $(c, f(c))$), and then move horizontally to the $y$ axis, intersecting it at $y = f(c)$. The figure shows this for $c=2$, from which we can read that $f(c)$ is about $4$. This is how an $x$ is associated to a single $y$.
If we were to *reverse* the direction, starting at $y = f(d)$ on the $y$ axis and then moving horizontally to the graph, and then vertically to the $x$-axis we end up at a value $d$ with the correct $f(d)$. This allows solving for $x$ knowing $y$ in $y=f(x)$.
If we were to *reverse* the direction, starting at $y = f(d)$ on the $y$ axis and then moving horizontally to the graph, and then vertically to the $x$-axis we end up at a value $d$ with the correct value of $f(d)$. This allows solving for $x$ knowing $y$ in $y=f(x)$.
The operation described will form a function **if** the initial movement horizontally is guaranteed to find *no more than one* value on the graph. That is, to have an inverse function, there can not be two $x$ values corresponding to a given $y$ value. This observation is often visualized through the "horizontal line test" - the graph of a function with an inverse function can only intersect a horizontal line at most in one place.
@@ -195,7 +202,7 @@ In the section on the [intermediate value theorem](../limits/intermediate_value_
## Functions which are not always invertible
Consider the function $f(x) = x^2$. The graph - a parabola - is clearly not *monotonic*. Hence no inverse function exists. Yet, we can solve equations $y=x^2$ quite easily: $y=\sqrt{x}$ *or* $y=-\sqrt{x}$. We know the square root undoes the squaring, but we need to be a little more careful to say the square root is the inverse of the squaring function.
Consider the function $f(x) = x^2$. The graph--a parabola--is clearly not *monotonic*. Hence no inverse function exists. Yet, we can solve equations $y=x^2$ quite easily: $y=\sqrt{x}$ *or* $y=-\sqrt{x}$. We know the square root undoes the squaring, but we need to be a little more careful to say the square root is the inverse of the squaring function.
The issue is there are generally *two* possible answers. To avoid this, we might choose to only take the *non-negative* answer. To make this all work as above, we restrict the domain of $f(x)$ and now consider the related function $f(x)=x^2, x \geq 0$. This is now a monotonic function, so will have an inverse function. This is clearly $f^{-1}(x) = \sqrt{x}$. (The $\sqrt{x}$ being defined as the principle square root or the unique *non-negative* answer to $u^2-x=0$.)
@@ -231,8 +238,9 @@ Consider again the graph of a monotonic function, in this case $f(x) = x^2 + 2,
```{julia}
#| hold: true
f(x) = x^2 + 2
plot(f, 0, 4, legend=false)
plot!([2,2,0], [0,f(2),f(2)])
plot(f, 0, 4; yticks=[2,4,8,16],
legend=false, framestyle=:origin)
plot!([(2,0), (2, f(2)), (0, f(2))])
```
The graph is shown over the interval $(0,4)$, but the *domain* of $f(x)$ is all $x \geq 0$. The *range* of $f(x)$ is clearly $2 \leq y \leq \infty$.
@@ -241,24 +249,18 @@ The graph is shown over the interval $(0,4)$, but the *domain* of $f(x)$ is all
The lines layered on the plot show how to associate an $x$ value to a $y$ value or vice versa (as $f(x)$ is one-to-one). The domain then of the inverse function is all the $y$ values for which a corresponding $x$ value exists: this is clearly all values bigger or equal to $2$. The *range* of the inverse function can be seen to be all the images for the values of $y$, which would be all $x \geq 0$. This gives the relationship:
> the *range* of $f(x)$ is the *domain* of $f^{-1}(x)$; furthermore the *domain* of $f(x)$ is the *range* for $f^{-1}(x)$;
> * the *domain* of $f^{-1}(x)$ is the *range* of $f(x)$;
> * the *range* of $f^{-1}(x)$ is the *domain* of $f(x)$;
From this we can see if we start at $x$, apply $f$ we get $y$, if we then apply $f^{-1}$ we will get back to $x$ so we have:
> For all $x$ in the domain of $f$: $f^{-1}(f(x)) = x$.
Similarly, were we to start on the $y$ axis, we would see:
> For all $x$ in the domain of $f^{-1}$: $f(f^{-1}(x)) = x$.
In short $f^{-1} \circ f$ and $f \circ f^{-1}$ are both identity functions, though on possibly different domains.
@@ -280,11 +282,12 @@ Let's see this in action. Take the function $2^x$. We can plot it by generating
f(x) = 2^x
xs = range(0, 2, length=50)
ys = f.(xs)
plot(xs, ys, color=:blue, label="f")
plot!(ys, xs, color=:red, label="f⁻¹") # the inverse
plot(xs, ys; color=:blue, label="f",
aspect_ratio=:equal, framestyle=:origin, xlims=(0,4))
plot!(ys, xs; color=:red, label="f⁻¹") # the inverse
```
By flipping around the $x$ and $y$ values in the `plot!` command, we produce the graph of the inverse function - when viewed as a function of $x$. We can see that the domain of the inverse function (in red) is clearly different from that of the function (in blue).
By flipping around the $x$ and $y$ values in the `plot!` command, we produce the graph of the inverse function--when viewed as a function of $x$. We can see that the domain of the inverse function (in red) is clearly different from that of the function (in blue).
The inverse function graph can be viewed as a symmetry of the graph of the function. Flipping the graph for $f(x)$ around the line $y=x$ will produce the graph of the inverse function: Here we see for the graph of $f(x) = x^{1/3}$ and its inverse function:
@@ -295,15 +298,16 @@ The inverse function graph can be viewed as a symmetry of the graph of the funct
f(x) = cbrt(x)
xs = range(-2, 2, length=150)
ys = f.(xs)
plot(xs, ys, color=:blue, aspect_ratio=:equal, legend=false)
plot!(ys, xs, color=:red)
plot!(identity, color=:green, linestyle=:dash)
x, y = 1/2, f(1/2)
plot!([x,y], [y,x], color=:green, linestyle=:dot)
plot(xs, ys; color=:blue,
aspect_ratio=:equal, legend=false)
plot!(ys, xs; line=(:red,))
plot!(identity; line=(:green, :dash))
x = 1/4
y = f(x)
plot!([(x,y), (y,x)]; line=(:green, :dot))
```
We drew a line connecting $(1/2, f(1/2))$ to $(f(1/2),1/2)$. We can see that it crosses the line $y=x$ perpendicularly, indicating that points are symmetric about this line. (The plotting argument `aspect_ratio=:equal` ensures that the $x$ and $y$ axes are on the same scale, so that this type of line will look perpendicular.)
We drew a line connecting $(1/4, f(1/4))$ to $(f(1/4),1/4)$. We can see that it crosses the line $y=x$ perpendicularly, indicating that points are symmetric about this line. (The plotting argument `aspect_ratio=:equal` ensures that the $x$ and $y$ axes are on the same scale, so that this type of line will look perpendicular.)
One consequence of this symmetry, is that if $f$ is strictly increasing, then so is its inverse.
@@ -472,7 +476,7 @@ plotly()
nothing
```
The key here is that the shape of $f(x)$ near $x=c$ is somewhat related to the shape of $f^{-1}(x)$ at $f(c)$. In this case, if we use the tangent line as a fill in for how steep a function is, we see from the relationship that if $f(x)$ is "steep" at $x=c$, then $f^{-1}(x)$ will be "shallow" at $x=f(c)$.
The key here is that the shape of $f(x)$ near $x=c$ is directly related to the shape of $f^{-1}(x)$ at $f(c)$. In this case, if we use the tangent line as a fill in for how steep a function is, we see from the relationship that if $f(x)$ is "steep" at $x=c$, then $f^{-1}(x)$ will be "shallow" at $x=f(c)$.
## Questions

View File

@@ -165,7 +165,7 @@ Integers are often used casually, as they come about from parsing. As with a cal
As per IEEE Standard 754, the `Float64` type gives 52 bits to the precision (with an additional implied one), 11 bits to the exponent and the other bit is used to represent the sign. Positive, finite, floating point numbers have a range approximately between $10^{-308}$ and $10^{308}$, as 308 is about $\log_{10} 2^{1023}$. The numbers are not evenly spread out over this range, but, rather, are much more concentrated closer to $0$.
The use of 32-bit floating point values is common, as some widley used computer chips expect this. These values have a narrower range of possible values.
The use of 32-bit floating point values is common, as some widely used computer chips expect this. These values have a narrower range of possible values.
:::{.callout-warning}
## More on floating point numbers
@@ -404,6 +404,42 @@ Though complex numbers are stored as pairs of numbers, the imaginary unit, `im`,
:::
### Strings and symbols
For text, `Julia` has a `String` type. When double quotes are used to specify a string, the parser creates this type:
```{julia}
x = "The quick brown fox jumped over the lazy dog"
typeof(x)
```
Values can be inserted into a string through *interpolation* using a dollar sign.
```{julia}
animal = "lion"
x = "The quick brown $(animal) jumped over the lazy dog"
```
The use of parentheses allows more complicated expressions; it isn't always necessary.
Longer strings can be produced using *triple* quotes:
```{julia}
lincoln = """
Four score and seven years ago our fathers brought forth, upon this continent, a new nation, conceived in Liberty, and dedicated to the proposition that all men are created equal.
"""
```
Strings are comprised of *characters* which can be produced directly using *single* quotes:
```{julia}
'c'
```
We won't use these.
Finally, `Julia` has *symbols* which are *interned* strings which are used as identifiers. Symbols are used for advanced programming techniques; we will only see them as shortcuts to specify plotting arguments.
## Type stability

View File

@@ -9,7 +9,7 @@ This section will use the following packages:
```{julia}
using CalculusWithJulia
using Plots
plotly()
plotly();
```
```{julia}
@@ -18,6 +18,7 @@ plotly()
using Roots
using SymPy
using DataFrames
using Latexify
nothing
```
@@ -83,7 +84,7 @@ plotly()
(Certain graphics are produced with the `gr()` backend.)
With `Plots` loaded, it is straightforward to graph a function.
With `Plots` loaded and a backend chosen, it is straightforward to graph a function.
For example, to graph $f(x) = 1 - x^2/2$ over the interval $[-3,3]$ we have:
@@ -97,8 +98,9 @@ plot(f, -3, 3)
The `plot` command does the hard work behind the scenes. It needs $2$ pieces of information declared:
* **What** to plot. With this invocation, this detail is expressed by passing a function object to `plot`
* **Where** to plot; the `xmin` and `xmax` values. As with a sketch, it is impossible in this case to render a graph with all possible $x$ values in the domain of $f$, so we need to pick some viewing window. In the example this is $[-3,3]$ which is expressed by passing the two endpoints as the second and third arguments.
* **What** to plot. With this invocation, this detail is expressed by passing a function object to `plot`
* **Where** to plot; the `xmin` and `xmax` values. As with a sketch, it is impossible in this case to render a graph with all possible $x$ values in the domain of $f$, so we need to pick some viewing window. In the example this has $x$ limits of $[-3,3]$; expressed by passing the two endpoints as the second and third arguments.
Plotting a function is then this simple: `plot(f, xmin, xmax)`.
@@ -198,9 +200,9 @@ This choices does *not* show the $x-y$ axes. As such, we might layer on the axes
To emphasize concepts, we may stylize a function graph, rather than display the basic graphic. For example, in this graphic highlighting the amount the function goes up as it moves from $1$ to $x$:
```{julia}
gr()
#| echo: false
let
plt = let
gr()
f(x) = x^2
empty_style = (xaxis=([], false),
@@ -239,6 +241,7 @@ let
])
current()
end
plt
```
```{julia}
@@ -315,6 +318,7 @@ plot(xs, ys)
This plots the points as pairs and then connects them in order using straight lines. Basically, it creates a dot-to-dot graph. The above graph looks primitive, as it doesn't utilize enough points.
##### Example: Reflections
@@ -396,17 +400,16 @@ The graph is that of the "inverse function" for $\sin(x), x \text{ in } [-\pi/2,
When plotting a univariate function there are three basic patterns that can be employed. We have examples above of:
* `plot(f, xmin, xmax)` uses an adaptive algorithm to identify values for $x$ in the interval `[xmin, xmas]`,
* `plot(xs, f.(xs))` to manually choose the values of $x$ to plot points for, and
* `plot(f, xmin, xmax)` uses a recipe implementing an adaptive algorithm to identify values for $x$ in the interval `[xmin, xmas]`,
* `plot(xs, f.(xs))` to manually choose the values of $x$ to plot points for, and
Finally, there is a merging of the first two following the pattern:
* `plot(xs, f)`
Finally there is a merging of these following either of these patterns:
* `plot(f, xs)` *or* `plot(xs, f)`
Both require a manual choice of the values of the $x$-values to plot, but the broadcasting is carried out in the `plot` command. This style is convenient, for example, to down sample the $x$ range to see the plotting mechanics, such as:
All require a manual choice of the values of the $x$-values to plot, but the broadcasting is carried out in the `plot` command. This style is convenient, for example, to down sample the $x$ range to see the plotting mechanics, such as:
```{julia}
@@ -416,10 +419,10 @@ plot(0:pi/4:2pi, sin)
#### NaN values
At times it is not desirable to draw lines between each successive point. For example, if there is a discontinuity in the function or if there were a vertical asymptote, such as what happens at $0$ with $f(x) = 1/x$.
At times it is not desirable to draw lines between each successive point. For example, if there is a discontinuity in the function or if there were a vertical asymptote.
The most straightforward plot is dominated by the vertical asymptote at $x=0$:
For example,what happens at $0$ with $f(x) = 1/x$. The most straightforward plot is dominated by the vertical asymptote at $x=0$:
```{julia}
@@ -437,10 +440,10 @@ As we see, even with this adjustment, the spurious line connecting the points wi
plot(q, -1, 1, ylims=(-10,10))
```
The dot-to-dot algorithm, at some level, assumes the underlying function is continuous; here $q(x)=1/x$ is not.
The dot-to-dot algorithm, at some level, assumes the underlying function is *continuous*; here $q(x)=1/x$ is not.
There is a convention for most plotting programs that **if** the $y$ value for a point is `NaN` that no lines will connect to that point, `(x,NaN)`. `NaN` conveniently appears in many cases where a plot may have an issue, though not with $1/x$ as `1/0` is `Inf` and not `NaN`. (Unlike, say, `0/0` which is NaN.)
There is a convention for most plotting programs that **if** the $y$ value for a point is `NaN` then no lines will connect to that point, `(x,NaN)`. `NaN` conveniently appears in many cases where a plot may have an issue, though not with $1/x$ as `1/0` is `Inf` and not `NaN`. (Unlike, say, `0/0` which is NaN.)
Here is one way to plot $q(x) = 1/x$ over $[-1,1]$ taking advantage of this convention:
@@ -457,7 +460,7 @@ plot(xs, ys)
By using an odd number of points, we should have that $0.0$ is amongst the `xs`. The next to last line replaces the $y$ value that would be infinite with `NaN`.
As a recommended alternative, we might modify the function so that if it is too large, the values are replaced by `NaN`. Here is one such function consuming a function and returning a modified function put to use to make this graph:
The above is fussy. As a recommended alternative, we might modify the function so that if it is too large, the values are replaced by `NaN`. Here is one such function consuming a function and returning a modified function put to use to make this graph:
```{julia}
@@ -471,7 +474,7 @@ plot(rangeclamp(x -> 1/x), -1, 1)
## Layers
Graphing more than one function over the same viewing window is often desirable. Though this is easily done in `Plots` by specifying a vector of functions as the first argument to `plot` instead of a single function object, we instead focus on building the graph layer by layer.
Graphing more than one function over the same viewing window is often desirable. Though this is easily done all at once in `Plots` by specifying a vector of functions as the first argument to `plot` instead of a single function object, we instead focus on building the graph layer by layer.^[The style of `Plots` is to combine multiple *series* to plot into one object and let `Plots` sort out which (every column is treated as a separate series). This can be very efficient from a programming perspective, but we leave it for power users. The use of layers, seems much easier to understand.]
For example, to see that a polynomial and the cosine function are "close" near $0$, we can plot *both* $\cos(x)$ and the function $f(x) = 1 - x^2/2$ over $[-\pi/2,\pi/2]$:
@@ -505,8 +508,8 @@ For another example, suppose we wish to plot the function $f(x)=x\cdot(x-1)$ ove
```{julia}
#| hold: true
f(x) = x*(x-1)
plot(f, -1, 2, legend=false) # turn off legend
f(x) = x * (x-1)
plot(f, -1, 2; legend=false) # turn off legend
plot!(zero)
scatter!([0,1], [0,0])
```
@@ -514,43 +517,53 @@ scatter!([0,1], [0,0])
The $3$ main functions used in these notes for adding layers are:
* `plot!(f, a, b)` to add the graph of the function `f`; also `plot!(xs, ys)`
* `scatter!(xs, ys)` to add points $(x_1, y_1), (x_2, y_2), \dots$.
* `annotate!((x,y, label))` to add a label at $(x,y)$
* `plot!(f, a, b)` to add the graph of the function `f`; also `plot!(xs, ys)`
* `scatter!(xs, ys)` to add points $(x_1, y_1), (x_2, y_2), \dots$.
* `annotate!((x,y, label))` to add a label at $(x,y)$
:::{.callout-warning}
## Warning
## Trailing ! convention
Julia has a convention to use functions named with a `!` suffix to indicate that they mutate some object. In this case, the object is the current graph, though it is implicit. Both `plot!`, `scatter!`, and `annotate!` (others too) do this by adding a layer.
:::
## Additional arguments
The `Plots` package provides many arguments for adjusting a graphic, here we mention just a few of the [attributes](https://docs.juliaplots.org/latest/attributes/):
The `Plots` package uses positional arguments for input data and keyword arguments for [attributes](https://docs.juliaplots.org/latest/attributes/).
The `Plots` package provides many such arguments for adjusting a graphic, here we mention just a few:
* `plot(..., title="main title", xlab="x axis label", ylab="y axis label")`: add title and label information to a graphic
* `plot(..., color="green")`: this argument can be used to adjust the color of the drawn figure (color can be a string,`"green"`, or a symbol, `:green`, among other specifications)
* `plot(..., linewidth=5)`: this argument can be used to adjust the width of drawn lines
* `plot(..., xlims=(a,b), ylims=(c,d))`: either or both `xlims` and `ylims` can be used to control the viewing window
* `plot(..., linestyle=:dash)`: will change the line style of the plotted lines to dashed lines. Also `:dot`, ...
* `plot(..., aspect_ratio=:equal)`: will keep $x$ and $y$ axis on same scale so that squares look square.
* `plot(..., legend=false)`: by default, different layers will be indicated with a legend, this will turn off this feature
* `plot(..., label="a label")` the `label` attribute will show up when a legend is present. Using an empty string, `""`, will suppress add the layer to the legend.
* `plot(...; title="main title", xlab="x axis label", ylab="y axis label")`: add title and label information to a graphic
* `plot(...; color="green")`: this argument can be used to adjust the color of the drawn figure (color can be a string,`"green"`, or a symbol, `:green`, among other specifications)
* `plot(...; linewidth=5)`: this argument can be used to adjust the width of drawn lines
* `plot(...; linestyle=:dash)`: will change the line style of the plotted lines to dashed lines. Also `:dot`, ...
* `plot(...; label="a label")` the `label` attribute will show up when a legend is present. Using an empty string, `""`, will suppress add the layer to the legend.
* `plot(...; legend=false)`: by default, different layers will be indicated with a legend, this will turn off this feature
* `plot(...; xlims=(a,b), ylims=(c,d))`: either or both `xlims` and `ylims` can be used to control the viewing window
* `plot(...; xticks=[xs..], yticks=[ys...]: either or both `xticks` and `yticks` can be used to specify where the tick marks are to be drawn
* `plot(...; aspect_ratio=:equal)`: will keep $x$ and $y$ axis on same scale so that squares look square.
* `plot(...; framestyle=:origin)`: The default `framestyle` places $x$-$y$ guides on the edges; this specification places them on the $x-y$ plane.
For plotting points with `scatter`, or `scatter!` the markers can be adjusted via
* `scatter(..., markersize=5)`: increase marker size
* `scatter(..., marker=:square)`: change the marker (uses a symbol, not a string to specify)
* `scatter(...; markersize=5)`: increase marker size
* `scatter(...; marker=:square)`: change the marker (uses a symbol, not a string to specify)
Of course, zero, one, or more of these can be used on any given call to `plot`, `plot!`, `scatter`, or `scatter!`.
There are also several *shorthands* in `Plots` that allows several related attributes to be specified to a single argument that is disambiguated using the type of the value. (Eg. `line=(5, 0.25, "blue")` will specify the line have width `5`, color `blue`, and alpha-transparency `0.25`.)
### Shorthands
There are also several *shorthands* in `Plots` that allows several related attributes to be specified to a single argument that is disambiguated using the type of the value. A few used herein are:
* `line`. For example, `line=(5, 0.25, "blue")` will specify `linewidth=5` (integer), `linecolor="blue"` (string or symbol), `linealpha=0.25` (floating point)
* `marker`. For example `marker=(:star, 5)` will specify `markerstyle=:star` (symbol) and `markersize=5` (integer).
* `fill`. For example `fill=(:blue, 0.25)` will specify `fillcolor=:blue` (string or symbol) and `fillalpha=0.25` (floating point).
#### Example: Bresenham's algorithm
@@ -607,9 +620,9 @@ p = plot(f, x₀, x₁; legend=false, aspect_ratio=:equal,
col = RGBA(.64,.64,.64, 0.25)
for xy ∈ xs
x, y = xy
scatter!([x], [y]; markersize=5)
scatter!([x+1], [y - 1/2], markersize=5, markershape=:star7)
plot!(Shape(x .+ [0, 1, 1, 0], y .+ [0, 0, -1, -1]), color=col)
scatter!([x], [y]; marker=(5,))
scatter!([x+1], [y - 1/2]; marker=(5,:star))
plot!(Shape(x .+ [0, 1, 1, 0], y .+ [0, 0, -1, -1]); color=col)
end
p
```
@@ -618,14 +631,46 @@ We see a number of additional arguments used: different marker sizes and shapes
Of course, generalizations for positive slope and slope with magnitude greater than $1$ are needed. As well, this basic algorithm could be optimized, especially if it is part of a lower-level drawing primitive. But this illustrates the considerations involved.
## Points, lines, polygons
Two basic objects to graph are points and lines. Add to these polygons.
A point in two-dimensional space has two coordinates, often denoted by $(x,y)$. In `Julia`, the same notation produces a `tuple`. Using square brackets, as in `[x,y]`, produces a vector. Vectors are are more commonly used in these notes, as we have seen there are algebraic operations defined for them. However, tuples have other advantages and are how `Plots` designates a point.
The plot command `plot(xs, ys)` plots the points $(x_1,y_1), \dots, (x_n, y_n)$ and then connects adjacent points with with lines. The command `scatter(xs, ys)` just plots the points.
However, the points might be more naturally specified as coordinate pairs. If tuples are used to pair them off, then `Plots` will plot a vector of tuples as a sequence of points through `plot([(x1,y1), (x2, y2), ..., (xn, yn)])`:
```{julia}
pts = [( 1, 0), ( 1/4, 1/4), (0, 1), (-1/4, 1/4),
(-1, 0), (-1/4, -1/4), (0, -1), ( 1/4, -1/4)]
scatter(pts; legend=false)
```
A line segment simply connects two points. While these can be specified as vectors of $x$ and $y$ values, again it may be more convenient to use coordinate pairs to specify the points. Continuing the above, we can connect adjacent points with line segments:
```{julia}
plot!(pts; line=(:gray, 0.5, :dash))
```
This uses the shorthand notation of `Plots` to specify `linecolor=:gray, linealpha=0.5, linestyle=:dash`. To plot just a line segment, just specifying two points suffices.
The four-pointed star is not closed off, as there isn't a value from the last point to the first point. A polygon closes itself off. The `Shape` function can take a vector of points or a pair of `xs` and `ys` to specify a polygon. When these are plotted, the arguments to `fill` describe the interior of the polygon, the arguments to `line` the boundary:
```{julia}
plot(Shape(pts); fill=(:gray, 0.25), line=(:black, 2), legend=false)
scatter!(pts)
```
## Graphs of parametric equations
If we have two functions $f(x)$ and $g(x)$ there are a few ways to investigate their joint behavior. As just mentioned, we can graph both $f$ and $g$ over the same interval using layers. Such a graph allows an easy comparison of the shape of the two functions and can be useful in solving $f(x) = g(x)$. For the latter, the graph of $h(x) = f(x) - g(x)$ is also of value: solutions to $f(x)=g(x)$ appear as crossing points on the graphs of `f` and `g`, whereas they appear as zeros (crossings of the $x$-axis) when `h` is plotted.
If we have two functions $f(x)$ and $g(x)$ there are a few ways to investigate their joint behavior. As mentioned, we can graph both $f$ and $g$ over the same interval using layers. Such a graph allows an easy comparison of the shape of the two functions and can be useful in solving $f(x) = g(x)$, as the $x$ solutions are where the two curves intersect.
A different graph can be made to compare the two functions side-by-side. This is a parametric plot. Rather than plotting points $(x,f(x))$ and $(x,g(x))$ with two separate graphs, the graph consists of points $(f(x), g(x))$. We illustrate with some examples below:
A different graph can be made to compare the two functions side-by-side. This is a parametric plot. Rather than plotting points $(x,f(x))$ and $(x,g(x))$ with two separate graphs, the graph consists of points $(f(x), g(x))$ over a range of $x$ values. We illustrate with some examples below:
##### Example
@@ -643,7 +688,7 @@ plot(f.(ts), g.(ts), aspect_ratio=:equal) # make equal axes
Any point $(a,b)$ on this graph is represented by $(\cos(t), \sin(t))$ for some value of $t$, and in fact multiple values of $t$, since $t + 2k\pi$ will produce the same $(a,b)$ value as $t$ will.
Making the parametric plot is similar to creating a plot using lower level commands. There a sequence of values is generated to approximate the $x$ values in the graph (`xs`), a set of commands to create the corresponding function values (e.g., `f.(xs)`), and some instruction on how to represent the values, in this case with lines connecting the points (the default for `plot` for two sets of numbers).
Making the parametric plot is similar to creating a plot using lower level commands. There a sequence of values is generated to approximate the $x$ values in the graph (`xs`), a set of commands to create the corresponding function values (e.g., `f.(xs)`), and some instruction on how to represent the values, in this case with lines connecting the points (the default for `plot` for two vectors of numbers).
In this next plot, the angle values are chosen to be the familiar ones, so the mechanics of the graph can be emphasized. Only the upper half is plotted:
@@ -653,7 +698,7 @@ In this next plot, the angle values are chosen to be the familiar ones, so the m
#| hold: true
#| echo: false
θs =[0, PI/6, PI/4, PI/3, PI/2, 2PI/3, 3PI/4,5PI/6, PI]
DataFrame(θ=θs, x=cos.(θs), y=sin.(θs))
latexify(DataFrame(θ=θs, x=cos.(θs), y=sin.(θs)))
```
```{julia}
@@ -718,7 +763,7 @@ This graph is *nearly* a straight line. At the point $(0,0)=(f(0), g(0))$, we se
##### Example: Etch A Sketch
[Etch A sketch](http://en.wikipedia.org/wiki/Etch_A_Sketch) is a drawing toy where two knobs control the motion of a pointer, one knob controlling the $x$ motion, the other the $y$ motion. The trace of the movement of the pointer is recorded until the display is cleared by shaking. Shake to clear is now a motion incorporated by some smart-phone apps.
[Etch A Sketch](http://en.wikipedia.org/wiki/Etch_A_Sketch) is a drawing toy where two knobs control the motion of a pointer, one knob controlling the $x$ motion, the other the $y$ motion. The trace of the movement of the pointer is recorded until the display is cleared by shaking. Shake to clear is now a motion incorporated by some smart-phone apps.
Playing with the toy makes a few things become clear:
@@ -757,38 +802,6 @@ plot(f, g, 0, max((R-r)/r, r/(R-r))*2pi)
In the above, one can fix $R=1$. Then different values for `r` and `rho` will produce different graphs. These graphs will be periodic if $(R-r)/r$ is a rational. (Nothing about these equations requires $\rho < r$.)
## Points, lines, polygons
Two basic objects to graph are points and lines.
A point in two-dimensional space has two coordinates, often denoted by $(x,y)$. In `Julia`, the same notation produces a `tuple`. Using square brackets, as in `[x,y]`, produces a vector. Vectors are usually used, as we have seen there are algebraic operations defined for them. However, tuples have other advantages and are how `Plots` designates a point.
The plot command `plot(xs, ys)` plots the points $(x_1,y_1), \dots, (x_n, y_n)$ and then connects adjacent points with with lines. The command `scatter(xs, ys)` just plots the points.
However, the points might be more naturally specified as coordinate pairs. If tuples are used to pair them off, then `Plots` will plot a vector of tuples as a sequence of points:
```{julia}
pts = [(1, 0), (1/4, 1/4), (0, 1), (-1/4, 1/4), (-1, 0),
(-1/4, -1/4), (0, -1), (1/4, -1/4)]
scatter(pts; legend=false)
```
A line segment simply connects two points. While these can be specified as vectors of $x$ and $y$ values, again it may be more convenient to use coordinate pairs to specify the points. Continuing the above, we can connect adjacent points with line segments:
```{julia}
plot!(pts; line=(:gray, 0.5, :dash))
```
This uses the shorthand notation of `Plots` to specify `linecolor=:gray, linealpha=0.5, linestyle=:dash`. To plot just a line segment, just specifying two points suffices.
The four-pointed star is not closed off, as there isn't a value from the last point to the first point. A polygon closes itself off. The `Shape` function can take a vector of points or a pair of `xs` and `ys` to specify a polygon. When these are plotted, the arguments to `fill` describe the interior of the polygon, the arguments to `line` the boundary:
```{julia}
plot(Shape(pts); fill=(:gray, 0.25), line=(:black, 2), legend=false)
scatter!(pts)
```
## Questions

View File

@@ -51,7 +51,12 @@ $$
gr()
anim = @animate for m in 2:2:10
fn = x -> x^m
plot(fn, -1, 1, size = fig_size, legend=false, title="graph of x^{$m}", xlims=(-1,1), ylims=(-.1,1))
title = L"graph of $x^{%$m}$"
plot(fn, -1, 1;
size = fig_size,
legend=false,
title=title,
xlims=(-1,1), ylims=(-.1,1))
end
imgfile = tempname() * ".gif"
@@ -96,7 +101,12 @@ $$
gr()
anim = @animate for m in [-5, -2, -1, 1, 2, 5, 10, 20]
fn = x -> m * x
plot(fn, -1, 1, size = fig_size, legend=false, title="m = $m", xlims=(-1,1), ylims=(-20, 20))
title = L"m = %$m"
plot(fn, -1, 1;
size = fig_size,
legend=false,
title=title,
xlims=(-1,1), ylims=(-20, 20))
end
imgfile = tempname() * ".gif"
@@ -301,7 +311,9 @@ float(y)
The use of the generic `float` method returns a floating point number. `SymPy` objects have their own internal types. To preserve these on conversion to a related `Julia` value, the `N` function from `SymPy` is useful:
The use of the generic `float` method returns a floating point number. (The `.evalf()` method of `SymPy` objects uses `SymPy` to produce floating point versions of symbolic values.
`SymPy` objects have their own internal types. To preserve these on conversion to a related `Julia` value, the `N` function from `SymPy` is useful:
```{julia}
@@ -310,14 +322,7 @@ p = -16x^2 + 100
N(p(2))
```
Where `convert(T, x)` requires a specification of the type to convert `x` to, `N` attempts to match the data type used by SymPy to store the number. As such, the output type of `N` may vary (rational, a BigFloat, a float, etc.) For getting more digits of accuracy, a precision can be passed to `N`. The following command will take the symbolic value for $\pi$, `PI`, and produce about $60$ digits worth as a `BigFloat` value:
```{julia}
N(PI, 60)
```
Conversion by `N` will fail if the value to be converted contains free symbols, as would be expected.
Where `convert(T, x)` requires a specification of the type to convert `x` to, `N` attempts to match the data type used by SymPy to store the number. As such, the output type of `N` may vary (rational, a BigFloat, a float, etc.) Conversion by `N` will fail if the value to be converted contains free symbols, as would be expected.
### Converting symbolic expressions into Julia functions
@@ -362,6 +367,8 @@ pp = lambdify(p, (x,a,b))
pp(1,2,3) # computes 2*1^2 + 3
```
(We suggest using the pair notation when there is more than one variable.)
## Graphical properties of polynomials
@@ -394,7 +401,12 @@ To investigate this last point, let's consider the case of the monomial $x^n$. W
gr()
anim = @animate for m in 0:2:12
fn = x -> x^m
plot(fn, -1.2, 1.2, size = fig_size, legend=false, xlims=(-1.2, 1.2), ylims=(0, 1.2^12), title="x^{$m} over [-1.2, 1.2]")
title = L"$x^{%$m}$ over $[-1.2, 1.2]$"
plot(fn, -1.2, 1.2;
size = fig_size,
legend=false,
xlims=(-1.2, 1.2), ylims=(0, 1.2^12),
title=title)
end
imgfile = tempname() * ".gif"
@@ -433,8 +445,13 @@ anim = @animate for n in 1:6
m = [1, .5, -1, -5, -20, -25]
M = [2, 4, 5, 10, 25, 30]
fn = x -> (x-1)*(x-2)*(x-3)*(x-5)
title = L"Graph of on $(%$(m[n]), %$(M[n]))$"
plt = plot(fn, m[n], M[n], size=fig_size, legend=false, linewidth=2, title ="Graph of on ($(m[n]), $(M[n]))")
plt = plot(fn, m[n], M[n];
size=fig_size,
legend=false,
linewidth=2,
title=title)
if n > 1
plot!(plt, fn, m[n-1], M[n-1], color=:red, linewidth=4)
end
@@ -468,11 +485,20 @@ The following graphic illustrates the $4$ basic *overall* shapes that can result
```{julia}
#| echo: false
plot(; layout=4)
plot!(x -> x^4, -3,3, legend=false, xticks=false, yticks=false, subplot=1, title="n = even, aₙ > 0")
plot!(x -> x^5, -3,3, legend=false, xticks=false, yticks=false, subplot=2, title="n = odd, aₙ > 0")
plot!(x -> -x^4, -3,3, legend=false, xticks=false, yticks=false, subplot=3, title="n = even, aₙ < 0")
plot!(x -> -x^5, -3,3, legend=false, xticks=false, yticks=false, subplot=4, title="n = odd, aₙ < 0")
let
gr()
plot(; layout=4)
plot!(x -> x^4, -3,3, legend=false, xticks=false, yticks=false, subplot=1, title=L"$n = $ even, $a_n > 0$")
plot!(x -> x^5, -3,3, legend=false, xticks=false, yticks=false, subplot=2, title=L"$n = $ odd, $a_m > 0$")
plot!(x -> -x^4, -3,3, legend=false, xticks=false, yticks=false, subplot=3, title=L"$n = $ even, $a_n < 0$")
plot!(x -> -x^5, -3,3, legend=false, xticks=false, yticks=false, subplot=4, title=L"$n = $ odd, $a_n < 0$")
end
```
```{julia}
#| echo: false
plotly()
nothing
```
##### Example
@@ -513,14 +539,15 @@ This observation is the start of Descartes' rule of [signs](http://sepwww.stanfo
Among numerous others, there are two common ways of representing a non-zero polynomial:
* expanded form, as in $a_n x^n + a_{n-1}x^{n-1} + \cdots + a_1 x + a_0, a_n \neq 0$; or
* factored form, as in $a\cdot(x-r_1)\cdot(x-r_2)\cdots(x-r_n), a \neq 0$.
* expanded form, as in $a_n x^n + a_{n-1}x^{n-1} + \cdots + a_1 x + a_0,\quad a_n \neq 0$; or
* factored form, as in $a\cdot(x-r_1)\cdot(x-r_2)\cdots(x-r_n), \quad a \neq 0$.
The former uses the *standard basis* to represent the polynomial $p$.
The latter writes $p$ as a product of linear factors, though this is only possible in general if we consider complex roots. With real roots only, then the factors are either linear or quadratic, as will be discussed later.
There are values to each representation. One value of the expanded form is that polynomial addition and scalar multiplication is much easier than in factored form. For example, adding polynomials just requires matching up the monomials of similar powers. For the factored form, polynomial multiplication is much easier than expanded form. For the factored form it is easy to read off *roots* of the polynomial (values of $x$ where $p$ is $0$), as a product is $0$ only if a term is $0$, so any zero must be a zero of a factor. Factored form has other technical advantages. For example, the polynomial $(x-1)^{1000}$ can be compactly represented using the factored form, but would require $1001$ coefficients to store in expanded form. (As well, due to floating point differences, the two would evaluate quite differently as one would require over a $1000$ operations to compute, the other just two.)
There are values to each representation. One value of the expanded form is that polynomial addition and scalar multiplication is much easier than in factored form. For example, adding polynomials just requires matching up the monomials of similar powers. (These can be realized easily as vector operations.) For the factored form, polynomial multiplication is much easier than expanded form. For the factored form it is easy to read off *roots* of the polynomial (values of $x$ where $p$ is $0$), as a product is $0$ only if a term is $0$, so any zero must be a zero of a factor. Factored form has other technical advantages. For example, the polynomial $(x-1)^{1000}$ can be compactly represented using the factored form, but would require $1001$ coefficients to store in expanded form. (As well, due to floating point differences, the two would evaluate quite differently as one would require over a $1000$ operations to compute, the other just two.)
Translating from factored form to expanded form can be done by carefully following the distributive law of multiplication. For example, with some care it can be shown that:
@@ -587,7 +614,7 @@ It is easy to create a symbolic expression from a function - just evaluate the f
f(x)
```
This is easy - but can also be confusing. The function object is `f`, the expression is `f(x)` - the function evaluated on a symbolic object. Moreover, as seen, the symbolic expression can be evaluated using the same syntax as a function call:
This is easy--but can also be confusing. The function object is `f`, the expression is `f(x)`--the function evaluated on a symbolic object. Moreover, as seen, the symbolic expression can be evaluated using the same syntax as a function call:
```{julia}

View File

@@ -453,7 +453,7 @@ N.(solveset(p ~ 0, x))
## Do numeric methods matter when you can just graph?
It may seem that certain practices related to roots of polynomials are unnecessary as we could just graph the equation and look for the roots. This feeling is perhaps motivated by the examples given in textbooks to be worked by hand, which necessarily focus on smallish solutions. But, in general, without some sense of where the roots are, an informative graph itself can be hard to produce. That is, technology doesn't displace thinking - it only supplements it.
It may seem that certain practices related to roots of polynomials are unnecessary as we could just graph the equation and look for the roots. This feeling is perhaps motivated by the examples given in textbooks to be worked by hand, which necessarily focus on smallish solutions. But, in general, without some sense of where the roots are, an informative graph itself can be hard to produce. That is, technology doesn't displace thinking--it only supplements it.
For another example, consider the polynomial $(x-20)^5 - (x-20) + 1$. In this form we might think the roots are near $20$. However, were we presented with this polynomial in expanded form: $x^5 - 100x^4 + 4000x^3 - 80000x^2 + 799999x - 3199979$, we might be tempted to just graph it to find roots. A naive graph might be to plot over $[-10, 10]$:
@@ -553,7 +553,7 @@ N.(solve(j ~ 0, x))
### Cauchy's bound on the magnitude of the real roots.
Descartes' rule gives a bound on how many real roots there may be. Cauchy provided a bound on how large they can be. Assume our polynomial is monic (if not, divide by $a_n$ to make it so, as this won't effect the roots). Then any real root is no larger in absolute value than $|a_0| + |a_1| + |a_2| + \cdots + |a_{n-1}| + 1$, (this is expressed in different ways.)
Descartes' rule gives a bound on how many real roots there may be. Cauchy provided a bound on how large they can be. Assume our polynomial is monic (if not, divide by $a_n$ to make it so, as this won't effect the roots). Then any real root is no larger in absolute value than $h = 1 + |a_0| + |a_1| + |a_2| + \cdots + |a_{n-1}|$, (this is expressed in different ways.)
To see precisely [why](https://captainblack.wordpress.com/2009/03/08/cauchys-upper-bound-for-the-roots-of-a-polynomial/) this bound works, suppose $x$ is a root with $|x| > 1$ and let $h$ be the bound. Then since $x$ is a root, we can solve $a_0 + a_1x + \cdots + 1 \cdot x^n = 0$ for $x^n$ as:
@@ -563,7 +563,7 @@ $$
x^n = -(a_0 + a_1 x + \cdots a_{n-1}x^{n-1})
$$
Which after taking absolute values of both sides, yields:
Which after taking absolute values of both sides, yields by the triangle inequality:
$$

View File

@@ -18,7 +18,7 @@ import SymPy # imported only: some functions, e.g. degree, need qualification
---
While `SymPy` can be used to represent polynomials, there are also native `Julia` packages available for this and related tasks. These packages include `Polynomials`, `MultivariatePolynomials`, and `AbstractAlgebra`, among many others. (A search on [juliahub.com](juliahub.com) found over $50$ packages matching "polynomial".) We will look at the `Polynomials` package in the following, as it is straightforward to use and provides the features we are looking at for univariate polynomials.
While `SymPy` can be used to represent polynomials, there are also native `Julia` packages available for this and related tasks. These packages include `Polynomials`, `MultivariatePolynomials`, and `AbstractAlgebra`, among many others. (A search on [juliahub.com](https://juliahub.com) found almost $100$ packages matching "polynomial".) We will look at the `Polynomials` package in the following, as it is straightforward to use and provides the features we are looking at for *univariate* polynomials.
## Construction
@@ -76,7 +76,7 @@ Polynomials may be evaluated using function notation, that is:
p(1)
```
This blurs the distinction between a polynomial expression a formal object consisting of an indeterminate, coefficients, and the operations of addition, subtraction, multiplication, and non-negative integer powers and a polynomial function.
This blurs the distinction between a polynomial expression--a formal object consisting of an indeterminate, coefficients, and the operations of addition, subtraction, multiplication, and non-negative integer powers--and a polynomial function.
The polynomial variable, in this case `1x`, can be returned by `variable`:
@@ -86,14 +86,15 @@ The polynomial variable, in this case `1x`, can be returned by `variable`:
x = variable(p)
```
This variable is a `Polynomial` object, so can be manipulated as a polynomial; we can then construct polynomials through expressions like:
This variable is a `Polynomial` object that prints as `x`.
These variables can be manipulated as any polynomial. We can then construct polynomials through expressions like:
```{julia}
r = (x-2)^2 * (x-1) * (x+1)
```
The product is expanded for storage by `Polynomials`, which may not be desirable for some uses. A new variable can be produced by calling `variable()`; so we could have constructed `p` by:
The product is expanded for storage by `Polynomials`, which may not be desirable for some uses. A new variable can also be produced by calling `variable()`; so we could have constructed `p` by:
```{julia}
@@ -125,7 +126,7 @@ The `Polynomials` package has different ways to represent polynomials, and a fac
fromroots(FactoredPolynomial, [2, 2, 1, -1])
```
This form is helpful for some operations, for example polynomial multiplication and positive integer exponentiation, but not others such as addition of polynomials, where such polynomials must first be converted to the standard basis to add and are then converted back into a factored form.
This form is helpful for some operations, for example polynomial multiplication and positive integer exponentiation, but not others such as addition of polynomials, where such polynomials must first be converted to the standard basis to add and are then converted back into a factored form. (A task that may suffer from floating point roundoff.)
---
@@ -181,7 +182,7 @@ A consequence of the fundamental theorem of algebra and the factor theorem is th
:::{.callout-note}
## Note
`SymPy` also has a `roots` function. If both `Polynomials` and `SymPy` are used together, calling `roots` must be qualified, as with `Polynomials.roots(...)`. Similarly, `degree` is provided in both, so it too must be qualified.
`SymPy` also has a `roots` function. If both `Polynomials` and `SymPy` are used together, calling `roots` must be qualified, as with `Polynomials.roots(...)`. Similarly, `degree` is exported in both, so it too must be qualified.
:::

View File

@@ -103,13 +103,13 @@ h = (b-a)/(n-1)
collect(a:h:b)
```
Pretty neat. If we were doing this many times - such as once per plot - we'd want to encapsulate this into a function, for example:
Pretty neat. If we were doing this many times - such as once per plot - we'd want to encapsulate this into a function, for example using a comprehension:
```{julia}
function evenly_spaced(a, b, n)
h = (b-a)/(n-1)
collect(a:h:b)
[a + i*h for i in 0:n-1]
end
```
@@ -131,10 +131,10 @@ It seems to work as expected. But looking just at the algorithm it isn't quite s
```{julia}
1/5 + 2*1/5 # last value
1/5 + 2*1/5 # last value if h is exactly 1/5 or 0.2
```
Floating point roundoff leads to the last value *exceeding* `0.6`, so should it be included? Well, here it is pretty clear it *should* be, but better to have something programmed that hits both `a` and `b` and adjusts `h` accordingly.
Floating point roundoff leads to the last value *exceeding* `0.6`, so should it be included? Well, here it is pretty clear it *should* be, but better to have something programmed that hits both `a` and `b` and adjusts `h` accordingly. Something which isn't subject to the vagaries of `(3/5 - 1/5)/2` not being `0.2`.
Enter the base function `range` which solves this seemingly simple - but not really - task. It can use `a`, `b`, and `n`. Like the range operation, this function returns a generator which can be collected to realize the values.
@@ -144,14 +144,7 @@ The number of points is specified as a third argument (though keyword arguments
```{julia}
xs = range(-1, 1,9)
```
and
```{julia}
collect(xs)
xs = range(1/5, 3/5, 3) |> collect
```
:::{.callout-note}
@@ -266,7 +259,7 @@ Here are decreasing powers of $2$:
[1/2^i for i in 1:10]
```
Sometimes, the comprehension does not produce the type of output that may be expected. This is related to `Julia`'s more limited abilities to infer types at the command line. If the output type is important, the extra prefix of `T[]` can be used, where `T` is the desired type. We will see that this will be needed at times with symbolic math.
Sometimes, the comprehension does not produce the type of output that may be expected. This is related to `Julia`'s more limited abilities to infer types at the command line. If the output type is important, the extra prefix of `T[]` can be used, where `T` is the desired type.
### Generators

View File

@@ -15,7 +15,7 @@ import Polynomials
using RealPolynomialRoots
```
The `Polynomials` package is "imported" to avoid naming collisions with `SymPy`; names will need to be qualified.
The `Polynomials` package is "imported" to avoid naming collisions with `SymPy`;some names may need to be qualified.
@@ -410,7 +410,7 @@ The usual recipe for construction follows these steps:
* Identify "test points" within each implied interval (these are $(-\infty, -1)$, $(-1,0)$, $(0,1)$, and $(1, \infty)$ in the example) and check for the sign of $f(x)$ at these test points. Write in `-`, `+`, `0`, or `*`, as appropriate. The value comes from the fact that "continuous" functions may only change sign when they cross $0$ or are undefined.
With the computer, where it is convenient to draw a graph, it might be better to emphasize the sign on the graph of the function. The `sign_chart` function from `CalculusWithJulia` does this by numerically identifying points where the function is $0$ or $\infty$ and indicating the sign as $x$ crosses over these points.
With the computer, where it is convenient to draw a graph, it might be better to emphasize the sign on the graph of the function, but at times numeric values are preferred. The `sign_chart` function from `CalculusWithJulia` does this analysis by numerically identifying points where the function is $0$ or $\infty$ and indicating the sign as $x$ crosses over these points.
```{julia}
@@ -419,6 +419,8 @@ f(x) = x^3 - x
sign_chart(f, -3/2, 3/2)
```
This format is a bit different from above, but shows to the left of $-1$ a minussign; between $-1$ and $0$ a plus sign; between $0$ and $1$ a minus sign; and between $1$ and $3/2$ a plus sign.
## Pade approximate

View File

@@ -220,7 +220,7 @@ Scaling by $2$ shrinks the non-zero domain, scaling by $1/2$ would stretch it. I
---
More exciting is what happens if we combine these operations.
More exciting is what happens if we compose these operations.
A shift right by $2$ and up by $1$ is achieved through
@@ -267,7 +267,7 @@ We can view this as a composition of "scale" by $1/a$, then "over" by $b$, and
#| hold: true
a = 2; b = 5
h(x) = stretch(over(scale(f, 1/a), b), 1/a)(x)
plot(f, -1, 8, label="f")
plot(f, -1, 8, label="f"; xticks=-1:8)
plot!(h, label="h")
```

View File

@@ -34,12 +34,37 @@ For a right triangle with angles $\theta$, $\pi/2 - \theta$, and $\pi/2$ ($0 < \
```{julia}
#| hide: true
#| echo: false
p = plot(legend=false, xlim=(-1/4,5), ylim=(-1/2, 3),
xticks=nothing, yticks=nothing, border=:none)
plot!([0,4,4,0],[0,0,3,0], linewidth=3)
del = .25
plot!([4-del, 4-del,4], [0, del, del], color=:black, linewidth=3)
annotate!([(.75, .25, "θ"), (4.0, 1.25, "opposite"), (2, -.25, "adjacent"), (1.5, 1.25, "hypotenuse")])
let
gr()
p = plot(;legend=false,
xticks=nothing, yticks=nothing,
border=:none,
xlim=(-1/4,5), ylim=(-1/2, 3))
plot!([0,4,4,0],[0,0,3,0], linewidth=3)
θ = atand(3,4)
del = .25
plot!([4-del, 4-del,4], [0, del, del], color=:black, linewidth=3)
theta = pi/20
r = sqrt((3/4)^2 + (1/4)^2)
ts = range(0, theta, 20)
plot!(r*cos.(ts), r*sin.(ts); line=(:gray, 1))
ts = range(atan(3/4) - theta, atan(3,4), 20)
plot!(r*cos.(ts), r*sin.(ts); line=(:gray, 1))
annotate!([
(.75, .25, L"\theta"),
(4.0, 1.5+.1, text("opposite", rotation=90,:top)),
(2, -.25, "adjacent"),
(2, 1.5+.1, text("hypotenuse", rotation=θ,:bottom))
])
end
```
```{julia}
#| echo: false
plotly()
nothing
```
With these, the basic definitions for the primary trigonometric functions are
@@ -77,9 +102,13 @@ gr()
function plot_angle(m)
r = m*pi
n,d = numerator(m), denominator(m)
ts = range(0, stop=2pi, length=100)
tit = "$m ⋅ pi -> ($(round(cos(r), digits=2)), $(round(sin(r), digits=2)))"
tit = latexstring("\\frac{$n}{$d}") *
L"\cdot\pi\rightarrow (" *
latexstring("$(round(cos(r), digits=2)),$(round(sin(r), digits=2)))")
ts = range(0, 2pi, 151)
p = plot(cos.(ts), sin.(ts), legend=false, aspect_ratio=:equal,title=tit)
plot!(p, [-1,1], [0,0], color=:gray30)
plot!(p, [0,0], [-1,1], color=:gray30)
@@ -95,13 +124,11 @@ function plot_angle(m)
plot!(p, [0,l*cos(r)], [0,l*sin(r)], color=:green, linewidth=4)
scatter!(p, [cos(r)], [sin(r)], markersize=5)
annotate!(p, [(1/4+cos(r), sin(r), "(x,y)")])
annotate!(p, [(1/4+cos(r), sin(r), L"(x,y)")])
p
end
## different linear graphs
anim = @animate for m in -4//3:1//6:10//3
plot_angle(m)
@@ -406,7 +433,7 @@ Suppose both $\alpha$ and $\beta$ are positive with $\alpha + \beta \leq \pi/2$.
```{julia}
#| echo: false
gr()
using Plots, LaTeXStrings
# two angles
@@ -425,7 +452,10 @@ color1 = :royalblue
color2 = :forestgreen
color3 = :brown3
color4 = :mediumorchid2
canvas() = plot(axis=([],false), legend=false, aspect_ratio=:equal)
canvas() = plot(;
axis=([],false),
legend=false,
aspect_ratio=:equal)
p1 = canvas()
plot!(Shape([A,B,F]), fill=(color4, 0.15))
@@ -440,29 +470,29 @@ ddf = sqrt(sum((D.-F).^2))
Δ = 0.0
alphabeta = (r*cos(α/2 + β/2), r*sin(α/2 + β/2),
text("α + β",:hcenter; rotation=pi/2))
cosαβ = (B[1]/2, 0, text("cos(α + β)", :top))
sinαβ = (B[1], F[2]/2, text("sin(α + β)"))
text(L"\alpha + \beta",:left, rotation=pi/2))
cosαβ = (B[1]/2, 0, text(L"\cos(\alpha + \beta)", :top))
sinαβ = (B[1], F[2]/2, text(L"\sin(\alpha + \beta)", rotation=90,:top))
txtpoints = (
one = (F[1]/2, F[2]/2, "1",:right),
one = (F[1]/2, F[2]/2, text(L"1", :bottom)),
beta=(r*cos(α + β/2), r*sin(α + β/2),
text(", :hcenter)),
text(L"\beta", :hcenter)),
alpha = (r*cos(α/2), r*sin(α/2),
text("α",:hcenter)),
text(L"\alpha",:hcenter)),
alphaa = (F[1] + r*sin(α/2), F[2] - r*cos(α/2) ,
text("α"),:hcenter),
text(L"\alpha"),:hcenter),
cosβ = (dae/2*cos(α),dae/2*sin(α) + Δ,
text("cos(β)",:hcenter)),
text(L"\cos(\beta)",:bottom, rotation=rad2deg(α))),
sinβ = (B[1] + dbc/2 + Δ/2, D[2] + ddf/2 + Δ/2,
text("sin(β)",:bottom)),
cosαcosβ = (C[1]/2, 0 - Δ, text("cos(α)cos(β)", :top)),
sinαcosβ = (cos(α)*cos(β) - 0.1, dce/2 ,
text("sin(α)cos(β)", :hcenter)),
text(L"\sin(\beta)",:bottom, rotation=-(90-rad2deg(α)))),
cosαcosβ = (C[1]/2, 0 - Δ, text(L"\cos(\alpha)\cos(\beta)", :top)),
sinαcosβ = (cos(α)*cos(β), dce/2 ,
text(L"\sin(\alpha)\cos(\beta)", :top, rotation=90)),
cosαsinβ = (D[1] - Δ, D[2] + ddf/2 ,
text("cos(α)sin(β)", :top)),
text(L"\cos(\alpha)\sin(\beta)", :bottom, 10, rotation=90)),
sinαsinβ = (D[1] + dde/2, D[2] + Δ ,
text("sin(α)sin(β)", :top)),
text(L"\sin(\alpha)\sin(\beta)", 10, :top)),
)
# Plot 1
@@ -574,6 +604,12 @@ $$
$$
:::
```{julia}
#| echo: false
plotly()
nothing
```
##### Example
@@ -1028,4 +1064,3 @@ Is this identical to the pattern for the regular sine function?
#| echo: false
yesnoq(false)
```

View File

@@ -1,5 +1,4 @@
# Vectors
# Vectors and containers
{{< include ../_common_code.qmd >}}
@@ -143,8 +142,9 @@ We call the values $x$ and $y$ of the vector $\vec{v} = \langle x,~ y \rangle$ t
Two operations on vectors are fundamental.
* Vectors can be multiplied by a scalar (a real number): $c\vec{v} = \langle cx,~ cy \rangle$. Geometrically this scales the vector by a factor of $\lvert c \rvert$ and switches the direction of the vector by $180$ degrees (in the $2$-dimensional case) when $c < 0$. A *unit vector* is one with magnitude $1$, and, except for the $\vec{0}$ vector, can be formed from $\vec{v}$ by dividing $\vec{v}$ by its magnitude. A vector's two parts are summarized by its direction given by a unit vector **and** its magnitude given by the norm.
* Vectors can be added: $\vec{v} + \vec{w} = \langle v_x + w_x,~ v_y + w_y \rangle$. That is, each corresponding component adds to form a new vector. Similarly for subtraction. The $\vec{0}$ vector then would be just $\langle 0,~ 0 \rangle$ and would satisfy $\vec{0} + \vec{v} = \vec{v}$ for any vector $\vec{v}$. Vector addition, $\vec{v} + \vec{w}$, is visualized by placing the tail of $\vec{w}$ at the tip of $\vec{v}$ and then considering the new vector with tail coming from $\vec{v}$ and tip coming from the position of the tip of $\vec{w}$. Subtraction is different, place both the tails of $\vec{v}$ and $\vec{w}$ at the same place and the new vector has tail at the tip of $\vec{w}$ and tip at the tip of $\vec{v}$.
* *Scalar multiplication*: Vectors can be multiplied by a scalar (a real number): $c\vec{v} = \langle cx,~ cy \rangle$. Geometrically this scales the vector by a factor of $\lvert c \rvert$ and switches the direction of the vector by $180$ degrees (in the $2$-dimensional case) when $c < 0$. A *unit vector* is one with magnitude $1$, and, except for the $\vec{0}$ vector, can be formed from $\vec{v}$ by dividing $\vec{v}$ by its magnitude. A vector's two parts are summarized by its direction given by a unit vector **and** its magnitude given by the norm.
* *Vector addition*: Vectors can be added: $\vec{v} + \vec{w} = \langle v_x + w_x,~ v_y + w_y \rangle$. That is, each corresponding component adds to form a new vector. Similarly for subtraction. The $\vec{0}$ vector then would be just $\langle 0,~ 0 \rangle$ and would satisfy $\vec{0} + \vec{v} = \vec{v}$ for any vector $\vec{v}$. Vector addition, $\vec{v} + \vec{w}$, is visualized by placing the tail of $\vec{w}$ at the tip of $\vec{v}$ and then considering the new vector with tail coming from $\vec{v}$ and tip coming from the position of the tip of $\vec{w}$. Subtraction is different, place both the tails of $\vec{v}$ and $\vec{w}$ at the same place and the new vector has tail at the tip of $\vec{w}$ and tip at the tip of $\vec{v}$.
```{julia}
@@ -334,7 +334,7 @@ Finally, to find an angle $\theta$ from a vector $\langle x,~ y\rangle$, we can
norm(v), atan(y, x) # v = [x, y]
```
## Higher dimensional vectors
### Higher dimensional vectors
Mathematically, vectors can be generalized to more than $2$ dimensions. For example, using $3$-dimensional vectors are common when modeling events happening in space, and $4$-dimensional vectors are common when modeling space and time.
@@ -395,334 +395,6 @@ Whereas, in this example where there is no common type to promote the values to,
["one", 2, 3.0, 4//1]
```
## Indexing
Getting the components out of a vector can be done in a manner similar to multiple assignment:
```{julia}
vs = [1, 2]
v₁, v₂ = vs
```
When the same number of variable names are on the left hand side of the assignment as in the container on the right, each is assigned in order.
Though this is convenient for small vectors, it is far from being so if the vector has a large number of components. However, the vector is stored in order with a first, second, third, $\dots$ component. `Julia` allows these values to be referred to by *index*. This too uses the `[]` notation, though differently. Here is how we get the second component of `vs`:
```{julia}
vs[2]
```
The last value of a vector is usually denoted by $v_n$. In `Julia`, the `length` function will return $n$, the number of items in the container. So `v[length(v)]` will refer to the last component. However, the special keyword `end` will do so as well, when put into the context of indexing. So `v[end]` is more idiomatic. (Similarly, there is a `begin` keyword that is useful when the vector is not $1$-based, as is typical but not mandatory.)
The functions `first` and `last` refer to the first and last components of a collection. An additional argument can be specified to take the first (or last) $n$ components. The function `only` will return the only component of a vector, if it has length $1$ and error otherwise.
:::{.callout-note}
## More on indexing
There is [much more](https://docs.julialang.org/en/v1/manual/arrays/#man-array-indexing) to indexing than just indexing by a single integer value. For example, the following can be used for indexing:
* a scalar integer (as seen)
* a range
* a vector of integers
* a boolean vector
:::
Some add-on packages extend this further.
### Assignment and indexing
Indexing notation can also be used with assignment, meaning it can appear on the left hand side of an equals sign. The following expression replaces the second component with a new value:
```{julia}
vs[2] = 10
```
The value of the right hand side is returned, not the value for `vs`. We can check that `vs` is then $\langle 1,~ 10 \rangle$ by showing it:
```{julia}
#| hold: true
vs = [1,2]
vs[2] = 10
vs
```
The assignment `vs[2]` is different than the initial assignment `vs=[1,2]` in that, `vs[2]=10` **modifies** the container that `vs` points to, whereas `vs=[1,2]` **replaces** any binding for `vs`. The indexed assignment is more memory efficient when vectors are large. This point is also of interest when passing vectors to functions, as a function may modify components of the vector passed to it, though can't replace the container itself.
## Some useful functions for working with vectors.
As mentioned, the `length` function returns the number of components in a vector. It is one of several useful functions for vectors.
The `sum` and `prod` function will add and multiply the elements in a vector:
```{julia}
v1 = [1,1,2,3,5,8]
sum(v1), prod(v1)
```
The `unique` function will throw out any duplicates:
```{julia}
unique(v1) # drop a `1`
```
The functions `maximum` and `minimum` will return the largest and smallest values of an appropriate vector.
```{julia}
maximum(v1)
```
(These should not be confused with `max` and `min` which give the largest or smallest value over all their arguments.)
The `extrema` function returns both the smallest and largest value of a collection:
```{julia}
extrema(v1)
```
Consider now
```{julia}
𝒗 = [1,4,2,3]
```
The `sort` function will rearrange the values in `𝒗`:
```{julia}
sort(𝒗)
```
The keyword argument, `rev=true` can be given to get values in decreasing order:
```{julia}
sort(𝒗, rev=true)
```
For adding a new element to a vector the `push!` method can be used, as in
```{julia}
push!(𝒗, 5)
```
To append more than one value, the `append!` function can be used:
```{julia}
append!(v1, [6,8,7])
```
These two functions modify or mutate the values stored within the vector `𝒗` that passed as an argument. In the `push!` example above, the value `5` is added to the vector of $4$ elements. In `Julia`, a convention is to name mutating functions with a trailing exclamation mark. (Again, these do not mutate the binding of `𝒗` to the container, but do mutate the contents of the container.) There are functions with mutating and non-mutating definitions, an example is `sort` and `sort!`.
If only a mutating function is available, like `push!`, and this is not desired a copy of the vector can be made. It is not enough to copy by assignment, as with `w = 𝒗`. As both `w` and `𝒗` will be bound to the same memory location. Rather, you call `copy` (or sometimes `deepcopy`) to make a new container with copied contents, as in `w = copy(𝒗)`.
Creating new vectors of a given size is common for programming, though not much use will be made here. There are many different functions to do so: `ones` to make a vector of ones, `zeros` to make a vector of zeros, `trues` and `falses` to make Boolean vectors of a given size, and `similar` to make a similar-sized vector (with no particular values assigned).
## Applying functions element by element to values in a vector
Functions such as `sum` or `length` are known as *reductions* as they reduce the "dimensionality" of the data: a vector is in some sense $1$-dimensional, the sum or length are $0$-dimensional numbers. Applying a reduction is straightforward it is just a regular function call.
```{julia}
#| hold: true
v = [1, 2, 3, 4]
sum(v), length(v)
```
Other desired operations with vectors act differently. Rather than reduce a collection of values using some formula, the goal is to apply some formula to *each* of the values, returning a modified vector. A simple example might be to square each element, or subtract the average value from each element. An example comes from statistics. When computing a variance, we start with data $x_1, x_2, \dots, x_n$ and along the way form the values $(x_1-\bar{x})^2, (x_2-\bar{x})^2, \dots, (x_n-\bar{x})^2$.
Such things can be done in *many* different ways. Here we describe two, but will primarily utilize the first.
### Broadcasting a function call
If we have a vector, `xs`, and a function, `f`, to apply to each value, there is a simple means to achieve this task. By adding a "dot" between the function name and the parenthesis that enclose the arguments, instructs `Julia` to "broadcast" the function call. The details allow for more flexibility, but, for this purpose, broadcasting will take each value in `xs` and apply `f` to it, returning a vector of the same size as `xs`. When more than one argument is involved, broadcasting will try to fill out different sized objects.
For example, the following will find, using `sqrt`, the square root of each value in a vector:
```{julia}
xs = [1, 1, 3, 4, 7]
sqrt.(xs)
```
This would find the sine of each number in `xs`:
```{julia}
sin.(xs)
```
For each function, the `.(` (and not `(`) after the name is the surface syntax for broadcasting.
The `^` operator is an *infix* operator. Infix operators can be broadcast, as well, by using the form `.` prior to the operator, as in:
```{julia}
xs .^ 2
```
Here is an example involving the logarithm of a set of numbers. In astronomy, a logarithm with base $100^{1/5}$ is used for star [brightness](http://tinyurl.com/ycp7k8ay). We can use broadcasting to find this value for several values at once through:
```{julia}
ys = [1/5000, 1/500, 1/50, 1/5, 5, 50]
base = (100)^(1/5)
log.(base, ys)
```
Broadcasting with multiple arguments allows for mixing of vectors and scalar values, as above, making it convenient when parameters are used.
As a final example, the task from statistics of centering and then squaring can be done with broadcasting. We go a bit further, showing how to compute the [sample variance](http://tinyurl.com/p6wa4r8) of a data set. This has the formula
$$
\frac{1}{n-1}\cdot ((x_1-\bar{x})^2 + \cdots + (x_n - \bar{x})^2).
$$
This can be computed, with broadcasting, through:
```{julia}
#| hold: true
import Statistics: mean
xs = [1, 1, 2, 3, 5, 8, 13]
n = length(xs)
(1/(n-1)) * sum(abs2.(xs .- mean(xs)))
```
This shows many of the manipulations that can be made with vectors. Rather than write `.^2`, we follow the definition of `var` and chose the possibly more performant `abs2` function which, in general, efficiently finds $|x|^2$ for various number types. The `.-` uses broadcasting to subtract a scalar (`mean(xs)`) from a vector (`xs`). Without the `.`, this would error.
:::{.callout-note}
## Note
The `map` function is very much related to broadcasting and similarly named functions are found in many different programming languages. (The "dot" broadcast is mostly limited to `Julia` and mirrors a similar usage of a dot in `MATLAB`.) For those familiar with other programming languages, using `map` may seem more natural. Its syntax is `map(f, xs)`.
:::
### Comprehensions
In mathematics, set notation is often used to describe elements in a set.
For example, the first $5$ cubed numbers can be described by:
$$
\{x^3: x \text{ in } 1, 2,\dots, 5\}
$$
Comprehension notation is similar. The above could be created in `Julia` with:
```{julia}
xs = [1,2,3,4,5]
[x^3 for x in xs]
```
Something similar can be done more succinctly:
```{julia}
xs .^ 3
```
However, comprehensions have a value when more complicated expressions are desired as they work with an expression of `xs`, and not a pre-defined or user-defined function.
Another typical example of set notation might include a condition, such as, the numbers divisible by $7$ between $1$ and $100$. Set notation might be:
$$
\{x: \text{rem}(x, 7) = 0 \text{ for } x \text{ in } 1, 2, \dots, 100\}.
$$
This would be read: "the set of $x$ such that the remainder on division by $7$ is $0$ for all x in $1, 2, \dots, 100$."
In `Julia`, a comprehension can include an `if` clause to mirror, somewhat, the math notation. For example, the above would become (using `1:100` as a means to create the numbers $1,2,\dots, 100$, as will be described in an upcoming section):
```{julia}
[x for x in 1:100 if rem(x,7) == 0]
```
Comprehensions can be a convenient means to describe a collection of numbers, especially when no function is defined, but the simplicity of the broadcast notation (just adding a judicious ".") leads to its more common use in these notes.
##### Example: creating a "T" table for creating a graph
The process of plotting a function is usually first taught by generating a "T" table: values of $x$ and corresponding values of $y$. These pairs are then plotted on a Cartesian grid and the points are connected with lines to form the graph. Generating a "T" table in `Julia` is easy: create the $x$ values, then create the $y$ values for each $x$.
To be concrete, let's generate $7$ points to plot $f(x) = x^2$ over $[-1,1]$.
The first task is to create the data. We will soon see more convenient ways to generate patterned data, but for now, we do this by hand:
```{julia}
a, b, n = -1, 1, 7
d = (b-a) // (n-1)
xs = [a, a+d, a+2d, a+3d, a+4d, a+5d, a+6d] # 7 points
```
To get the corresponding $y$ values, we can use a compression (or define a function and broadcast):
```{julia}
ys = [x^2 for x in xs]
```
Vectors can be compared together by combining them into a separate container, as follows:
```{julia}
[xs ys]
```
(If there is a space between objects they are horizontally combined. In our construction of vectors using `[]` we used a comma for vertical combination. More generally we should use a `;` for vertical concatenation.)
In the sequel, we will typically use broadcasting for this task using two steps: one to define a function the second to broadcast it.
:::{.callout-note}
## Note
The style generally employed here is to use plural variable names for a collection of values, such as the vector of $y$ values and singular names when a single value is being referred to, leading to expressions like "`x in xs`".
:::
## Other container types
We end this section with some general comments that are for those interested in a bit more, but in general aren't needed to understand most all of what follows later.
@@ -761,12 +433,16 @@ Tuples are fixed-length containers where there is no expectation or enforcement
While a vector is formed by placing comma-separated values within a `[]` pair (e.g., `[1,2,3]`), a tuple is formed by placing comma-separated values within a `()` pair. A tuple of length $1$ uses a convention of a trailing comma to distinguish it from a parenthesized expression (e.g. `(1,)` is a tuple, `(1)` is just the value `1`).
Vectors and tuples can appear at the same time: a vector of tuples--each of length $n$--can be used in plotting to specify points.
:::{.callout-note}
## Well, actually...
Technically, the tuple is formed just by the use of commas, which separate different expressions. The parentheses are typically used, as they clarify the intent and disambiguate some usage. In a notebook interface, it is useful to just use commas to separate values to output, as typically the only the last command is displayed. This usage just forms a tuple of the values and displays that.
:::
#### Named tuples
There are *named tuples* where each component has an associated name. Like a tuple these can be indexed by number and unlike regular tuples also by name.
@@ -795,12 +471,14 @@ The values in a named tuple can be accessed using the "dot" notation:
nt.x1
```
Alternatively, the index notation -- using a *symbol* for the name -- can be used:
Alternatively, the index notation--using a *symbol* for the name--can be used:
```{julia}
nt[:x1]
```
(Indexing is described a bit later, but it is a way to pull elements out of a collection.)
Named tuples are employed to pass parameters to functions. To find the slope, we could do:
```{julia}
@@ -820,11 +498,15 @@ x1 - x0
:::
### Associative arrays
### Pairs, associative arrays
Named tuples associate a name (in this case a symbol) to a value. More generally an associative array associates to each key a value, where the keys and values may be of different types.
The `pair` notation, `key => value`, is used to make one association. A *dictionary* is used to have a container of associations. For example, this constructs a simple dictionary associating a spelled out name with a numeric value:
The `pair` notation, `key => value`, is used to make one association between the first and second value.
A *dictionary* is used to have a container of associations.
This example constructs a simple dictionary associating a spelled out name with a numeric value:
```{julia}
d = Dict("one" => 1, "two" => 2, "three" => 3)
@@ -842,7 +524,449 @@ d["two"]
Named tuples are associative arrays where the keys are restricted to symbols. There are other types of associative arrays, specialized cases of the `AbstractDict` type with performance benefits for specific use cases. In these notes, dictionaries appear as output in some function calls.
Unlike vectors and tuples, dictionaries are not currently supported by broadcasting. This causes no loss in usefulness, as the values can easily be iterated over, but the convenience of the dot notation is lost.
Unlike vectors and tuples, dictionaries are not currently supported by broadcasting. (To be described in the next section.) This causes no loss in usefulness, as the values can easily be iterated over, but the convenience of the dot notation is lost.
## The container interface in Julia
There are numerous generic functions for working across the many different types of containers. Some are specific to containers which can be modified, some to associative arrays. But it is expected for different container types to implement as many as possible. We list a few here for completeness. Only a few will be used in these notes.
### Indexing
Vectors have an implied order: first element, second, last, etc. Tuples do as well. Matrices have two orders: by a row-column pair or by linear order where the first column precedes the second etc. Arrays are similar in that they have a linear order and can be accessed by their individual dimensions.
To access an element in a vector, say the second, the underlying `getindex` function is used. This is rarely typed, as the `[` notation is used. This notation is used in a style similar to a function call, the indexes go between matching pairs.
For example, we create a vector, tuple, and matrix:
```{julia}
v = [1,2,3,4]
t = (1,2,3,4)
m = [1 2; 3 4]
```
The second element of each is accessed similarly:
```{julia}
v[2], t[2], m[2]
```
(All of `v`, `t`, and `m` have $1$-based indexing.)
There is special syntax to reference the last index when used within the square braces:
```{julia}
v[end], t[end], m[end]
```
The last element is also returned by `last`:
```{julia}
last(v), last(t), last(m)
```
These use `lastindex` behind the scenes. There is also a `firstindex` which is associated with the `first` method:
```{julia}
first(v), first(t), first(m)
```
For indexing by a numeric index, a container of numbers may be used. Containers can be generated different ways, here we just use a vector to get the second and third elements:
```{julia}
I = [2,3]
v[I], t[I], m[I]
```
When indexing by a vector, the value will not be a scalar, even if there is only one element indicated.
Indexing can also be done by a mask of Boolean values with a matching length. This following mask should do the same as indexing by `I` above:
```{julia}
J = [false, true, true, false]
v[J], t[J], m[J]
```
For the matrix, values can be referenced by row/column values. The following will extract the second row, first column:
```{julia}
m[2, 1]
```
*If* a container has *only* one entry, then the `only` method will return that element (not within the container). Here we use a tuple to illustrate to emphasize the trailing comma in construction:
```{julia}
s = ("one", )
```
```{julia}
only(s)
```
There will be an error with `only` should the container not have just one element.
### Mutating values
Vectors and matrices can have their elements changed or mutated; tuples can not. The process is similar to assignment--using an equals sign--but the left hand side has indexing notation to reference which values within the container are to be updated.
To change the last element of `v` to `0` we have:
```{julia}
v[end] = 0
v
```
We might read this as assignment, but what happens is the underlying container has an element indicated by the index mutated. The `setindex!` function is called behind the scenes.
The `setindex!` function will try to promote the value (`0` above) to the element type of the container. This can throw an error if the promotion isn't possible. For example, to specify an element as `missing` with `v[end] = missing` will error, as missing can't be promoted to an integer.
If more than one value is referenced in the assignment, then more than one value can be specified on the right-hand side.
Mutation is different from reassignment. A command like `v=[1,2,3,0]` would have had the same effect as `v[end] = 0`, but would be quite different. The first *replaces* the binding to `v` with a new container, the latter reaches into the container and replaces just a value it holds.
### Size and type
The `length` of a container is the number of elements in linear order:
```{julia}
length(v), length(t), length(m)
```
The `isempty` method will indicate if the length is 0, perhaps in a performant way:
```{julia}
isempty(v), isempty([]), isempty(t), isempty(())
```
The `size` of a container, when defined, takes into account its shape or the dimensions:
```{julia}
size(v), size(m) # no size defined for tuples
```
Arrays, and hence vectors and matrices have an element type given by `eltype` (the `typeof` method returns the container type:
```{julia}
eltype(v), eltype(t), eltype(m)
```
(The element type of the tuple is `Int64`, but this is only because of this particular tuple. Tuples are typically heterogeneous containers--not homogeneous like vectors--and do not expect to have a common type. The `NTuple` type is for tuples with elements of the same type.)
### Modifying the length of a container
Vectors and some other containers allow elements to be added on or elements to be taken off. In computer science a queue is a collection that is ordered and has addition at one or the other end. Vectors can be used as a queue, though for just that task, there are more performant structures available.
Two key methods for queues are `push!` and `pop!`. We `push!` elements onto the end of the queue:
```{julia}
push!(v, 5)
```
The output is expected -- `5` was added to the end of `v`. What might not be expected is the underlying `v` is changed without assignment. (Actually `mutated`, the underlying container assigned to the symbol `v` is extended, not replaced.)
:::{.callout-note}
## Trailing exclamation point convention
The function `push!` has a trailing exclamation point which is a `Julia` convention to indicate one of the underlying arguments (traditionally the first) will be *mutated* by the function call.
:::
The `pop!` function is somewhat of a reverse: it takes the last element and "pops" it off the queue, leaving the queue one element shorter and returning the last element.
```{julia}
pop!(v)
```
```{julia}
v
```
There are also `pushfirst!`, `popfirst!`, `insert!` and `deleteat!` methods.
### Iteration
A very fundamental operation is to iterate over the elements of a collection one by one.
In computer science the `for` loop is the basic construct to iterate over values. This example will iterate over `v` and add each value to `tot` which is initialized to be `0`:
```{julia}
tot = 0
for e in v
tot = tot + e
end
tot
```
The `for` loop construct is central in many programming languages; in `Julia` for loops are very performant and very flexible, however, they are more verbose than needed. (In the above example we had to initialize an accumulator and then write three lines for the loop, whereas `sum(v)` would do the same--and in this case more flexibly, with just a single call.) Alternatives are usually leveraged--we mention a few.
Iterating over a vector can be done by *value*, as above, or by *index*. For the latter the `eachindex` method creates an iterable for the indices of the container. For rectangular objects, like matrices, there are also many uses for `eachrow` and `eachcol`, though not in these notes.
There are a few basic patterns where alternatives to a `for` loop exist. We discuss two:
* mapping a function or expression over each element in the collection
* a reduction where a larger dimensional object is summarized by a lower dimensional one. In the example above, the $1$-dimensional vector is reduced to a $0$-dimensional scalar by summing the elements.
#### Comprehensions
In mathematics, set notation is often used to describe elements in a set.
For example, the first $5$ cubed numbers can be described by:
$$
\{x^3: x \text{ in } 1, 2,\dots, 5\}
$$
Comprehension notation is similar. The above could be created in `Julia` with:
```{julia}
xs = [1, 2, 3, 4, 5]
[x^3 for x in xs]
```
Comprehensions are one way of iterating over a collection and evaluating an expression on each element.
In the above, the value `x` takes on each value in `xs`. The variables may be tuples, as well.
The `enumerate` method wraps a container (or iterable) and iterates both the index and the value. This is useful, say for polynomials:
```{julia}
x = 3
as = [1, 2, 3] # evaluate a₀⋅x⁰, a₁⋅x¹, a₂⋅x²
[a*x^(i-1) for (i, a) in enumerate(as)]
```
(These values can then be easily summed to evaluate the polynomial.)
When iterating over `enumerate` a tuple is returned. The use of `(i, a)` to iterate over these tuples destructures the tuple into parts to be used in the expression.
The `zip` function also is useful to *pair* off iterators. Redoing the above to have the powers iterated over:
```{julia}
as = [1, 2, 3]
inds = [0, 1, 2]
[a*x^i for (i, a) in zip(inds, as)]
```
Like `enumerate`, the `zip` iterator has elements which are tuples.
:::{.callout-note}
## Note
The style generally employed herein is to use plural variable names for a collection of values, such as the vector of $y$ values and singular names when a single value is being referred to, leading to expressions like "`x in xs`".
:::
#### Broadcasting a function call
If we have a vector, `xs`, and a function, `f`, to apply to each value, there is a simple means to achieve this task that is shorter than a `for` loop or the comprehension `[f(x) for x in s]`. By adding a "dot" between the function name and the parenthesis that enclose the arguments, instructs `Julia` to "broadcast" the function call. The details allow for more much flexibility, but, for this purpose, broadcasting will take each value in `xs` and apply `f` to it, returning a vector of the same size as `xs`. When more than one argument is involved, broadcasting will try to pad out different sized objects to the same shape. Broadcasting can also *fuse* combined function calls.
For example, the following will find, using `sqrt`, the square root of each value in a vector:
```{julia}
xs = [1, 1, 3, 4, 7]
sqrt.(xs)
```
This call finds the sine of each number in `xs`:
```{julia}
sin.(xs)
```
For each function call, the `.(` (and not `(`) after the name is the surface syntax for broadcasting.
The `^` operator is an *infix* operator. Infix operators can be broadcast, as well, by using the form `.` prior to the operator, as in:
```{julia}
xs .^ 2
```
Here is an example involving the logarithm of a set of numbers. In astronomy, a logarithm with base $100^{1/5}$ is used for star [brightness](http://tinyurl.com/ycp7k8ay). We can use broadcasting to find this value for several values at once through:
```{julia}
ys = [1/5000, 1/500, 1/50, 1/5, 5, 50]
base = (100)^(1/5)
log.(base, ys)
```
Broadcasting with multiple arguments allows for mixing of vectors and scalar values, as above, making it convenient when parameters are used. In broadcasting, there are times where it is desirable to treat a container as a scalar-like argument, a common idiom is to wrap that container in a 1-element tuple.
As a final example, the task from statistics of centering and then squaring can be done with broadcasting. We go a bit further, showing how to compute the [sample variance](http://tinyurl.com/p6wa4r8) of a data set. This has the formula
$$
\frac{1}{n-1}\cdot ((x_1-\bar{x})^2 + \cdots + (x_n - \bar{x})^2).
$$
This can be computed, with broadcasting, through:
```{julia}
#| hold: true
import Statistics: mean
xs = [1, 1, 2, 3, 5, 8, 13]
n = length(xs)
(1/(n-1)) * sum(abs2.(xs .- mean(xs)))
```
This shows many of the manipulations that can be made with vectors. Rather than write `.^2`, we follow the definition of `var` and chose the possibly more performant `abs2` function which, in general, efficiently finds $|x|^2$ for various number types. The `.-` uses broadcasting to subtract a scalar (`mean(xs)`) from a vector (`xs`). Without the `.`, this would error.
Broadcasting is a widely used and powerful surface syntax which we will employ occasionally in the sequel.
#### Mapping a function over a collection
The `map` function is very much related to broadcasting. Similarly named functions are found in many different programming languages. (The "dot" broadcast is mostly limited to `Julia` and mirrors a similar usage of a dot in `MATLAB`.) For those familiar with other programming languages, using `map` may seem more natural. Its syntax is `map(f, xs)`. There may be one or more iterable passed to `map`.
For example, this will map `sin` over each value in `xs`, computing the same things as `sin.(xs)`:
```{julia}
map(sin, xs)
```
The `map` function can be used with one or more iterators.
The `map` function can also be used in combination with `reduce`, a reduction. Reductions take a container with one or more dimensions and reduces the number of dimensions. A example might be:
```{julia}
sum(map(sin, xs))
```
This has a performance drawback--there are two passes through the container, one to apply `sin` another to add.
The `mapreduce` function combines the map and reduce operations in one pass. It takes a third argument to reduce by in the second position. This is a *binary* operator. So this combination will map `sin` over `xs` and then add the results up:
```{julia}
mapreduce(sin, +, xs)
```
There are other specialized reduction functions that reverse the order of the mapper and the reducer. For example, we have `sum` (used above) and `prod` for adding and multiplying values in a collection:
```{julia}
sum(xs), prod(xs)
```
These are reductions, which which fall back to a `mapreduce` call. They require a starting value (`init`) of `0` and `1` (which in this case can be determined from `xs`). The `sum` and `prod` function also allow as a first argument an initial function to map over the collection:
```{julia}
sum(sin, xs)
```
#### Other reductions
There are other reductions, which summarize a container. We mention those related to the maximum or minimum of a collection. For these examples, we have
```{julia}
v = [1, 2, 3, 4]
```
The largest value in a numeric collection is returned by `maximum`:
```{julia}
maximum(v)
```
Where this maximum occurred is returned by `argmax`:
```{julia}
argmax(v)
```
For `v` these are the same. But if we were to apply `sin` to `v` say, then the result may not be in order. This can be done with, say, a call to `map` and then `maximum`, but the functions allow an initial function to be specified:
```{julia}
maximum(sin, v), argmax(sin, v)
```
This combination is also the duty of `findmax`:
```{julia}
findmax(sin, v)
```
There are also `minimum`, `argmin`, and `findmin`.
The `extrema` function returns the maximum and minimum of the collection:
```{julia}
extrema(v)
```
:::{.callout-note}
## `maximum` and `max`
In `Julia` there are two related functions: `maximum` and `max`. The `maximum` function generically returns the largest element in a collection. The `max` function returns the maximum of its *arguments*.
That is, these return identical values:
```{julia}
xs = [1, 3, 2]
maximum(xs), max(1, 3, 2), max(xs...)
```
The latter using *splatting* to iterate over each value in `xs` and pass it to `max` as an argument.
:::
### Predicate functions
A few reductions work with *predicate* functions--those that return `true` or `false`. Let's use `iseven` as an example, which tests if a number is even.
We can check if *all* the alements of a container are even or if *any* of the elements of a container are even with `all` and `even`:
```{julia}
xs = [1, 1, 2, 3, 5]
all(iseven, xs), any(iseven, xs)
```
Related, we can count the number of `true` responses of the predicate function:
```{julia}
count(iseven, xs)
```
#### methods for associative arrays
For dictionaries, the collection is unordered (by default), but iteration can still be done over "key-value" pairs.
In `Julia` a `Pair` matches a key and a value into one entity. Pairs are made with the `=>` notation with the `key` on the left and the value on the right.
Dictionaries are a collection of pairs. The `Dict` constructor can be passed pairs directly:
```{julia}
ascii = Dict("a"=>97, "b"=>98, "c"=>99) # etc.
```
To iterate over these, the `pairs` iterator is useful:
```{julia}
collect(pairs(ascii))
```
(We used `collect` to iterate over values and return them as a vector.)
The keys are returned by `keys`, the values by `values`:
```{julia}
keys(ascii)
```
@@ -993,6 +1117,7 @@ From [transum.org](http://www.transum.org/Maths/Exam/Online_Exercise.asp?Topic=V
#| hold: true
#| echo: false
let
gr()
p = plot(xlim=(0,10), ylim=(0,5), legend=false, framestyle=:none)
for j in (-3):10
plot!(p, [j, j + 5], [0, 5*sqrt(3)], color=:blue, alpha=0.5)
@@ -1058,6 +1183,12 @@ answ = 4
radioq(choices, answ)
```
```{julia}
#| echo: false
plotly()
nothing
```
###### Question