PGF and TIkz里面自定义函数的例子:
\documentclass{beamer} \input{tikzall.tex} %包含所有的tikz包 \begin{document} \begin{frame} %第一种定义方法 \begin{tikzpicture} \draw [help lines] (0,0) grid (3,2); \draw [blue, thick, x=0.0085cm, y=1cm, declare function={ sines(\t,\a,\b)=1 + 0.5*(sin(\t)+sin(\t*\a)+sin(\t*\b)); }] plot [domain=0:360, samples=144, smooth] (\x,{sines(\x,3,5)}); \end{tikzpicture} \end{frame} \begin{frame} %第二种定义方法 \begin{tikzpicture}[ declare function={ excitation(\t,\w) = sin(\t*\w); noise = rnd - 0.5; source(\t) = excitation(\t,20) + noise; filter(\t) = 1 - abs(sin(mod(\t, 90))); speech(\t) = 1 + source(\t)*filter(\t); } ] \draw [help lines] (0,0) grid (3,2); \draw [blue, thick, x=0.0085cm, y=1cm] (0,1) --plot [domain=0:360, samples=144, smooth] (\x,{speech(\x)}); \end{tikzpicture} \end{frame} \end{document}rnd是生成一个随机数的函数.:
declare function后面定义的函数,可以立即使用前面定义的函数.
效果如图所示:
还有使用datavisualization 绘制函数也是很好的一种方法:
\begin{tikzpicture} \datavisualization [school book axes, visualize as smooth line] data [format=function] { var x : interval [-1.5:1.5] samples 7; func y = \value x*\value x; }; \end{tikzpicture}效果如下: