Debian Codegraph @

texmf-dist/doc/lualatex/luacas/tutorial/tut1/tut1.tex

raw
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 1: Limit Definition of the Derivative}
52
53 Alice is teaching calculus, and she wants to give her students many examples of the dreaded \emph{limit definition of the derivative}. On the other hand, she'd like to avoid working out many examples by-hand. She decides to give \texttt{luacas} a try.
54
55 Alice can access the \texttt{luacas} program using a custom environment: \mintinline{latex}{\begin{CAS}..\end{CAS}}. The first thing Alice must do is declare variables that will be used going forward:
56 \begin{minted}{latex}
57 \begin{CAS}
58 vars('x','h')
59 \end{CAS}
60 \end{minted}
61 Alice decides that $f$, the function to be differentiated, should be $x^2$. So Alice makes this assignment with:
62 \begin{minted}{latex}
63 \begin{CAS}
64 vars('x','h')
65 f = x^2
66 \end{CAS}
67 \end{minted}
68 Now, Alice wants to use the variable $q$ to store the appropriate \emph{difference quotient} of $f$. Alice could hardcode this into $q$, but that seems to defeat the oft sought after goal of reusable code. So Alice decides to use the \texttt{substitute} command of \texttt{luacas}:
69 \begin{minted}{latex}
70 \begin{CAS}
71 vars('x','h')
72 f = x^2
73 subs = {[x]=x+h}
74 q = (substitute(subs,f) - f)/h
75 \end{CAS}
76 \end{minted}
77 Alice is curious to know if $q$ is what she thinks it is. So Alice decides to have \LaTeX{} print out the contents of $q$ within her document. For this, she uses the \mintinline{latex}{\print} command.
78 \begin{CAS}
79 vars('x','h')
80 f = x^2
81 subs = {[x]=x+h}
82 q = (substitute(subs,f)- f)/h
83 \end{CAS}
84 \begin{codebox}
85 \begin{minted}[fontsize=\small]{latex}
86 \[ \print{q} \]
87 \end{minted}
88 \tcblower
89 \[ \print{q} \]
90 \end{codebox}
91 So far so good! Alice wants to expand the numerator of $q$; she finds the aptly named \texttt{expand} method helpful in this regard. Alice redefines \mintinline{lua}{q} to be \mintinline{lua}{q=expand(q)}, and prints the result to see if things worked as expected:
92 \begin{codebox}
93 \begin{minted}[fontsize=\small]{latex}
94 \begin{CAS}
95 vars('x','h')
96 f = x^2
97 subs = {[x]=x+h}
98 q = (substitute(subs,f)-f)/h
99 q = expand(q)
100 \end{CAS}
101 \[ \print{q} \]
102 \end{minted}
103 \tcblower
104 \begin{CAS}
105 q = expand(q)
106 \end{CAS}
107 \[ \print*{q} \]
108 \end{codebox}
109 Alice is pleasantly surprised that the result of the expansion has been \emph{simplified}, i.e., the factors of $x^2$ and $-x^2$ cancelled each other out, and the resulting extra factor of $h$ has been cancelled out of the numerator and denominator.
110
111 Finally, Alice wants to take the limit as $h\to 0$. Now that our difference quotient has been expanded and simplified, this amounts to another substitution:
112 \begin{codebox}
113 \begin{minted}[fontsize=\small]{latex}
114 \begin{CAS}
115 vars('x','h')
116 f = x^2
117 subs = {[x]=x+h}
118 q = (substitute(subs,f)-f)/h
119 q = expand(q)
120 subs = {[h] = 0}
121 q = substitute(subs,q)
122 \end{CAS}
123 \[ \print{q} \]
124 \end{minted}
125 \tcblower
126 \begin{CAS}
127 subs = {[h]=0}
128 q = q:substitute(subs)
129 \end{CAS}
130 \[ \print{q} \]
131 \end{codebox}
132 Alice is slightly disappointed that $0+2x$ is returned and not $2x$. Alice takes a guess that there's a \mintinline{lua}{simplify} command. This does the trick: adding the line \mintinline{lua}{q = simplify(q)} before leaving the \texttt{CAS} environment returns the expected $2x$:
133 \begin{codebox}
134 \begin{minted}[fontsize=\small]{latex}
135 \begin{CAS}
136 vars('x','h')
137 f = x^2
138 subs = {[x]=x+h}
139 q = (substitute(subs,f)-f)/h
140 q = expand(q)
141 subs = {[h] = 0}
142 q = substitute(subs,q)
143 q = simplify(q)
144 \end{CAS}
145 \[ \print{q} \]
146 \end{minted}
147 \tcblower
148 \begin{CAS}
149 q=simplify(q)
150 \end{CAS}
151 \[ \print{q} \]
152 \end{codebox}
153
154 Alternatively, Alice could have used the \mintinline{latex}{\print*} command instead of \mintinline{latex}{\print} -- the essential difference is that \mintinline{latex}{\print*}, unlike \mintinline{latex}{\print}, automatically simplifies the content of the argument.
155
156 Alice is pretty happy with how everything is working, but she wants to be able to typeset the individual steps of this process. Alice is therefore thrilled to learn that the \mintinline{latex}{\begin{CAS}..\end{CAS}} environment is very robust -- it can:
157 \begin{itemize}
158 \item Be entered into and exited out of essentially anywhere within her \LaTeX{} document, for example, within \mintinline{latex}{\begin{aligned}..\end{aligned}}; and
159 \item CAS variables persist -- if Alice assigns \mintinline{lua}{f = x^2} within \mintinline{latex}{\begin{CAS}..\end{CAS}}, then the CAS remembers that \mintinline{lua}{f = x^2} the next time Alice enters the CAS environment.
160 \end{itemize}
161 Here's Alice's completed code:
162 \begin{codebox}[frame hidden,breakable]
163 \begin{minted}[breaklines,fontsize=\small]{latex}
164 \begin{CAS}
165 vars('x','h')
166 f = x^2
167 \end{CAS}
168 Let $f(x) = \print{f}$. We wish to compute the derivative of $f(x)$ at $x$ using thelimit definition of the derivative. Toward that end, we start with the appopriatedifference quotient:
169 \begin{CAS}
170 subs = {[x]=x+h}
171 q = (substitute(subs,f) - f)/h
172 \end{CAS}
173 \[ \begin{aligned}
174 \print{q} &=
175 \begin{CAS}
176 q = expand(q)
177 \end{CAS}
178 \print{q}& &\text{expand/simplify} \\
179 \begin{CAS}
180 subs = {[h]=0}
181 q = substitute(subs,q)
182 \end{CAS}
183 &\xrightarrow{h\to 0} \print{q}& &\text{take limit}\\
184 &=
185 \begin{CAS}
186 q = simplify(q)
187 \end{CAS}
188 \print{q} & &\text{simplify.}
189 \end{aligned} \]
190 So $\print{diff(f,x)} = \print*{diff(f,x)}$.
191 \end{minted}
192 \end{codebox}
193
194 Alice can produce another example merely by changing the definition of $f$ on the third line to another polynomial:
195
196 \begin{minted}{latex}
197 \begin{CAS}
198 vars('x','h')
199 f = 2*x^3-x
200 \end{CAS}
201 \end{minted}
202 And here is Alice's completed project:
203 \begin{tcolorbox}[colback=rose!10,
204 colframe=rose,
205 arc=1pt,
206 frame hidden]
207 {\bf Tutorial 1:} {\itshape A limit definition of the derivative for Alice.}\vskip0.2cm
208
209 \begin{CAS}
210 vars('x','h')
211 f = 2*x^3-x
212 \end{CAS}
213 Let $f(x) = \print{f}$. We wish to compute the derivative of $f(x)$ at $x$ using the limit definition of the derivative. Toward that end, we start with the appropriate difference quotient:
214 \begin{CAS}
215 subs = {[x] = x+h}
216 q = (f:substitute(subs) - f)/h
217 \end{CAS}
218 \[ \begin{aligned}
219 \print{q} &=
220 \begin{CAS}
221 q = expand(q)
222 \end{CAS}
223 \print{q}& &\text{expand/simplify} \\
224 \begin{CAS}
225 subs = {[h]=0}
226 q = q:substitute(subs)
227 \end{CAS}
228 &\xrightarrow{h\to 0} \print{q}& &\text{take limit} \\
229 &=
230 \begin{CAS}
231 q = simplify(q)
232 \end{CAS}
233 \print{q}& &\text{simplify.}
234 \end{aligned} \]
235 %So $\print{diff(f,x)} = \print*{diff(f,x)}$.
236 \end{tcolorbox}
237
238 \end{document}