From 46c01a8363a5c5724540e0522784de465f6cf463 Mon Sep 17 00:00:00 2001 From: Norbert Preining Date: Thu, 29 Nov 2007 14:19:49 +0000 Subject: bpolynomial update from CTAN git-svn-id: svn://tug.org/texlive/trunk@5644 c570f23f-e606-0410-a88d-b1316a301751 --- Master/texmf-dist/doc/metapost/bpolynomial/CHANGES | 8 +- Master/texmf-dist/doc/metapost/bpolynomial/README | 10 +- Master/texmf-dist/doc/metapost/bpolynomial/TODO | 5 + .../doc/metapost/bpolynomial/bpolynomial.pdf | Bin 467193 -> 510858 bytes .../doc/metapost/bpolynomial/bpolynomial.tex | 221 ++++++++++++++++----- .../doc/metapost/bpolynomial/examples.mp | 63 +++++- .../texmf-dist/metapost/bpolynomial/bpolynomial.mp | 27 ++- 7 files changed, 273 insertions(+), 61 deletions(-) create mode 100644 Master/texmf-dist/doc/metapost/bpolynomial/TODO (limited to 'Master/texmf-dist') diff --git a/Master/texmf-dist/doc/metapost/bpolynomial/CHANGES b/Master/texmf-dist/doc/metapost/bpolynomial/CHANGES index 892c2cb32f6..b4bbb32273c 100644 --- a/Master/texmf-dist/doc/metapost/bpolynomial/CHANGES +++ b/Master/texmf-dist/doc/metapost/bpolynomial/CHANGES @@ -1,3 +1,9 @@ +Version 0.4 released on 2007-11-28: +* New: + + The package can now compute paths of derivatives and tangents. + + Improved documentation. + + Version 0.3 released on 2007-11-26: * Changed: + Improved documentation. @@ -5,7 +11,7 @@ Version 0.3 released on 2007-11-26: Version 0.2 pre-released on 2007-11-25: -* Added: +* New: + Added documentation. * Changed: + New user-interface. diff --git a/Master/texmf-dist/doc/metapost/bpolynomial/README b/Master/texmf-dist/doc/metapost/bpolynomial/README index adbaaf3d9f5..a1715c93e44 100644 --- a/Master/texmf-dist/doc/metapost/bpolynomial/README +++ b/Master/texmf-dist/doc/metapost/bpolynomial/README @@ -2,15 +2,16 @@ % http://www.ctan.org/tex-archive/help/Catalogue/licenses.lppl.html % for the details of that license. -package: bpolynomial v0.3 2007/11/26 +package: bpolynomial v0.4 2007/11/28 author: Stephan Hennig (stephanhennig@arcor.de) Description ----------- -The MetaPost package bpolynomial.mp helps drawing polynomial functions -of up to degree three. It provides macros to calculate Bézier curves +The MetaPost package bpolynomial helps drawing polynomial functions +of up to degree three. It provides macros to calculate Bezier curves exactly matching a given constant, linear, quadratic or cubic polynomial. +Additionally, paths of derivatives and tangents can be calculated. Installation @@ -23,6 +24,9 @@ name data base. bpolynomial.pdf bpolynomial.tex examples.mp + README + CHANGES + TODO /metapost/bpolynomial: bpolynomial.mp diff --git a/Master/texmf-dist/doc/metapost/bpolynomial/TODO b/Master/texmf-dist/doc/metapost/bpolynomial/TODO new file mode 100644 index 00000000000..518aa9cd596 --- /dev/null +++ b/Master/texmf-dist/doc/metapost/bpolynomial/TODO @@ -0,0 +1,5 @@ +* Drawing square and cubic roots and tangents thereof. Note, derivatives + of roots cannot be represented by Bezier curves. +* Approximation of arbitrary polynomials by multi-segment Bezier curves. + +Contributions are welcome! Especially for the second item. diff --git a/Master/texmf-dist/doc/metapost/bpolynomial/bpolynomial.pdf b/Master/texmf-dist/doc/metapost/bpolynomial/bpolynomial.pdf index 7c9badf177d..6caa622a1a3 100644 Binary files a/Master/texmf-dist/doc/metapost/bpolynomial/bpolynomial.pdf and b/Master/texmf-dist/doc/metapost/bpolynomial/bpolynomial.pdf differ diff --git a/Master/texmf-dist/doc/metapost/bpolynomial/bpolynomial.tex b/Master/texmf-dist/doc/metapost/bpolynomial/bpolynomial.tex index 02acf9e0402..e91aee36e6f 100644 --- a/Master/texmf-dist/doc/metapost/bpolynomial/bpolynomial.tex +++ b/Master/texmf-dist/doc/metapost/bpolynomial/bpolynomial.tex @@ -13,7 +13,8 @@ \usepackage{amsmath} \usepackage{amssymb} \newcommand*{\cmd}[1]{\texttt{#1}} -\newcommand*{\pkg}{\cmd{bpolynomial.mp}} +\newcommand*{\name}[1]{\textsf{#1}} +\newcommand*{\pkg}{\name{bpolynomial}} \newcommand{\user}[1]{\emph{#1}} \newcommand*{\B}{B\'ezier} \usepackage{xcolor} @@ -35,12 +36,12 @@ \fi \begin{document} -\title{The \pkg\ package\thanks{This document describes \pkg\ v0.3, last revised 11/26/2007.}} +\title{The \pkg\ package\thanks{This document describes \pkg\ v0.4, last revised 11/28/2007.}} \author{Stephan Hennig\thanks{stephanhennig@arcor.de}} \maketitle \begin{abstract} -The MetaPost package \pkg\ helps drawing polynomial functions of up to degree three. It provides macros to calculate \B\ curves exactly matching a given constant, linear, quadratic or cubic polynomial. +The MetaPost package \pkg\ helps drawing polynomial functions of up to degree three. It provides macros to calculate \B\ curves exactly matching a given constant, linear, quadratic or cubic polynomial. Additionally, paths of derivatives and tangents can be calculated. \end{abstract} \begin{multicols}{2} @@ -51,7 +52,7 @@ The MetaPost package \pkg\ helps drawing polynomial functions of up to degree th \section{Introduction} MetaPost has a variable type \cmd{path} that can be used for drawing smooth and visualy pleasing curves. Internally, paths are \B\ curves and MetaPost is able to calculate the points along such a curve.\footnote{Since PostScript has a concept of \B\ curves, too, for MetaPost drawing a path is simply an act of copying the parameters of the corresponding \B\ curve into PostScript output. But nonetheless MetaPost \emph{can} calculate points on a \B\ curve.} -When drawing graphs, the problem users are confronted with is how to define a suitable path representing a given function $f(x)$? The \cmd{splines} package by Dan Luecking provides macros to draw smooth piece-wise \B\ curves through arbitrary sample points.~\cite{mp:splines} However, since \B\ curves are polynomials of degree three, we can do better with just one \B\ curve segment for such polynomials. This package eases the task of finding a \B\ curve matching a given polynomial +When drawing graphs, the problem users are confronted with is how to define a suitable path representing a given function $f(x)$? The \name{splines} package by Dan Luecking provides macros to draw smooth piece-wise \B\ curves through arbitrary sample points.~\cite{mp:splines} However, since \B\ curves are polynomials of degree three, we can do better with just one \B\ curve segment for such polynomials. This package eases the task of finding a \B\ curve matching a given polynomial \begin{equation} f(x) = ax^3 + bx^2 + cx + d @@ -59,8 +60,8 @@ f(x) = ax^3 + bx^2 + cx + d \section{Usage} -\subsection{Macro \cmd{newBPolynomial}} -The \pkg\ package provides just one macro \cmd{newBPolynomial}. This macro takes one suffix parameter and four numeric parameters that are the coefficients of the given polynomial. A polynomial definition for a function +\subsection{\cmd{newBPolynomial}}\label{sec:newBPolynomial} +The \pkg\ package provides just one macro \cmd{newBPolynomial}. This macro takes one suffix argumant and four numeric arguments that are the coefficients of the given polynomial. A polynomial definition for a function \begin{equation} f(x) = 2x^3 + 0x^2 - 3x - 1 \end{equation} @@ -68,25 +69,25 @@ exemplary looks like this \begin{listing} newBPolynomial.f(2, 0, -3, -1); \end{listing} -Here, suffix parameter \cmd{f} serves as an identifier where some names of macros and variables, that have to be called later, are derived from and the parameters $2$, $0$, $-3$, $-1$ match the coefficients of our function $f$. To be more precise, command +Here, numbers $2$, $0$, $-3$, $-1$ match the coefficients of our function $f$. The suffix argument \cmd{f} serves as an identifier where some names of macros and variables, that have to be called later, are derived from. To be more precise, command \begin{center} - \cmd{newBPolynomial.()} + \cmd{newBPolynomial.} \end{center} -defines two new macros +defines three new macros \begin{center} - \cmd{.getPath()} + \begin{tabular}{l} + \cmd{.getPath} \\ + \cmd{.eval} \\ + \cmd{.getTangent} \\ + \end{tabular} \end{center} -and -\begin{center} - \cmd{.eval()} -\end{center} -that do the real work. +that do the real work. These macros are described in the following sections. -\subsection{Macro \cmd{.getPath}} -Macro \cmd{.getPath(xmin, xmax)} returns a path exactly matching the polynomial defined by \cmd{newBPolynomial.} on the intervall $[xmin, xmax]$. Let's have a look at an example. Drawing our polynomial $f(x)$ on the intervall $(-2, 2)$ can be done with the following code (figure~\ref{fig:cubic}). +\subsection{\cmd{.getPath}}\suppressfloats +Macro \cmd{.getPath(xmin, xmax)} returns a path exactly matching the polynomial defined by \cmd{newBPolynomial.} on an intervall $[xmin, xmax]$. Let's have a look at an example. Drawing our polynomial $f(x)$ on the intervall $(-2, 2)$ can be done with the following code (figure~\ref{fig:cubic}). \begin{listing} -newBPolynomial.f(2, 0, -3, -1); -draw f.getPath(-2, 2) xscaled 1cm yscaled 0.1cm; + newBPolynomial.f(2, 0, -3, -1); + draw f.getPath(-2, 2) transformed T; \end{listing} \begin{figure} @@ -99,42 +100,123 @@ draw f.getPath(-2, 2) xscaled 1cm yscaled 0.1cm; \begin{minipage}[t]{.45\linewidth} \centering \includegraphics{examples.2} - \caption{With stars.} - \label{fig:starred} + \caption{With a labelled point.} + \label{fig:labelled} \end{minipage} \end{figure} +Note, since the base unit of MetaPost is a big point (1\,bp) in most cases polynomials have to be scaled to a proper size before plotting. It is \emph{not} recommended, however, to apply scaling to the polynomial coefficients, since current MetaPost versions\footnote{At the time of writing the latest release is MetaPost v1.002.} +can't handle large numbers very well. Instead, scaling should be applied to the path during the \cmd{draw} operation. In this manual, scaling is applied by an affine transform +\begin{center} + \cmd{T = identity xscaled 10mm yscaled 1mm;} +\end{center} + Once a polynomial $\langle$suffix$\rangle$ has been defined \cmd{.getPath} can be called as often as required with varying arguments and returns a path corresponding to the requested section of polynomial $\langle$suffix$\rangle$. -Note, since the \pkg\ package never uses $\langle$suffix$\rangle$ as a complete identifier, you can use that as the name of a path variable to store the path returned by \cmd{.getPath} for later drawing. Any other path (array) variable serves the same purpose, though. +\emph{Hint:} Since the \pkg\ package never uses $\langle$suffix$\rangle$ as a complete identifier, you can use that as the name of a path variable to store the path returned by \cmd{.getPath} for later drawing. Any other path (array) variable serves the same purpose, though. \begin{listing} -newBPolynomial.f(2, 0, -3, -1); -path f; -f := f.getPath(-2, 2); -draw f xscaled 1cm yscaled 0.1cm; + newBPolynomial.f(2, 0, -3, -1); + path f; + f := f.getPath(-2, 2); + draw f transformed T; \end{listing} -\subsection{Macro \cmd{.eval}} -The other macro defined by \cmd{newBPolynomial.}, macro \cmd{.eval}, can be used to evaluate polynomial $\langle$suffix$\rangle$ at a given x-coordinate. This macro takes one parameter---the x-coordinate. A ``starred'' version of our polynomial can be plotted with the following code (figure~\ref{fig:starred}). +\subsection{\cmd{.eval}} +Macro \cmd{.eval} can be used to evaluate polynomial $\langle$suffix$\rangle$ at a given x-coordinate. The macro takes one argument---the x-coordinate. Labelling an arbitrary point on a polynomial can be done as follows (figure~\ref{fig:labelled}). \begin{listing} -newBPolynomial.f(2, 0, -3, -1); -for x=-2 step .25 until 2: - label(btex $\star$ etex, (x, f.eval(x)) xscaled 1cm yscaled 0.1cm); -endfor + newBPolynomial.f(2, 0, -3, -1); + draw f.getPath(-2, 2) transformed T; + x := 1.5; + show (x, f.eval(x)); + dotlabeldiam := 2bp; + dotlabel.ulft(btex $(1.5, 1.25)$ etex, (x, f.eval(x)) transformed T); +\end{listing} + +Note, the label has been provided explicitly in this example (after reading the coordinates off the \cmd{log} file). It is also possible to attach the correct coordinates automatically with the help of the MetaPost package \name{LaTeXMP} and the \LaTeX\ package \name{numprint}. While the former helps passing dynamically generated text from MetaPost to \LaTeX, the latter can be used to format and round numbers.~\cite{latex:numprint, mp:latexmp}. + +\subsection{\cmd{.getTangent}} +Macro \cmd{.getTangent} returns a path tangent to polynomial $\langle$suffix$\rangle$. This macro takes three numeric arguments, the coordinate $x$ where the tangent should touch polynomial $\langle$suffix$\rangle$, and two values $\epsilon_-$, $\epsilon_+$ that specify the range the tangent is drawn in. These arguments are not the range boundaries, but the neighbourhood around $x$. The range is $[x+\epsilon_-,x+\epsilon_+]$. This syntax has been choosen to make is easier to move a tangent along a polynomial, keeping the neighbourhood fixed. + +As an example, the following code draws a tangent that touches $f$ at $x=-1$ with a neighbourhood $\epsilon = \pm 1$ (figure~\ref{fig:tangent}). +\begin{listing} + newBPolynomial.f(2, 0, -3, -1); + draw f.getPath(-2, 2) transformed T; + draw f.getTangent(-1, -1, 1) transformed T; +\end{listing} + +\subsection{Dealing with derivatives} +Additionally to drawing polynomials, the \pkg\ package supports drawing derivatives of polynomials and tangents thereof. In section~\ref{sec:newBPolynomial} it was said macro \cmd{newBPolynomial} defines three new macros. But this is not the full story. In fact, the command +\begin{center} + \cmd{newBPolynomial.} +\end{center} +defines twelve macros, three of them we already know, \cmd{.getPath}, \cmd{.eval}, \cmd{.getTangent}. The remaining nine macros are similar, but correspond to the first, second and third derivative of a polynomial $\langle$suffix$\rangle$, resp. To access these macros just add the required number of prime characters to the suffix name (three at maximum). For instance, to get the path corresponding to the first derivative of polynomial $\langle$suffix$\rangle$ call +\begin{center} + \cmd{'.getPath} +\end{center} +and to get a tangent on the first derivative call +\begin{center} + \cmd{'.getTangent}. +\end{center} + +In total these are the macros defined by \cmd{newBPolynomial.}: +\begin{multicols}{2} + \centering + \begin{tabular}{l} + \cmd{.getPath} \\ + \cmd{.eval} \\ + \cmd{.getTangent} \\ + \cmd{'.getPath} \\ + \cmd{'.eval} \\ + \cmd{'.getTangent} \\ + \end{tabular}\columnbreak + + \begin{tabular}{l} + \cmd{''.getPath} \\ + \cmd{''.eval} \\ + \cmd{''.getTangent} \\ + \cmd{'''.getPath} \\ + \cmd{'''.eval} \\ + \cmd{'''.getTangent} \\ + \end{tabular} +\end{multicols} + +As an example, the following code draws a tangent on the first derivative of a polynomial \cmd{f} (figure~\ref{fig:derivative}). + +\begin{figure} + \begin{minipage}[t]{.45\linewidth} + \centering + \includegraphics{examples.3} + \caption{Cubic polynomial with a tangent.} + \label{fig:tangent} + \end{minipage}\hfill% + \begin{minipage}[t]{.45\linewidth} + \centering + \includegraphics{examples.4} + \caption{First derivative with a tangent.} + \label{fig:derivative} + \end{minipage} +\end{figure} + +\begin{listing} + newBPolynomial.f(2, 0, -3, -1); + draw f'.getPath(-2, 2) transformed T; + draw f'.getTangent(1, -1, 1) transformed T; \end{listing} \subsection{Accessing polynomial coefficients} -Additionally, macro \cmd{newBPolynomial.} saves the coefficients passed as arguments in variables \cmd{.a}, \cmd{.b}, \cmd{.c} and \cmd{.d} for later reference. +The coefficients passed to \cmd{newBPolynomial.} are saved in variables \cmd{.a}, \cmd{.b}, \cmd{.c} and \cmd{.d} and can be accessed by the user. \section{Examples} -In the first example a simple corrdinate system is drawn manually. Then a quadratic polynomial \cmd{f} is drawn in three strokes. Two dahsed strokes correspond to the positive values of \cmd{f}, a dotted stroke corresponds to negative values. Finally, a cubic polynomials \cmd{g} is plotted and a table of points is written to the console and log file (figure~\ref{fig:coordinatesystem}). +This section contains some more elaborate examples. The code of all examples can also be found in file \cmd{examples.mp}. + +In the first example a simple coordinate system is drawn manually. Then a quadratic polynomial \cmd{f} is drawn in three strokes. Two dashed strokes correspond to the positive values of \cmd{f}, a dotted stroke corresponds to negative values. Finally, a cubic polynomial \cmd{g} is plotted and a table of points is written to the console and log file (figure~\ref{fig:multistrokes}). \begin{figure} \centering - \includegraphics{examples.3} + \includegraphics{examples.5} \caption{Two polynomials in a coordinate system.} - \label{fig:coordinatesystem} + \label{fig:multistrokes} \end{figure} \begin{listing} @@ -170,20 +252,21 @@ path g; endfor \end{listing} -Note command \cmd{show g} that writes path~\cmd{g} to the \cmd{log} file. Inspecting that we can easily verify, that \cmd{g} consists of just one path segment: +Note command \cmd{show g} that writes path~\cmd{g} to the \cmd{log} file. Inspecting that we can easily verify \cmd{g} consists of just one path segment: \begingroup\small \begin{verbatim} (-3.5,-3.36273)..controls (-1,16.70013) and (1.5,-22.30025)..(4,6.2002) \end{verbatim} \endgroup -The next example demonstrates how \pkg\ and John Hobby's \cmd{graph} package\cite{mp:graph} can be used together to draw polynomials in a coordinate system. Instead of \cmd{draw} paths have just to be drawn with a \cmd{gdraw} command. The latter macro additionally clips paths to the boundaries of the coordinate system (figure~\ref{fig:bpolynomialgraph}). + +The next example demonstrates how \pkg\ and John Hobby's \name{graph} package\cite{mp:graph} can be used together to draw polynomials in a coordinate system. Instead of \cmd{draw} paths have just to be drawn with a \cmd{gdraw} command. The latter macro additionally clips paths to the boundaries of the coordinate system (figure~\ref{fig:graph.mp}). \begin{figure} \centering - \includegraphics{examples.4} + \includegraphics{examples.6} \caption{Packages \pkg\ and \cmd{graph} interacting.} - \label{fig:bpolynomialgraph} + \label{fig:graph.mp} \end{figure} \begin{listing} @@ -196,8 +279,7 @@ path f,g; g := g.getPath(xmin, xmax); draw begingraph(10cm, 6cm); setrange(xmin,ymin, xmax,ymax); - autogrid(grid.bot, grid.lft) - dashed withdots withpen pencircle scaled .7bp withcolor .5white; + autogrid(grid.bot, grid.lft) dashed evenly withcolor .9white; drawoptions(withpen pencircle scaled 1bp); gdraw f dashed evenly scaled 2; gdraw g; @@ -205,18 +287,62 @@ path f,g; endgraph; \end{listing} -The code of all examples can also be found in file \cmd{examples.mp}. + +In the last example a cubic polynomial $f$ is drawn together with its derivatives $f'$, $f''$ and $f'''$. Additionally, for all four functions the tangents are drawn at $x=2$. Admittedly, the plot is a little bit crowded. But it should only serve as an example (figure~\ref{fig:derivatives}). + +\begin{figure} + \centering + \includegraphics{examples.7} + \caption{A cubic polynomial with derivatives and tangents.} + \label{fig:derivatives} +\end{figure} + +\begin{listing} + xmin := -6; xmax := 6; + ymin := -6; ymax := 6; + newBPolynomial.f(0.3, -0.5, -0.5, -1); + draw begingraph(10cm, 6cm); + setrange(xmin,ymin, xmax,ymax); + autogrid(grid.bot, grid.lft) dashed evenly withcolor .9white; + drawoptions(withpen pencircle scaled 1bp); + %%% Draw f and its derivatives f', f'', f'''. + gdraw f.getPath(xmin, xmax); + gdraw f'.getPath(xmin, xmax) dashed evenly scaled 2; + gdraw f''.getPath(xmin, xmax) dashed withdots + withpen pencircle scaled 2bp; + gdraw f'''.getPath(-5, 5) withcolor .6white; + %%% Draw tangents and mark points. + x := 2; + drawoptions(withcolor red+.6(green+blue)); + gdraw f.getTangent(x, -2, 2); + gdraw f'.getTangent(x, -1, 1); + gdraw f''.getTangent(x, -2, 2); + gdraw f'''.getTangent(x, -2, 2); + drawoptions(withcolor blue+.6(red+green)); + dotlabeldiam := 2.5bp; + gdotlabel("", (x, f.eval(x))); + gdotlabel("", (x, f'.eval(x))); + gdotlabel("", (x, f''.eval(x))); + gdotlabel("", (x, f'''.eval(x))); + drawoptions(); + endgraph; +\end{listing} \section{Mathematics} -A \B\ curve $P(t)$ with end points $A=(x_A,y_A)$ and $D=(x_D,y_D)$ and control points $B=(x_B,y_B)$ and $C=(x_C,y_C)$ is defined as +A \B\ curve $P(t)$ with end points $A=(x_A,y_A)$ and $D=(x_D,y_D)$ and control points $B=(x_B,y_B)$ and $C=(x_C,y_C)$ is defined as~\cite{mfbook} \begin{equation} P(t) = \left( \begin{array}{@{}c@{}} x\\ y\\ \end{array} - \right)(t) = A + 3(B-A)t + 3(C-2B+A)t^2 + (D-3C+3B-A)t^3,\quad 0\leq t\leq 1. + \right)(t) = (1-t)^3A + 3(1-t)^2tB + 3(1-t)t^2C + t^3D,\quad 0\leq t\leq 1. +\end{equation} + +This equation can be rewritten as +\begin{equation} +P(t) = A + 3(B-A)t + 3(C-2B+A)t^2 + (D-3C+3B-A)t^3,\quad 0\leq t\leq 1. \end{equation} An arbitrary function $y=f(x)$ can be written in parameter form as @@ -235,7 +361,7 @@ An arbitrary function $y=f(x)$ can be written in parameter form as \end{equation} with parameter $t$. -For a function +For a polynomial function \begin{equation} f(x) = ax^3 + bx^2 + cx + d,\quad x\in [x_0, x_1] \end{equation} @@ -304,8 +430,11 @@ Equations~\ref{eq:yA} to~\ref{eq:yC} of the original equation system and the new \begin{thebibliography}{999} +\bibitem{latex:numprint} \textsc{Harders}, Harald, \emph{The numprint package}, 2007, \url{CTAN:macros/latex/contrib/numprint/numprint.pdf} \bibitem{mp:graph} \textsc{Hobby}, John~D., \emph{Drawing graphs with MetaPost}, \url{http://www.tug.org/docs/metapost/mpgraph.pdf} +\bibitem{mfbook} \textsc{Knuth}, Donald~E., \emph{The METAFONTbook}, Addison-Wesley, Reading, Massachusetts, 1986, (Computers \& Typesetting, C) \bibitem{mp:splines} \textsc{Luecking}, Dan, \emph{Macros to compute splines}, 2005, \url{CTAN:graphics/metapost/contrib/macros/splines/splines.pdf} +\bibitem{mp:latexmp} \textsc{Morawski}, Jens-Uwe, \emph{latexMP}, 2005, \url{CTAN:graphics/metapost/contrib/macros/latexmp/doc/latexmp.pdf} \end{thebibliography} \end{document} diff --git a/Master/texmf-dist/doc/metapost/bpolynomial/examples.mp b/Master/texmf-dist/doc/metapost/bpolynomial/examples.mp index 135b7881fdc..1d32a2f8f0b 100644 --- a/Master/texmf-dist/doc/metapost/bpolynomial/examples.mp +++ b/Master/texmf-dist/doc/metapost/bpolynomial/examples.mp @@ -2,22 +2,40 @@ input bpolynomial; input graph prologues := 3; +transform T; +T = identity xscaled 10mm yscaled 1mm; beginfig(1); newBPolynomial.f(2, 0, -3, -1); - draw f.getPath(-2, 2) xscaled 1cm yscaled 0.1cm; + draw f.getPath(-2, 2) transformed T; endfig; beginfig(2); newBPolynomial.f(2, 0, -3, -1); - for x=-2 step .25 until 2: - label(btex $\star$ etex, (x, f.eval(x)) xscaled 1cm yscaled 0.1cm); - endfor + draw f.getPath(-2, 2) transformed T; + x := 1.5; + show (x, f.eval(x)); + dotlabeldiam := 2bp; + dotlabel.ulft(btex $(1.5, 1.25)$ etex, (x, f.eval(x)) transformed T); endfig; beginfig(3); + newBPolynomial.f(2, 0, -3, -1); + draw f.getPath(-2, 2) transformed T; + draw f.getTangent(-1, -1, 1) transformed T; +endfig; + + +beginfig(4); + newBPolynomial.f(2, 0, -3, -1); + draw f'.getPath(-2, 2) transformed T; + draw f'.getTangent(1, -1, 1) transformed T; +endfig; + + +beginfig(5); numeric u; u := 0.5cm; %%% Draw a coordinate system. @@ -51,7 +69,7 @@ path g; endfig; -beginfig(4); +beginfig(6); path f,g; xmin := -7; xmax := 7; ymin := -7; ymax := 7; @@ -61,8 +79,7 @@ path f,g; g := g.getPath(xmin, xmax); draw begingraph(10cm, 6cm); setrange(xmin,ymin, xmax,ymax); - autogrid(grid.bot, grid.lft) - dashed withdots withpen pencircle scaled .7bp withcolor .5white; + autogrid(grid.bot, grid.lft) dashed evenly withcolor .9white; drawoptions(withpen pencircle scaled 1bp); gdraw f dashed evenly scaled 2; gdraw g; @@ -70,4 +87,36 @@ path f,g; endgraph; endfig; + +beginfig(7); + xmin := -6; xmax := 6; + ymin := -6; ymax := 6; + newBPolynomial.f(0.3, -0.5, -0.5, -1); + draw begingraph(10cm, 6cm); + setrange(xmin,ymin, xmax,ymax); + autogrid(grid.bot, grid.lft) dashed evenly withcolor .9white; + drawoptions(withpen pencircle scaled 1bp); + %%% Draw f and its derivatives f', f'', f'''. + gdraw f.getPath(xmin, xmax); + gdraw f'.getPath(xmin, xmax) dashed evenly scaled 2; + gdraw f''.getPath(xmin, xmax) dashed withdots + withpen pencircle scaled 2bp; + gdraw f'''.getPath(-5, 5) withcolor .6white; + %%% Draw tangents and mark points. + x := 2; + drawoptions(withcolor red+.6(green+blue)); + gdraw f.getTangent(x, -2, 2); + gdraw f'.getTangent(x, -1, 1); + gdraw f''.getTangent(x, -2, 2); + gdraw f'''.getTangent(x, -2, 2); + drawoptions(withcolor blue+.6(red+green)); + dotlabeldiam := 2.5bp; + gdotlabel("", (x, f.eval(x))); + gdotlabel("", (x, f'.eval(x))); + gdotlabel("", (x, f''.eval(x))); + gdotlabel("", (x, f'''.eval(x))); + drawoptions(); + endgraph; +endfig; + end diff --git a/Master/texmf-dist/metapost/bpolynomial/bpolynomial.mp b/Master/texmf-dist/metapost/bpolynomial/bpolynomial.mp index 6840bb04038..f1c0129bb94 100644 --- a/Master/texmf-dist/metapost/bpolynomial/bpolynomial.mp +++ b/Master/texmf-dist/metapost/bpolynomial/bpolynomial.mp @@ -9,15 +9,21 @@ if known bpolynomial_fileversion: endinput fi; string bpolynomial_fileversion; -bpolynomial_fileversion := "v0.3 (2007/11/26)"; +bpolynomial_fileversion := "v0.4 (2007/11/28)"; message "Loading bpolynomial " & bpolynomial_fileversion; +vardef newBPolynomial@#(expr a, b, c, d)= + defineBPolynomial.@#(a, b, c, d); + defineBPolynomial.@#'(0, 3a, 2b, c); + defineBPolynomial.@#''(0, 0, 6a, 2b); + defineBPolynomial.@#'''(0, 0, 0, 6a); +enddef; + %%% This macro defines two macros @#.eval and @#.getPath. %%% Parameters are the coefficients of the polynomial a*x^3 + b*x^2 + c*x + d. -vardef newBPolynomial@#(expr ca,cb,cc,cd)= +vardef defineBPolynomial@#(expr ca,cb,cc,cd)= numeric @#.a, @#.b, @#.c, @#.d; - %%% Save coefficients for later access. %%% For instance, variable @#.a refers to coefficient a of polynomial @#. @#.a := ca; @@ -25,12 +31,14 @@ numeric @#.a, @#.b, @#.c, @#.d; @#.c := cc; @#.d := cd; - %%% Define a macro that returns polynomial values. + + %%% Define macro that returns values of polynomial @#. %%% Parameter is an x value. vardef @#.eval(expr x)= (((@#.a*x + @#.b)*x + @#.c)*x + @#.d) enddef; + %%% Define a macro that returns a path of the polynomial %%% on a given intervall [xl, xr]. vardef @#.getPath(expr xl,xr)= @@ -63,4 +71,15 @@ numeric @#.a, @#.b, @#.c, @#.d; (xA,yA)..controls (xB,yB) and (xC,yC)..(xD,yD) enddef; + + %%% Define macro that returns a path tangent to @# at point (x, f(x)) + %%% covering interval (x+xm, x+xp). + vardef @#.getTangent(expr x, xm, xp)= + save m, y; + numeric m, y; + m := (3@#.a*x + 2@#.b)*x + @#.c; + y := @#.eval(x); + (x+xm, y + m*xm) -- (x+xp, y + m*xp) + enddef; + enddef; -- cgit v1.2.3