| 1 |
\documentclass{article} |
| 2 |
|
| 3 |
\usepackage{luacas} |
| 4 |
\usepackage{amsmath} |
| 5 |
\usepackage{amssymb} |
| 6 |
|
| 7 |
\usepackage[margin=1in]{geometry} |
| 8 |
\usepackage[shortlabels]{enumitem} |
| 9 |
|
| 10 |
\usepackage{pgfplots} |
| 11 |
\pgfplotsset{compat=1.18} |
| 12 |
\usetikzlibrary{positioning,calc} |
| 13 |
\usepackage{forest} |
| 14 |
\usepackage{minted} |
| 15 |
\usemintedstyle{pastie} |
| 16 |
\usepackage[hidelinks]{hyperref} |
| 17 |
\usepackage{parskip} |
| 18 |
\usepackage{multicol} |
| 19 |
\usepackage[most]{tcolorbox} |
| 20 |
\tcbuselibrary{xparse} |
| 21 |
\usepackage{microtype} |
| 22 |
|
| 23 |
\definecolor{rose}{RGB}{128,0,0} |
| 24 |
\definecolor{roseyellow}{RGB}{222,205,99} |
| 25 |
\definecolor{roseblue}{RGB}{167,188,214} |
| 26 |
\definecolor{rosenavy}{RGB}{79,117,139} |
| 27 |
\definecolor{roseorange}{RGB}{232,119,34} |
| 28 |
\definecolor{rosegreen}{RGB}{61,68,30} |
| 29 |
\definecolor{rosewhite}{RGB}{223,209,167} |
| 30 |
\definecolor{rosebrown}{RGB}{108,87,27} |
| 31 |
\definecolor{rosegray}{RGB}{84,88,90} |
| 32 |
|
| 33 |
\usepackage[ |
| 34 |
backend=biber, |
| 35 |
style=numeric, |
| 36 |
]{biblatex} |
| 37 |
\addbibresource{sources.bib} |
| 38 |
|
| 39 |
\newtcolorbox{codebox}[1][sidebyside]{ |
| 40 |
enhanced,skin=bicolor, |
| 41 |
#1, |
| 42 |
arc=1pt, |
| 43 |
colframe=brown, |
| 44 |
colback=brown!15,colbacklower=white, |
| 45 |
boxrule=1pt, |
| 46 |
notitle |
| 47 |
} |
| 48 |
|
| 49 |
\begin{document} |
| 50 |
|
| 51 |
\subsection{Tutorial 2: Finding Maxima/Minima} |
| 52 |
|
| 53 |
Bob is teaching calculus too, and he wants to give his students many examples of the process of \emph{finding the local max/min of a given function}. But, like Alice, Bob doesn't want to work out a bunch of examples by-hand. Bob decides to try his hand with \texttt{luacas} after having been taught the basics by Alice. |
| 54 |
|
| 55 |
Bob decides to stick with polynomials for these examples; if anything because those functions are in the wheel-house of \texttt{luacas}. In particular, Bob decides that the \emph{derivative} of the function he wants to use should be a composition of quadratics. This ought to ensure that the roots of that derivative are expressible in a nice way. |
| 56 |
|
| 57 |
Accordingly, Bob declares variables and chooses two quadratic polynomials to compose, say $f$ and $g$, and sets $dh = g \circ f$: |
| 58 |
|
| 59 |
\begin{minted}{latex} |
| 60 |
\begin{CAS} |
| 61 |
vars('x') |
| 62 |
f = x^2+2*x-2 |
| 63 |
g = x^2-1 |
| 64 |
subs = {[x] = f} |
| 65 |
dh = substitute(subs,g) |
| 66 |
\end{CAS} |
| 67 |
\end{minted} |
| 68 |
\begin{CAS} |
| 69 |
vars('x') |
| 70 |
f = x^2+2*x-2 |
| 71 |
g = x^2-1 |
| 72 |
subs = {[x] = f} |
| 73 |
dh = substitute(subs,g) |
| 74 |
\end{CAS} |
| 75 |
|
| 76 |
Bob wants to compute $h$, the integral of $dh$. Bob could certainly compute this quantity by-hand, but why hardcode that information into the document when \texttt{luacas} can do this for you? So Bob uses the \texttt{int} command and shifts the result (with some malice aforethought): |
| 77 |
|
| 78 |
\begin{minted}{latex} |
| 79 |
\begin{CAS} |
| 80 |
h = int(dh,x) + 10 |
| 81 |
\end{CAS} |
| 82 |
\end{minted} |
| 83 |
\begin{CAS} |
| 84 |
h = int(dh,x) + 10 |
| 85 |
\end{CAS} |
| 86 |
|
| 87 |
Bob is curious to know the value of $h$. So he uses \mintinline{latex}{\print{h}} to produce: |
| 88 |
\begin{codebox} |
| 89 |
\begin{minted}[fontsize=\small]{latex} |
| 90 |
\[ \print{h} \] |
| 91 |
\end{minted} |
| 92 |
\tcblower |
| 93 |
\[\print{h} \] |
| 94 |
\end{codebox} |
| 95 |
This isn't exactly what Bob had in mind. It occurs to Bob that he may need to simplify the expression $h$, so he tries: |
| 96 |
|
| 97 |
\begin{codebox} |
| 98 |
\begin{minted}[fontsize=\small]{latex} |
| 99 |
\begin{CAS} |
| 100 |
h = simplify(int(dh,x)+10) |
| 101 |
\end{CAS} |
| 102 |
\[ \print{h} \] |
| 103 |
\end{minted} |
| 104 |
\tcblower |
| 105 |
\begin{CAS} |
| 106 |
h = simplify(h) |
| 107 |
\end{CAS} |
| 108 |
\[\print{h} \] |
| 109 |
\end{codebox} |
| 110 |
|
| 111 |
That's more like it! Now, Bob wants to find the roots to $dh$. Bob uses the \texttt{roots} command to do this: |
| 112 |
|
| 113 |
\begin{minted}{latex} |
| 114 |
\begin{CAS} |
| 115 |
r = roots(dh) |
| 116 |
\end{CAS} |
| 117 |
\end{minted} |
| 118 |
\begin{CAS} |
| 119 |
r = roots(dh) |
| 120 |
\end{CAS} |
| 121 |
|
| 122 |
But then Bob wonders to himself, ``How do I actually retrieve the roots of $dh$ from \texttt{luacas}?'' The assignment \mintinline{lua}{r = roots(dh)} stores the roots of the polynomial $dh$ in a table named \texttt{r}: |
| 123 |
|
| 124 |
\begin{codebox}[] |
| 125 |
\begin{minted}[fontsize=\small]{latex} |
| 126 |
\[ \print{r[1]}, \quad \print{r[2]}, \quad \print{r[3]}, \quad\print{r[4]} \] |
| 127 |
\end{minted} |
| 128 |
\tcblower |
| 129 |
\begin{CAS} |
| 130 |
r = roots(dh) |
| 131 |
\end{CAS} |
| 132 |
\[ \print{r[1]}, \quad \print{r[2]}, \quad \print{r[3]}, \quad \print{r[4]} \] |
| 133 |
\end{codebox} |
| 134 |
If Bob truly wants to print the entire list \texttt{r}, Bob can use the \mintinline{latex}{\lprint} (\textbf{l}ist \textbf{print}) command: |
| 135 |
\begin{codebox}[] |
| 136 |
\begin{minted}[fontsize=\small]{latex} |
| 137 |
\[ \left\{ \lprint{r} \right\} \] |
| 138 |
\end{minted} |
| 139 |
\tcblower |
| 140 |
\[ \left\{ \lprint{r} \right\} \] |
| 141 |
\end{codebox} |
| 142 |
Splendid! Bob would now like to evaluate the function $h$ at these roots (for these are the local max/min values of $h$). Here's Bob's first thought: |
| 143 |
|
| 144 |
\begin{codebox} |
| 145 |
\begin{minted}[fontsize=\small]{latex} |
| 146 |
\begin{CAS} |
| 147 |
v = simplify(substitute({[x]=r[1]},h)) |
| 148 |
\end{CAS} |
| 149 |
\[ \print{v} \] |
| 150 |
\end{minted} |
| 151 |
\tcblower |
| 152 |
\begin{CAS} |
| 153 |
v = simplify(substitute({[x]=r[1]},h)) |
| 154 |
\end{CAS} |
| 155 |
\[ \print{v} \] |
| 156 |
\end{codebox} |
| 157 |
|
| 158 |
What the heck?! Bob is (understandably) confused. But here's where Bob learns a valuable lesson\dots |
| 159 |
|
| 160 |
\subsubsection{A brief interlude: Lua numbers vs \texttt{luacas Integers}} |
| 161 |
|
| 162 |
The \LaTeX{} environment \mintinline{latex}{\begin{CAS}..\end{CAS}} is really a glorified Lua environment. The ``glory'' comes in how the contents of the environment are parsed in a special manner to make interacting with the CAS (mostly) easy. Bob has encountered a situation where that interaction is not as easy as we'd like. |
| 163 |
|
| 164 |
For comparison, consider the following: |
| 165 |
\begin{multicols}{2} |
| 166 |
Here's some code using the \mintinline{latex}{\begin{CAS}..\end{CAS}}: |
| 167 |
\begin{codebox}\small |
| 168 |
\begin{minted}[fontsize=\small]{latex} |
| 169 |
\begin{CAS} |
| 170 |
vars('y') |
| 171 |
a = 1 |
| 172 |
b = y+a |
| 173 |
\end{CAS} |
| 174 |
\[ \print{b} \] |
| 175 |
\end{minted} |
| 176 |
\tcblower |
| 177 |
\begin{CAS} |
| 178 |
vars('y') |
| 179 |
a = 1 |
| 180 |
b = y+a |
| 181 |
\end{CAS} |
| 182 |
\[ \print{b} \] |
| 183 |
\end{codebox} |
| 184 |
Here's that same code but using \mintinline{latex}{\directlua} instead: |
| 185 |
\begin{codebox}\small |
| 186 |
\begin{minted}[fontsize=\small]{latex} |
| 187 |
\directlua{ |
| 188 |
vars('y') |
| 189 |
a = Integer(1) |
| 190 |
b = y+a |
| 191 |
} |
| 192 |
\[ \print{b} \] |
| 193 |
\end{minted} |
| 194 |
\tcblower |
| 195 |
\directlua{ |
| 196 |
vars('y') |
| 197 |
a = Integer(1) |
| 198 |
b = y+a |
| 199 |
} |
| 200 |
\[ \print{b} \] |
| 201 |
\end{codebox} |
| 202 |
\end{multicols} |
| 203 |
The essential difference being: |
| 204 |
\begin{itemize} |
| 205 |
\item Using \mintinline{latex}{\begin{CAS}..\end{CAS}}, a parser automatically interprets any digit strings as an \texttt{Integer}; this is a special class defined within the bowels of \texttt{luacas}. Ultimately, it allows for us to define things like the addition of an \texttt{Integer} and an \texttt{Expression} (in this case, the result is a new \texttt{Expression}) as well as arbitrary precision arithmetic. |
| 206 |
\item Using \mintinline{latex}{\directlua}, there is no parsing, so the user (aka Bob) is responsible for telling \texttt{luacas} what to interpret as an \texttt{Integer} versus what to interpret as a normal Lua \texttt{number}. |
| 207 |
\end{itemize} |
| 208 |
Generally speaking, we like what the parser in \mintinline{latex}{\begin{CAS}..\end{CAS}} does: it keeps us from having to wrap all integers in \texttt{Integer(..)} (among other things). But the price we pay is that the parser indiscriminately wraps \emph{all} (or rather, most) digit strings in \texttt{Integer(..)}. This causes a problem in the following line in Bob's code: |
| 209 |
\begin{minted}{lua} |
| 210 |
v = simplify(substitute({[x]=r[1]},h)) |
| 211 |
\end{minted} |
| 212 |
The parser sees \mintinline{lua}{r[1]} and interprets \texttt{1} as \texttt{Integer(1)} -- but \mintinline{lua}{r[Integer(1)]} is \texttt{nil}, so no substitution is performed. |
| 213 |
|
| 214 |
The good news is that, excluding the annoyance between \texttt{Integer} and Lua number, interacting with the CAS via \mintinline{latex}{\directlua} is not much different than interacting with it via \mintinline{latex}{\begin{CAS}..\end{CAS}}. |
| 215 |
|
| 216 |
\subsubsection*{Back to the tutorial...} |
| 217 |
|
| 218 |
After that enlightening interlude, Bob realizes that some care needs to be taken when constructing tables. Here's a solution from within \mintinline{latex}{\begin{CAS}..\end{CAS}}: |
| 219 |
|
| 220 |
\begin{codebox}[] |
| 221 |
\begin{minted}[fontsize=\small]{latex} |
| 222 |
\begin{CAS} |
| 223 |
r = ZTable(r) |
| 224 |
v = ZTable() |
| 225 |
for i in range(1, 4) do |
| 226 |
v[i] = simplify(substitute({[x]=r[i]},h)) |
| 227 |
end |
| 228 |
\end{CAS} |
| 229 |
\[ \left\{ \lprint{v} \right\} \] |
| 230 |
\end{minted} |
| 231 |
\tcblower |
| 232 |
\begin{CAS} |
| 233 |
r = ZTable(r) |
| 234 |
v = ZTable() |
| 235 |
for i in range(1, 4) do |
| 236 |
v[i] = simplify(substitute({[x]=r[i]},h)) |
| 237 |
end |
| 238 |
\end{CAS} |
| 239 |
\[ \left\{ \lprint{v} \right\} \] |
| 240 |
\end{codebox} |
| 241 |
|
| 242 |
The function \mintinline{lua}{ZTable()} sets indices appropriately for use within \mintinline{latex}{\begin{CAS}..\end{CAS}} while the function \mintinline{lua}{range()} protects the bounds of the for-loop. Alternatively, Bob can make tables directly within \mintinline{latex}{\directlua} (or \mintinline{latex}{\luaexec} from the \texttt{luacode} package) using whatever Lua syntax pleases him: |
| 243 |
|
| 244 |
\begin{codebox}[] |
| 245 |
\begin{minted}[fontsize=\small]{latex} |
| 246 |
\directlua{ |
| 247 |
v = {} |
| 248 |
for i=1,4 do |
| 249 |
table.insert(v,simplify(substitute({[x]=r[i]},h))) |
| 250 |
end} |
| 251 |
\[ \left\{ \lprint{v} \right\} \] |
| 252 |
\end{minted} |
| 253 |
\tcblower |
| 254 |
\directlua{ |
| 255 |
v = {} |
| 256 |
for i=1,4 do |
| 257 |
table.insert(v,simplify(substitute({[x]=r[i]},h))) |
| 258 |
end |
| 259 |
} |
| 260 |
\[ \left\{ \lprint{v} \right\} \] |
| 261 |
\end{codebox} |
| 262 |
Great! But still; Bob doesn't want to just pretty-print the roots of $dh$ (or the values that $h$ takes at those roots). Bob is determined to plot the results -- he wants to hammer home the point that the roots of $dh$ point to the local extrema of $h$. |
| 263 |
|
| 264 |
Luckily, Bob is familiar with some of the fantastic graphics tools in the \LaTeX{} ecosystem, like \texttt{pgfplots} and \texttt{asymptote}. But then Bob begins to wonder, ``How can I yoink results out of \texttt{luacas} so that I may yeet them into something like \texttt{pgfplots}?'' |
| 265 |
Bob is delighted to find the following commands: \mintinline{latex}{\fetch} and \mintinline{latex}{\store}. |
| 266 |
|
| 267 |
Whereas the \mintinline{latex}{\print} command relies on the \texttt{luacas} method \mintinline{lua}{tolatex()}, the commands \mintinline{latex}{\fetch} and \mintinline{latex}{\store} rely on the \texttt{luacas} function \mintinline{lua}{tostring()}. Bob can view the output of \mintinline{lua}{tostring()} using the \mintinline{latex}{\vprint} command ({\bf v}erbatim {\bf print}). For example, \mintinline{latex}{\vprint{h}} produces: |
| 268 |
\vprint{h} |
| 269 |
This is more-or-less what Bob wants -- but he doesn't want the verbatim output printed to his document, Bob just wants the contents of \mintinline{lua}{tostring(h)}. Here's where \mintinline{latex}{\fetch} comes in. The command \mintinline{latex}{\fetch{h}} is equivalent to: |
| 270 |
\begin{minted}{latex} |
| 271 |
\directlua{ |
| 272 |
tex.print(tostring(h)) |
| 273 |
} |
| 274 |
\end{minted} |
| 275 |
For comparison, the command \mintinline{latex}{\print{h}} is equivalent to: |
| 276 |
\begin{minted}{latex} |
| 277 |
\directlua{ |
| 278 |
tex.print(h:tolatex()) |
| 279 |
} |
| 280 |
\end{minted} |
| 281 |
For Bob's purposes, \mintinline{latex}{\fetch{h}} is exactly what he needs: |
| 282 |
|
| 283 |
\begin{codebox}\small |
| 284 |
\begin{minted}[breaklines,fontsize=\small]{latex} |
| 285 |
\begin{tikzpicture}[scale=0.9] |
| 286 |
\begin{axis}[legend pos = north west] |
| 287 |
\addplot [domain=-3.5:1.5,samples=100] |
| 288 |
{\fetch{h}}; |
| 289 |
\addlegendentry{$f$}; |
| 290 |
\addplot[densely dashed] |
| 291 |
[domain=-3.25:1.25,samples=100] |
| 292 |
{\fetch{dh}}; |
| 293 |
\addlegendentry{$df/dx$}; |
| 294 |
\addplot[gray,dashed,thick] |
| 295 |
[domain=-3.5:1.5] {0}; |
| 296 |
\end{axis} |
| 297 |
\end{tikzpicture} |
| 298 |
\end{minted} |
| 299 |
\tcblower |
| 300 |
\begin{tikzpicture}[scale=0.9] |
| 301 |
\begin{axis}[legend pos = north west] |
| 302 |
\addplot [domain=-3.5:1.5,samples=100] {\fetch{h}}; |
| 303 |
\addlegendentry{$f$}; |
| 304 |
\addplot[densely dashed] |
| 305 |
[domain=-3.25:1.25,samples=100] {\fetch{dh}}; |
| 306 |
\addlegendentry{$df/dx$}; |
| 307 |
\addplot[gray,dashed,thick] |
| 308 |
[domain=-3.5:1.5] {0}; |
| 309 |
\end{axis} |
| 310 |
\end{tikzpicture} |
| 311 |
\end{codebox} |
| 312 |
Alternatively, Bob could use \mintinline{latex}{\store}. The \mintinline{latex}{\store} command will \emph{fetch} the contents of its mandatory argument and store it in a macro of the same name. |
| 313 |
\begin{minted}{latex} |
| 314 |
\store{h} |
| 315 |
\store{dh} |
| 316 |
\end{minted} |
| 317 |
Now the macros \mintinline{latex}{\h} and \mintinline{latex}{\dh} can be used in place of \mintinline{latex}{\fetch{h}} and \mintinline{latex}{\fetch{dh}}, respectively. An optional argument can be used to store contents in a macro under a different name. This is useful for situations like the following: |
| 318 |
\begin{minted}{latex} |
| 319 |
\store{r[1]}[rootone] |
| 320 |
\end{minted} |
| 321 |
Now \mintinline{latex}{\rootone} can be used in place of \mintinline{latex}{\fetch{r[1]}}. But Bob wants to fetch all the values stored in \texttt{r} (and \texttt{v}, for that matter). In this case, Bob can use: |
| 322 |
\begin{minted}{latex} |
| 323 |
\store{r} |
| 324 |
\store{v} |
| 325 |
\end{minted} |
| 326 |
The command \mintinline{latex}{\store{r}} is equivalent to: |
| 327 |
\begin{minted}{latex} |
| 328 |
\def\r{{ \fetch{r[1]}, \fetch{r[2]}, \fetch{r[3]}, \fetch{r[4]} }} |
| 329 |
\end{minted} |
| 330 |
The contents of the \LaTeX{} macro \mintinline{latex}{\r} can be accessed with \mintinline{latex}{\pgfmathsetmacro}. For example: |
| 331 |
|
| 332 |
\begin{codebox} |
| 333 |
\begin{minted}[fontsize=\small,numbersep=6pt,linenos]{latex} |
| 334 |
\begin{tikzpicture}[scale=0.6] |
| 335 |
\draw [dashed,latex-latex] |
| 336 |
(-7,0) -- (4,0); |
| 337 |
\foreach \k in {0,1,2,3}{ |
| 338 |
\pgfmathsetmacro\a{\r[\k]} |
| 339 |
\draw (\a,0) circle (\a); |
| 340 |
} |
| 341 |
\foreach \x in {-6,...,3}{ |
| 342 |
\draw[fill,orange] |
| 343 |
(\x,0) circle (2pt) |
| 344 |
node[below] {\footnotesize$\x$}; |
| 345 |
} |
| 346 |
\end{tikzpicture} |
| 347 |
\end{minted} |
| 348 |
\tcblower |
| 349 |
\store{r} |
| 350 |
\begin{center} |
| 351 |
\begin{tikzpicture}[scale=0.65] |
| 352 |
\draw [dashed,latex-latex] (-7,0) -- (4,0); |
| 353 |
\foreach \k in {0,1,2,3}{ |
| 354 |
\pgfmathsetmacro\a{\r[\k]} |
| 355 |
\draw (\a,0) circle (\a); |
| 356 |
} |
| 357 |
\foreach \x in {-6,...,3}{ |
| 358 |
\draw[fill,orange] (\x,0) circle (2pt) |
| 359 |
node[below] {\footnotesize$\x$}; |
| 360 |
} |
| 361 |
\end{tikzpicture} |
| 362 |
\end{center} |
| 363 |
\end{codebox} |
| 364 |
|
| 365 |
Alternatively, Bob could avoid the call to \mintinline{latex}{\pgfmathsetmacro} by replacing lines 5-6 in the above code with the slightly more verbose: |
| 366 |
|
| 367 |
\begin{minted}{latex} |
| 368 |
\draw ({\fetch{r[\k]}},0) circle (\fetch{r[\k]}); |
| 369 |
\end{minted} |
| 370 |
|
| 371 |
Alternatively still, Bob could appeal directly to the \mintinline{lua}{tostring()} function in \texttt{luacas} and iterate over tables like \texttt{r} using Lua itself. This can often be a simpler solution (particularly when working within \mintinline{latex}{\begin{axis}..\end{axis}}), and it is exactly what Bob does in his complete project shared below: |
| 372 |
\begin{codebox}[frame hidden,breakable] |
| 373 |
\begin{minted}[breaklines,fontsize=\small]{latex} |
| 374 |
Consider the function $f(x)$ defined by: |
| 375 |
\begin{CAS} |
| 376 |
vars('x') |
| 377 |
f = x^2+2*x-2 |
| 378 |
g = x^2-1 |
| 379 |
subs = {[x] = f} |
| 380 |
dh = expand(substitute(subs,g)) |
| 381 |
h = simplify(int(dh,x)+10) |
| 382 |
\end{CAS} |
| 383 |
$\displaystyle f(x) = \print{h}$. |
| 384 |
\begin{multicols}{2} |
| 385 |
Note that: |
| 386 |
\[ f'(x) = \print{dh}.\] |
| 387 |
The roots to $f'(x)=0$ equation are: |
| 388 |
\begin{CAS} |
| 389 |
r = roots(dh) |
| 390 |
\end{CAS} |
| 391 |
\[ \left\{ \lprint{r} \right\} \] |
| 392 |
Recall: $f'(x_0)$ measures the slope of the tangent line to $y= (x)$ at $x=x_0$. The values $r$ where $f'(r)=0$ correspond to places where the slope of the tangent line to $y=f(x)$ is horizontal (see the illustration). This gives us a method for identifying locations where the graph $y=f(x)$ attains a peak (local maximum) or a valley (local minimum). |
| 393 |
\begin{CAS} |
| 394 |
r = ZTable(r) |
| 395 |
v = ZTable() |
| 396 |
for i in range(1, 4) do |
| 397 |
v[i] = simplify(substitute({[x]=r[i]},h)) |
| 398 |
end |
| 399 |
\end{CAS} |
| 400 |
\columnbreak |
| 401 |
\store{h}\store{dh} |
| 402 |
\begin{tikzpicture}[scale=0.95] |
| 403 |
\begin{axis}[legend pos = north west] |
| 404 |
\addplot [domain=-3.5:1.5,samples=100] {\h}; |
| 405 |
\addlegendentry{$f$}; |
| 406 |
\addplot[densely dashed] [domain=-3.25:1.25,samples=100] {\dh}; |
| 407 |
\addlegendentry{$df/dx$}; |
| 408 |
\addplot[gray,dashed,thick] [domain=-3.5:1.5] {0}; |
| 409 |
\luaexec{for i=1,4 do |
| 410 |
tex.print("\\draw[fill=purple,purple]", |
| 411 |
"(axis cs:{",tostring(r[i]),"},0) circle (1.5pt)", |
| 412 |
"(axis cs:{",tostring(r[i]),"},{",tostring(v[i]),"}) circle (1.5pt)", |
| 413 |
"(axis cs:{",tostring(r[i]),"},{",tostring(v[i]),"}) edge[dashed] (axis cs:{",tostring(r[i]),"},0);") |
| 414 |
end} |
| 415 |
\end{axis} |
| 416 |
\end{tikzpicture} |
| 417 |
\end{multicols} |
| 418 |
\end{minted} |
| 419 |
\end{codebox} |
| 420 |
And here is Bob's completed project: |
| 421 |
\begin{tcolorbox}[colback=rosenavy!10, |
| 422 |
colframe=rosenavy, |
| 423 |
arc=1pt, |
| 424 |
frame hidden] |
| 425 |
{\bf Tutorial 2:} {\itshape A local max/min diagram for Bob}. |
| 426 |
\vskip 0.2cm |
| 427 |
Consider the function $f(x)$ defined by: |
| 428 |
\begin{CAS} |
| 429 |
vars('x') |
| 430 |
f = x^2+2*x-2 |
| 431 |
g = x^2-1 |
| 432 |
subs = {[x] = f} |
| 433 |
dh = expand(substitute(subs,g)) |
| 434 |
h = simplify(int(dh,x)+10) |
| 435 |
\end{CAS} |
| 436 |
$\displaystyle f(x) = \print{h}$. |
| 437 |
\begin{multicols}{2} |
| 438 |
Note that: |
| 439 |
\[ f'(x) = \print{dh}.\] |
| 440 |
The roots to $f'(x)=0$ equation are: |
| 441 |
\begin{CAS} |
| 442 |
r = roots(dh) |
| 443 |
\end{CAS} |
| 444 |
\[ \left\{ \lprint{r} \right\} \] |
| 445 |
Recall: $f'(x_0)$ measures the slope of the tangent line to $y=f(x)$ at $x=x_0$. The values $r$ where $f'(r)=0$ correspond to places where the slope of the tangent line to $y=f(x)$ is horizontal (see the illustration). This gives us a method for identifying locations where the graph $y=f(x)$ attains a peak (local maximum) or a valley (local minimum). |
| 446 |
\begin{CAS} |
| 447 |
r = ZTable(r) |
| 448 |
v = ZTable() |
| 449 |
for i in range(1, 4) do |
| 450 |
v[i] = simplify(substitute({[x]=r[i]},h)) |
| 451 |
end |
| 452 |
\end{CAS} |
| 453 |
\columnbreak |
| 454 |
\store{h}\store{dh} |
| 455 |
\begin{tikzpicture}[scale=0.95] |
| 456 |
\begin{axis}[legend pos = north west] |
| 457 |
\addplot |
| 458 |
[domain=-3.5:1.5,samples=100] {\h}; |
| 459 |
\addlegendentry{$f$}; |
| 460 |
\addplot[densely dashed] |
| 461 |
[domain=-3.25:1.25,samples=100] {\dh}; |
| 462 |
\addlegendentry{$df/dx$}; |
| 463 |
\addplot[gray,dashed,thick] |
| 464 |
[domain=-3.5:1.5] {0}; |
| 465 |
\luaexec{for i=1,4 do |
| 466 |
tex.print("\\draw[fill=purple,purple]", |
| 467 |
"(axis cs:{", tostring(r[i]) ,"},0) circle (1.5pt)", |
| 468 |
"(axis cs:{", tostring(r[i]) ,"},{", tostring(v[i]), "}) circle (1.5pt)", |
| 469 |
"(axis cs:{", tostring(r[i]) ,"},{", tostring(v[i]), "}) edge[dashed] (axis cs:{", tostring(r[i]) ,"},0);") |
| 470 |
end} |
| 471 |
\end{axis} |
| 472 |
\end{tikzpicture} |
| 473 |
\end{multicols} |
| 474 |
\end{tcolorbox} |
| 475 |
|
| 476 |
\end{document} |