use plotly; fix bitrot

This commit is contained in:
jverzani
2024-06-07 13:07:09 -04:00
parent 0bcc8b5a6c
commit 55f37a6dfb
59 changed files with 423 additions and 176 deletions

View File

@@ -762,6 +762,30 @@ numericq(val)
###### Question
You are asked to cook chicken is an unfamiliar kitchen. Your recipe says to turn the oven to 200 Celsius, but you the oven is calibrated in Fahrenheit. Which value is closest?
```{julia}
#| echo: false
choices = [350, 375, 400, 425, 450]
v = 9/5*200 + 32
_, answer = findmin(abs.(choices .- v))
radioq(choices, answer; keep_order=true)
```
The recipe says cook until the internal temperature is 165 degrees Fahrenheit. However, the food thermometer in the kitchen is calibrated in Celsius. Which value is closest?
```{julia}
#| echo: false
choices = [68,70,72,74,76,78,80]
v = 5/9*(165 - 32)
_, answer = findmin(abs.(choices .- v))
radioq(choices, answer; keep_order=true)
```
(Use $F = 9/5\cdot C + 32$ and $C = 5/9(F-32)$.)
###### Question
Which of the following is a valid `Julia` expression for

View File

@@ -8,7 +8,7 @@ This section uses the following add-on packages:
```{julia}
using CalculusWithJulia
using Plots
using Plots; plotly()
```

View File

@@ -8,6 +8,7 @@ This section will use the following add-on packages:
```{julia}
using CalculusWithJulia, Plots
plotly()
```

View File

@@ -9,6 +9,7 @@ In this section we will use these add-on packages:
```{julia}
using CalculusWithJulia
using Plots
plotly()
```
---

View File

@@ -518,6 +518,7 @@ We must load `Plots` before we can plot (and it must be installed before we can
```{julia}
using Plots
plotly() # optinally change the backend from the default
```
With `Plots` loaded, we can plot a function by passing the function object by name to `plot`, specifying the range of `x` values to show, as follows:

View File

@@ -14,6 +14,7 @@ using CalculusWithJulia # loads the `SpecialFunctions` package
#| echo: false
#| results: "hidden"
using Plots
plotly()
nothing
```

View File

@@ -9,6 +9,7 @@ This section will use the following packages:
```{julia}
using CalculusWithJulia
using Plots
plotly()
```
```{julia}
@@ -74,8 +75,13 @@ using Plots
:::
The `plotly` backend is part of the `Plots` package, as is `gr`. Other backends require installation, such as `PyPlot` and `PlotlyJS`. We use `gr` in these notes, for the most part. (The `plotly` backend is also quite nice for interactive usage, but doesn't work as well with the static HTML pages.)
Some backends require installation, such as `PyPlot` and `PlotlyJS`. We use `plotly` in these notes, for the most part, which is not the default, so requires an additional command to set the backend:
```{julia}
plotly()
```
(Certain graphics are produced with the `gr()` backend.)
With `Plots` loaded, it is straightforward to graph a function.

View File

@@ -9,6 +9,7 @@ In this section we use the following add-on packages:
```{julia}
using SymPy
using Plots
plotly()
```
```{julia}
@@ -43,7 +44,7 @@ $$
#| cache: true
##{{{ different_poly_graph }}}
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))
@@ -53,6 +54,7 @@ imgfile = tempname() * ".gif"
gif(anim, imgfile, fps = 1)
caption = "Polynomials of varying even degrees over ``[-1,1]``."
plotly()
ImageFile(imgfile, caption)
```
@@ -87,7 +89,7 @@ $$
#| echo: false
#| cache: true
### {{{ lines_m_graph }}}
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))
@@ -96,7 +98,7 @@ end
imgfile = tempname() * ".gif"
gif(anim, imgfile, fps = 1)
caption = "Graphs of y = mx for different values of m"
plotly()
ImageFile(imgfile, caption)
```
@@ -172,17 +174,17 @@ To illustrate, using the just defined `x`, here is how we can create the polynom
```{julia}
𝒑 = -16x^2 + 100
p = -16x^2 + 100
```
That is, the expression is created just as you would create it within a function body. But here the result is still a symbolic object. We have assigned this expression to a variable `p`, and have not defined it as a function `p(x)`. Mentally keeping the distinction between symbolic expressions and functions is very important.
The `typeof` function shows that `𝒑` is of a symbolic type (`Sym`):
The `typeof` function shows that `p` is of a symbolic type (`Sym`):
```{julia}
typeof(𝒑)
typeof(p)
```
We can mix and match symbolic objects. This command creates an arbitrary quadratic polynomial:
@@ -230,28 +232,28 @@ To illustrate, to do the task above for the polynomial $-16x^2 + 100$ we could h
```{julia}
𝒑(x => (x-1)^2)
p(x => (x-1)^2)
```
This "call" notation takes pairs (designated by `a=>b`) where the left-hand side is the variable to substitute for, and the right-hand side the new value. The value to substitute can depend on the variable, as illustrated; be a different variable; or be a numeric value, such as $2$:
```{julia}
𝒚 = 𝒑(x=>2)
y = p(x=>2)
```
The result will always be of a symbolic type, even if the answer is just a number:
```{julia}
typeof(𝒚)
typeof(y)
```
If there is just one free variable in an expression, the pair notation can be dropped:
```{julia}
𝒑(4) # substitutes x=>4
p(4) # substitutes x=>4
```
##### Example
@@ -311,8 +313,8 @@ Evaluating a symbolic expression and returning a numeric value can be done by co
```{julia}
𝐩 = 200 - 16x^2
N(𝐩(2))
p = 200 - 16x^2
N(p(2))
```
This approach is direct, but can be slow *if* many such evaluations were needed (such as with a plot). An alternative is to turn the symbolic expression into a `Julia` function and then evaluate that as usual.
@@ -323,7 +325,7 @@ The `lambdify` function turns a symbolic expression into a `Julia` function
```{julia}
#| hold: true
pp = lambdify(𝐩)
pp = lambdify(p)
pp(2)
```
@@ -356,7 +358,7 @@ Consider the graph of the polynomial `x^5 - x + 1`:
plot(x^5 - x + 1, -3/2, 3/2)
```
(Plotting symbolic expressions is similar to plotting a function, in that the expression is passed in as the first argument. The expression must have only one free variable, as above, or an error will occur.)
(Plotting symbolic expressions with `Plots` is similar to plotting a function, in that the expression is passed in as the first argument. The expression must have only one free variable, as above, or an error will occur. This happens, as there is a `Plots` "recipe" for `SymPy` defined.)
This graph illustrates the key features of polynomial graphs:
@@ -375,7 +377,7 @@ To investigate this last point, let's consider the case of the monomial $x^n$. W
#| echo: false
#| cache: true
### {{{ poly_growth_graph }}}
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]")
@@ -384,7 +386,7 @@ end
imgfile = tempname() * ".gif"
gif(anim, imgfile, fps = 1)
caption = L"Demonstration that $x^{10}$ grows faster than $x^8$, ... and $x^2$ grows faster than $x^0$ (which is constant)."
plotly()
ImageFile(imgfile, caption)
```
@@ -412,7 +414,7 @@ To see the roots and/or the peaks and valleys of a polynomial requires a judicio
#| echo: false
#| cache: true
### {{{ leading_term_graph }}}
gr()
anim = @animate for n in 1:6
m = [1, .5, -1, -5, -20, -25]
M = [2, 4, 5, 10, 25, 30]
@@ -427,7 +429,7 @@ end
caption = "The previous graph is highlighted in red. Ultimately the leading term (\$x^4\$ here) dominates the graph."
imgfile = tempname() * ".gif"
gif(anim, imgfile, fps=1)
plotly()
ImageFile(imgfile, caption)
```

View File

@@ -9,6 +9,7 @@ In this section we use the following add on packages:
```{julia}
using CalculusWithJulia
using Plots
plotly()
using SymPy
```

View File

@@ -9,6 +9,7 @@ This section will use the following add-on packages:
```{julia}
import CalculusWithJulia
using Plots
plotly()
using Polynomials
using RealPolynomialRoots
import SymPy # imported only: some functions, e.g. degree, need qualification

View File

@@ -10,6 +10,7 @@ This section uses the following add-on packages:
using CalculusWithJulia
using SymPy
using Plots
plotly()
import Polynomials
using RealPolynomialRoots
```

View File

@@ -9,6 +9,7 @@ In this section we will use these add-on packages:
```{julia}
using CalculusWithJulia
using Plots
plotly()
```
```{julia}

View File

@@ -9,6 +9,7 @@ This section uses the following add-on packages:
```{julia}
using CalculusWithJulia
using Plots
plotly()
using SymPy
```
@@ -70,6 +71,7 @@ These definitions in terms of sides only apply for $0 \leq \theta \leq \pi/2$. M
#| cache: true
## {{{radian_to_trig}}}
gr()
function plot_angle(m)
r = m*pi
@@ -107,6 +109,7 @@ imgfile = tempname() * ".gif"
gif(anim, imgfile, fps = 1)
caption = "An angle in radian measure corresponds to a point on the unit circle, whose coordinates define the sine and cosine of the angle. That is ``(x,y) = (\\cos(\\theta), \\sin(\\theta))``."
plotly()
ImageFile(imgfile, caption)
```
@@ -293,7 +296,8 @@ The superposition of the two sine functions that `g2` represents could be viewed
#| hold: true
#| echo: false
#| cache: true
unzip(vs::Vector) = Tuple([[vs[i][j] for i in eachindex(vs)] for j in eachindex(vs[1])])
gr()
function makegraph(t, b₁,n₁, b₂=0, n₂=1)
f₁ = x -> b₁*[sin(2pi*n₁*x), cos(2pi*n₁*x)]
@@ -350,7 +354,7 @@ imgfile = tempname() * ".gif"
gif(anim, imgfile, fps = 5)
caption = "Superposition of sines and cosines represented by an epicycle"
plotly()
ImageFile(imgfile, caption)
```

View File

@@ -9,6 +9,7 @@
#| results: "hidden"
using CalculusWithJulia
using Plots
plotly()
using Measures
using LaTeXStrings
@@ -21,6 +22,8 @@ One of the first models learned in physics are the equations governing the laws
```{julia}
#| hold: true
#| echo: false
gr()
px = 0.26mm
x0 = [0, 64]
v0 = [20, 0]
@@ -45,7 +48,7 @@ function make_plot(t)
xs, ys = [p[1] for p in xys], [p[2] for p in xys]
plt = Plots.plot(xs, ys, legend=false, size=fig_size, xlims=(0,45), ylims=(0,70))
plt = plot(xs, ys, legend=false, size=fig_size, xlims=(0,45), ylims=(0,70))
plot!(plt, zero, extrema(xs)...)
arrow!(xn(t), 10*unit(xn(t)), color="black")
arrow!(xn(t), 10*unit(vn(t)), color="red")
@@ -74,6 +77,7 @@ end
gif(anim, imgfile, fps = 1)
plotly()
ImageFile(imgfile, caption)
```
@@ -111,6 +115,8 @@ The magnitude of a vector comes from the distance formula applied to a line segm
#| hold: true
#| echo: false
## generic vector
gr()
p0 = [0,0]
a1 = [4,1]
b1 = [-2,2]
@@ -126,6 +132,7 @@ png(plt, imgfile)
caption = "A vector and its unit vector. They share the same direction, but the unit vector has a standardized magnitude."
plotly()
ImageFile(imgfile, caption)
```
@@ -143,13 +150,13 @@ Two operations on vectors are fundamental.
#| hold: true
#| echo: false
## vector_addition_image
gr()
p0 = [0,0]
a1 = [4,1]
b1 = [-2,2]
plt = Plots.plot(legend=false, size=fig_size)
plt = plot(legend=false, size=fig_size)
arrow!(p0, a1, color="blue")
arrow!(p0+a1, b1, color="red")
arrow!(p0, a1+b1, color="black")
@@ -160,6 +167,7 @@ png(plt, imgfile)
caption = "The sum of two vectors can be visualized by placing the tail of one at the tip of the other"
plotly()
ImageFile(imgfile, caption)
```
@@ -167,7 +175,7 @@ ImageFile(imgfile, caption)
#| hold: true
#| echo: false
## vector_subtraction_image
gr()
p0 = [0,0]
a1 = [-2,2]
b1 = [4,1]
@@ -183,7 +191,7 @@ imgfile = tempname() * ".png"
png(plt, imgfile)
caption = "The difference of two vectors can be visualized by placing the tail of one at the tip of the other"
plotly()
ImageFile(imgfile, caption)
```
@@ -194,7 +202,7 @@ The concept of scalar multiplication and addition, allow the decomposition of ve
#| hold: true
#| echo: false
### {{{vector_decomp}}}
gr()
p0 = [0,0]
aa = [1,2]
bb = [2,1]
@@ -224,7 +232,7 @@ can be written uniquely as
provided ``\vec{a}`` and ``\vec{b}`` are not parallel.
"""
plotly()
ImageFile(imgfile, caption)
```
@@ -248,6 +256,7 @@ If we are given $r$ and $\theta$, then the vector is $v = \langle r \cdot \cos(\
#| hold: true
#| echo: false
## vector_rtheta
gr()
p0 = [0,0]
plt = plot(legend=false, size=fig_size)
@@ -266,6 +275,7 @@ A vector ``\langle x, y \rangle`` can be written as ``\langle r\cdot
``\theta``. The value ``r`` is a magnitude, the direction parameterized by
``\theta``."""
plotly()
ImageFile(imgfile, caption)
```