Vous êtes sur la page 1sur 4

Parameterized plots | TikZ example

http://www.texample.net/tikz/examples/parameterized-plots/

About Contact

ample resources for TeX users TikZ Community Weblog Examples Resources Builds Questions Home > TikZ > Examples > All > Parameterized plots

Example: Parameterized plots


Published 2009-09-13 | Author: Yves Delhaye As a math teacher, I have to explain how parameters affect the graph of a function. By putting the tikzpicture inside a LaTeX macro, it is very easy and fast to create multiple graphs by modifying only the parameters and calling the macro. Download as: [PDF] [TEX]

Navigation
Gallery main page About the gallery Contribute Show all examples Subscribe to the TikZ examples RSS feed There are currently four comments on this entry.

Features
Foreach76 Mathematical engine31 Plotting17

Tags
Annotations7 Axes9 GNUPLOT9 Macros10 Plots28

Scientific and technical areas


Mathematics57

% under Creative Commons attribution license. % A work by Yves Delhaye % Requires GNUPLOT and shell-escape enabled \documentclass{minimal} \usepackage{tikz} \usetikzlibrary{arrows,shapes} \usepackage{xifthen} \begin{document} % Macros for cst. They have to \newcommand{\cA}{1}% Cste . \newcommand{\cB}{0}% Cste + \newcommand{\cC}{1}% Cste . \newcommand{\cD}{0}% Cste + be redefined each time. See inside document fct fct var var

%LaTeX Macro for drawing fct with pgf/tikz. Define once, use many! \newcommand{\FctAss}{ \begin{tikzpicture}[domain=0:8] \pgfmathparse{0.1+\cA*1.1 +\cB} \pgfmathresult \let\maxY\pgfmathresult% evaluate maxY

1 de 4

19/04/2012 17:12

Parameterized plots | TikZ example

http://www.texample.net/tikz/examples/parameterized-plots/

\pgfmathparse{-0.1-\cA*1.1 +\cB} \pgfmathresult \let\minY\pgfmathresult% evaluate minY \pgfmathparse{\maxY < 1} \pgfmathresult \let\BmaxY\pgfmathresult% What if maxY < 1? Then set Boolean to 1 \ifthenelse{\equal{\BmaxY}{1.0}}{% \pgfmathparse{1.2} \pgfmathresult \let\maxY\pgfmathresult% Correct maxY to have correct graph }{} \pgfmathparse{\minY > 0} \pgfmathresult \let\BminY\pgfmathresult% What if minY > 0? Then set Boolean to 1 \ifthenelse{\equal{\BminY}{1.0}}{% \pgfmathparse{0} \pgfmathresult \let\minY\pgfmathresult% Correct minY to have correct graph % }{} DRAW the graph of the function from here on \draw[very thin,color=gray] (-0.1,\minY) grid (7.9,\maxY);% GRID use minY & maxY

\draw[->] (-0.2,0) -- (8.2,0) node[right] {$x$}; \draw[->] (0,\minY) -- (0,\maxY) node[above] {$f(x)$};% y axis use minY & maxY too \draw[smooth,samples=200,color=blue] plot function{(\cA)* (cos((\cC)*x+(\cD))) + \cB} node[right] {$f(x) = \cA{} . cos(\cC{} . x + \cD{}) + \cB{}$}; % units for cartesian reference frame \foreach \x in {0,1} \draw (\x cm,1pt) -- (\x cm,-3pt) node[anchor=north,xshift=-0.15cm] {$\x$}; \foreach \y/\ytext in {1} \draw (1pt,\y cm) -- (-3pt,\y cm) node[anchor=east] {$\ytext$}; \end{tikzpicture} } % END of macro % And now use it! \FctAss{} % Change the parameters \renewcommand{\cA}{3} \renewcommand{\cB}{0} \renewcommand{\cC}{1} \renewcommand{\cD}{0} % WITHOUT rewriting the code for the picture \FctAss{} % % And do it again \renewcommand{\cA}{1} \renewcommand{\cB}{0} \renewcommand{\cC}{4} \renewcommand{\cD}{0} \FctAss{} % And again \renewcommand{\cA}{1} \renewcommand{\cB}{0.5} \renewcommand{\cC}{1} \renewcommand{\cD}{0} \FctAss{} % And again \renewcommand{\cA}{1} \renewcommand{\cB}{0} \renewcommand{\cC}{1} \renewcommand{\cD}{2} \FctAss{} % UNCOMMENT IF YOU WANT TO SEE MORE % % This is where the test on maxY is useful. % \renewcommand{\cA}{3} % \renewcommand{\cB}{-14} % \renewcommand{\cC}{2} % \renewcommand{\cD}{-2} % % \FctAss{} % % % And here the test on minY is useful. % \renewcommand{\cA}{3}

2 de 4

19/04/2012 17:12

Parameterized plots | TikZ example

http://www.texample.net/tikz/examples/parameterized-plots/

% % % %

\renewcommand{\cB}{14} \renewcommand{\cC}{2} \renewcommand{\cD}{-2}

% \FctAss{} % \end{document}

Comments
#1 David Olivieri, September 24, 2009 at 11:21 p.m. Great Site!! Thanks for the help. This example is exactly what I was looking for... Cheers. #2 Dominik, October 19, 2009 at 12:18 a.m. You could improve the \FctAss command by parameterize it itself:

\newcommand{\FctAss}[4]{ ... }

This allows you to use #1-#4 inside the command definition and \FctAss{1}{0}{1}{0} for the typesetting. Alway performing a \renewcommand before the use of \FctAss isn't intuituve ;-) --Dominik #3 ben, February 3, 2010 at 2:25 p.m. Hey everyone and thanks !! you guys are helping me pretty much on a daily basis... anyway, I'd like to plot a function that shows an hysteresis, and putting arrow to show what side of the curve means x going up or down... Scilab does that pretty easily, but I couldn't find a way to do it conveniently with tikz. thanks for your help ! #4 Michel AlSharidah, February 3, 2012 at 2:52 a.m. Hi This is a great example but i could not get the function plot on the graphs, the only thing that shows is the axis, grid, and the text but no plot. am i missing something? I just noticed the below, pls help thanks Preview: Fontsize 10pt Preview: PDFoutput 1 Package pgf Warning: Plot data file `parameterized-plots.pgf-plot.table' not found. on input line 59. Package pgf Warning: Plot data file `parameterized-plots.pgf-plot.table' not found. on input line 67. Package pgf Warning: Plot data file `parameterized-plots.pgf-plot.table' not found. on input line 75. Package pgf Warning: Plot data file `parameterized-plots.pgf-plot.table' not found. on input line 83. Package pgf Warning: Plot data file `parameterized-plots.pgf-plot.table' not found. on input line 91.

Post a comment
Name Email address URL

3 de 4

19/04/2012 17:12

Parameterized plots | TikZ example

http://www.texample.net/tikz/examples/parameterized-plots/

Comment

Markdown syntax enabled. No HTML allowed. about | contact

4 de 4

19/04/2012 17:12

Vous aimerez peut-être aussi