| 1 |
% !TEX program = pdfLaTeX --shell-escape |
| 2 |
\documentclass[a4paper]{article} |
| 3 |
\usepackage{tikz,multicol,bezierplot,amsmath,cancel} |
| 4 |
\usepackage[margin=3.5cm,top=1.75cm]{geometry} |
| 5 |
\usepackage{fetamont} |
| 6 |
\title{bezierplot}\author{Linus Romer} |
| 7 |
\DeclareDocumentCommand{\graphcomparison}{ m m }{ |
| 8 |
\begin{center} |
| 9 |
\begin{tikzpicture}[scale=.4] |
| 10 |
\draw (0,-5) node[below]{\tiny\texttt{\detokenize{#1}\quad | \detokenize{#2}}}; |
| 11 |
\draw[step=1,thin] (-5,-5) grid (5,5); |
| 12 |
\draw[thick,->] (-5,0) -- (5.5,0) node[below]{$x$}; |
| 13 |
\draw[thick,->] (0,-5) -- (0,5.5) node[left]{$y$}; |
| 14 |
\foreach \x in {-4,-3,-2,-1,1,2,3,4} {\draw (\x,1pt) -- (\x,-1pt) node[below]{\tiny \x};} |
| 15 |
\foreach \y in {-4,-3,-2,-1,1,2,3,4} {\draw (1pt,\y) -- (-1pt,\y) node[left]{\tiny \y};} |
| 16 |
\draw[color=red,domain=-5:5,range=-5:5,samples=1000] plot function{#2}; |
| 17 |
\draw \bezierplot{#1}; |
| 18 |
\end{tikzpicture} |
| 19 |
\end{center} |
| 20 |
} |
| 21 |
\begin{document} |
| 22 |
\maketitle\noindent |
| 23 |
\section{Introduction} |
| 24 |
\texttt{bezierplot} is a Lua program as well as a (Lua)\LaTeX{} package. This document describes both. |
| 25 |
|
| 26 |
Given a smooth function, \texttt{bezierplot} returns a smooth bezier path written in Ti\emph{k}Z notation (which also matches \MP{}) that approximates the graph of the function. For polynomial functions of degree $\leq 3$ and inverses of them, the approximation is exact (up to numeric precision). \texttt{bezierplot} finds special graph points such as extreme points and inflection points and reduces the number of used points. |
| 27 |
|
| 28 |
The following example will show a comparison of \textsc{gnuplot} with \verb|bezierplot| for the function $y=\sqrt{x}$ for $0\leq x \leq 5$: |
| 29 |
\begin{center} |
| 30 |
\begin{tikzpicture}[scale=1.4] |
| 31 |
\draw (0,0) .. controls (0,0.7454) and (1.6667,1.4907) .. (5,2.2361); |
| 32 |
\draw (0,0) circle(.02) -- (0,0.745) circle( .02); |
| 33 |
\draw (1.6667,1.4907) circle(.02) -- (5,2.2361) circle( .02); |
| 34 |
\draw (2.5,.5) node[above]{\verb|bezierplot|}; |
| 35 |
\begin{scope}[shift={(5.2,0)}] |
| 36 |
\draw[domain=0:5,samples=51] plot function{x**0.5}; |
| 37 |
\foreach \x in {0,0.1,...,5.05} {\draw (\x,{\x^0.5}) circle (0.02);} |
| 38 |
\draw (2.5,.5) node[above]{\textsc{gnuplot}}; |
| 39 |
\end{scope} |
| 40 |
\end{tikzpicture} |
| 41 |
\end{center} |
| 42 |
\textsc{gnuplot} used 51 samples (no smoothing) and is still quite inexact at the beginning, whereas \verb|bezierplot| uses 4 points only and is exact (up to numeric precision)! |
| 43 |
\section{Installation} |
| 44 |
As \texttt{bezierplot} is written in Lua, the installation depends whether you are using Lua\LaTeX{} or another \LaTeX{} engine. |
| 45 |
\subsection{Installation For Lua\LaTeX{}} |
| 46 |
If you have installed \texttt{bezierplot} by a package manager, the installation is already complete. The manual installation of \texttt{bezierplot} is done in 2 steps: |
| 47 |
\begin{itemize} |
| 48 |
\item copy the files \texttt{bezierplot.lua} and \texttt{bezierplot.sty} somewhere in your \texttt{texmf} tree (e.g. to \verb|~/texmf/tex/lualatex/bezierplot/bezierplot.sty| and\\ |
| 49 |
\verb|~/texmf/scripts/bezierplot/bezierplot.lua|) |
| 50 |
\item update the ls-R databases by running \texttt{mktexlsr} |
| 51 |
\end{itemize} |
| 52 |
\subsection{Additional Installation Steps For Other \LaTeX{} Engines} |
| 53 |
You will have to call \texttt{bezierplot} as an external program via the option \texttt{--shell-escape} (\texttt{--write18} for MiK\TeX{}). Therefore, \texttt{bezierplot.lua} has to be copied with the name \texttt{bezierplot} to a place, where your OS can find it. Under Linux this usually means copying to the directory \texttt{/usr/local/bin/}, but for Windows this will probably include more steps (like adding to the \texttt{PATH}). Of course, Lua has to be installed as well. As soon as you can call \texttt{bezierplot} from a command line (e.g. by typing \verb|bezierplot "x^2"|), it should also work with other \LaTeX{} engines. |
| 54 |
\section{Loading} |
| 55 |
The \texttt{bezierplot} package is loaded with \verb|\usepackage{bezierplot}|. There are no loading options for the package. |
| 56 |
\section{Usage} |
| 57 |
\begin{multicols}{2} |
| 58 |
\noindent A minimal example of Lua\LaTeX{} document could be: |
| 59 |
\begin{verbatim} |
| 60 |
\documentclass{article} |
| 61 |
\usepackage{tikz,bezierplot} |
| 62 |
\begin{document} |
| 63 |
\tikz \draw \bezierplot{x^2}; |
| 64 |
\end{document} |
| 65 |
\end{verbatim} |
| 66 |
\begin{center} |
| 67 |
\tikz \draw[scale=.7] \bezierplot{x^2}; |
| 68 |
\end{center} |
| 69 |
\end{multicols} |
| 70 |
\noindent |
| 71 |
The command \verb|\bezierplot| has 6 optional arguments in the sense of |
| 72 |
\begin{center} |
| 73 |
\verb|\bezierplot[XMIN][XMAX][YMIN][YMAX][SAMPLES]{FUNCTION}| |
| 74 |
\end{center} |
| 75 |
The defaults are \verb|XMIN| = \verb|YMIN| $= -5$, \verb|XMAX| = \verb|YMAX| $= 5$ and \verb|SAMPLES| $= 0$ (this will set as few samples as possible). |
| 76 |
\begin{center} |
| 77 |
\begin{tikzpicture}[scale=.7] |
| 78 |
\draw \bezierplot[-1][2]{x^2}; |
| 79 |
\draw (0,0) node[below]{\verb|\bezierplot[-1][2]{x^2}|}; |
| 80 |
\begin{scope}[shift={(10,0)}] |
| 81 |
\draw \bezierplot[-1][2][0.5][3]{x^2}; |
| 82 |
\draw (0,0) node[below]{\verb|\bezierplot[-1][2][0.5][3]{x^2}|}; |
| 83 |
\end{scope} |
| 84 |
\end{tikzpicture} |
| 85 |
\end{center} |
| 86 |
You may reverse the graph by making \verb|XMIN| bigger than \verb|XMAX|. E.g. |
| 87 |
\begin{verbatim} |
| 88 |
\bezierplot[-5][5]{0.5*x+1} |
| 89 |
\end{verbatim} |
| 90 |
returns \verb|(-5,-1.5) -- (5,3.5)|, whereas |
| 91 |
\begin{verbatim} |
| 92 |
\bezierplot[5][-5]{0.5*x+1} |
| 93 |
\end{verbatim} |
| 94 |
returns the reversed path \verb|(5,3.5) -- (-5,-1.5)|. This is useful, if you want to cycle a path to a closed area: |
| 95 |
\begin{multicols}{2} |
| 96 |
\begin{verbatim} |
| 97 |
\begin{tikzpicture} |
| 98 |
\fill[black!30] \bezierplot[-1][1]{2-x^2} |
| 99 |
-- \bezierplot[1][-1]{x^3-x} -- cycle; |
| 100 |
\draw \bezierplot[-1.1][1.1]{2-x^2}; |
| 101 |
\draw \bezierplot[-1.1][1.1]{x^3-x}; |
| 102 |
\end{tikzpicture} |
| 103 |
\end{verbatim} |
| 104 |
\begin{center} |
| 105 |
\begin{tikzpicture} |
| 106 |
\fill[black!30] \bezierplot[-1][1]{2-x^2} -- \bezierplot[1][-1]{x^3-x} -- cycle; |
| 107 |
\draw \bezierplot[-1.1][1.1]{2-x^2}; |
| 108 |
\draw \bezierplot[-1.1][1.1]{x^3-x}; |
| 109 |
\end{tikzpicture} |
| 110 |
\end{center} |
| 111 |
\end{multicols} |
| 112 |
\subsection{Running Raw \texttt{bezierplot}} |
| 113 |
Of course, you can run \verb|bezierplot.lua| in a terminal without using \LaTeX{}, e.g. |
| 114 |
\begin{verbatim} |
| 115 |
lua bezierplot.lua "3*x^0.8+2" |
| 116 |
\end{verbatim} |
| 117 |
will return |
| 118 |
\begin{verbatim} |
| 119 |
(0,2) .. controls (0.03,2.282) and (0.268,3.244) .. (1,5) |
| 120 |
\end{verbatim} |
| 121 |
You can set the window of the graph and the number of samples as follows: |
| 122 |
\begin{verbatim} |
| 123 |
lua bezierplot.lua "FUNCTION" XMIN XMAX YMIN YMAX SAMPLES |
| 124 |
\end{verbatim} |
| 125 |
e.g. |
| 126 |
\begin{verbatim} |
| 127 |
lua bezierplot.lua "FUNCTION" 0 1 -3 2.5 201 |
| 128 |
\end{verbatim} |
| 129 |
will set $0\leq x\leq 1$ and $-3\leq y\leq 2.5$ and $201$ equidistant samples. You may also omit the $y$--range, hence |
| 130 |
\begin{verbatim} |
| 131 |
lua bezierplot.lua "FUNCTION" 0 1 |
| 132 |
\end{verbatim} |
| 133 |
will set $0\leq x\leq 1$ and leave the default $-5\leq y\leq 5$. The variables \verb|XMIN|, \verb|XMAX|, \verb|YMIN| and \verb|YMAX| may also be computable expressions like \verb|2*pi+6|: |
| 134 |
\begin{verbatim} |
| 135 |
lua bezierplot.lua "sin(x)" -pi pi |
| 136 |
\end{verbatim} |
| 137 |
\subsection{Notation Of Functions} |
| 138 |
The function term given to \verb|bezierplot| must contain at most one variable: $x$. E.g. \verb|"2.3*(x-1)^2-3"|. You must not omit \verb|*| operators: |
| 139 |
\begin{center} |
| 140 |
wrong:\quad $\cancel{\texttt{2x(x+1)}}$ \hfil correct:\quad \texttt{2*x*(x+1)} |
| 141 |
\end{center} |
| 142 |
You have two possibilities to write powers: \verb|"x^2"| and \verb|"x**2"| both mean $x^2$. |
| 143 |
|
| 144 |
\medskip |
| 145 |
|
| 146 |
The following functions and constants are possible: |
| 147 |
\begin{center} |
| 148 |
\begin{tabular}{ll} |
| 149 |
\verb|abs| & absolute value (remember: your function should still be smooth)\\ |
| 150 |
\verb|acos| & $\cos^{-1}$ inverse function of cosine in radians\\ |
| 151 |
\verb|asin| & $\sin^{-1}$ inverse function of sine in radians\\ |
| 152 |
\verb|atan| & $\tan^{-1}$ inverse function of tangent in radians\\ |
| 153 |
\verb|cbrt| & cube root $\sqrt[3]{\quad}$ that works for negative numbers, too\\ |
| 154 |
\verb|cos| & cosine for angles in radians\\ |
| 155 |
\verb|cosh| & hyperbolic cosine\\ |
| 156 |
\verb|deg| & converts from radians to degrees\\ |
| 157 |
\verb|exp| & the exponential function $e^{(\;)}$\\ |
| 158 |
\verb|huge| & the numerical $\infty$\\ |
| 159 |
\verb|e| & the euler constant $e=\mathrm{exp}(1)$\\ |
| 160 |
\verb|log| & the natural logarithm $\mathrm{log}_e(\;)$\\ |
| 161 |
\verb|pi| & Archimedes’ constant $\pi\approx 3.14$\\ |
| 162 |
\verb|rad| & converts from degrees to radians\\ |
| 163 |
\verb|sgn| & sign function\\ |
| 164 |
\verb|sin| & sine for angles in radians\\ |
| 165 |
\verb|sinh| & hyperbolic sine\\ |
| 166 |
\verb|sqrt| & square root $\sqrt{\quad}$\\ |
| 167 |
\verb|tan| & tangent for angles in radians\\ |
| 168 |
\verb|tanh| & hyperbolic tangent |
| 169 |
\end{tabular} |
| 170 |
\end{center} |
| 171 |
% |
| 172 |
\newpage |
| 173 |
% |
| 174 |
\section{Examples of \texttt{bezierplot} in Comparison with \textsc{gnuplot}} |
| 175 |
The following graphs are drawn with \texttt{bezierplot} (black) and \textsc{gnuplot} (red). You may not recognize the red behind the black unless you zoom in. \textsc{gnuplot} used 1000 samples per example. The functions are given below the pictures (left: bezierplot, right: \textsc{gnuplot}). |
| 176 |
\begin{multicols}{3} |
| 177 |
\graphcomparison{0.32*x-0.7}{0.32*x-0.7} |
| 178 |
\graphcomparison{-x^2+4}{-x**2+4} |
| 179 |
\graphcomparison{(x+1)*x*(x-1)}{(x+1)*x*(x-1)} |
| 180 |
\graphcomparison{x^0.5}{x**0.5} |
| 181 |
%\graphcomparison{x^(1/3)}{x**(1/3.)} |
| 182 |
\graphcomparison{cbrt(x)}{sgn(x)*abs(x)**(1/3.)} |
| 183 |
\graphcomparison{x^3*(x-1)}{x**3*(x-1)} |
| 184 |
\graphcomparison{2*cos(3*x+4)+3}{2*cos(3*x+4)+3} |
| 185 |
\graphcomparison{tan(x)}{tan(x)} |
| 186 |
\graphcomparison{x+0.5*sin(x)}{x+0.5*sin(x)} |
| 187 |
%\graphcomparison{1/(x-2)+1}{1/(x-2)+1} |
| 188 |
\graphcomparison{2*x^2/(3*x-3)}{2*x**2/(3*x-3)} |
| 189 |
\graphcomparison{4-exp(x)}{4-exp(x)} |
| 190 |
\graphcomparison{log(x+4)}{log(x+4)} |
| 191 |
\end{multicols} |
| 192 |
|
| 193 |
\end{document} |