我有这个代码:
\documentclass[border=2pt]{standalone}
\usepackage[utf8]{inputenc}
\usepackage{animate}
\usepackage{calc}
\usepackage{fp}
\usepackage{pgfplots}
\usepackage{tikz}
\usetikzlibrary{arrows}
\newcommand*{\xMin}{0}%
\newcommand*{\xMax}{20}%
\newcommand*{\yMin}{0}%
\newcommand*{\yMax}{10}%
\begin{document}
\begin{tikzpicture}[scale=1.5]
\foreach \i in {\xMin,...,\xMax} {
\draw [very thin,gray] (\i,\yMin) -- (\i,\yMax) node [below] at (\i,\yMin) {$\i$};
}
\foreach \i in {\yMin,...,\yMax} {
\draw [very thin,gray] (\xMin,\i) -- (\xMax,\i) node [left] at (\xMin,\i) {$\i$};
}
\draw[->, thick] (0,0)--(20,0) node[right] (X) {x};
\draw[->, thick] (0,0)--(0,10) node[above] (Y) {y};
\end{tikzpicture}
\end{document}
问题:我不知道 a) 如何创建曲线和 b) 如何制作动画。
最后看起来应该是这样的:https://en.wikipedia.org/wiki/Simpson%27s_rule#/media/File:Simpsonsrule2.gif
希望你能帮助我。我知道这是一个大项目,但我陷入困境。如果我得到一些帮助,我也许可以编写更多代码,但现在我陷入困境。
提前致谢。
答案1
这是一个使用选项pgfplots
并显示二次插值:
代码:
\documentclass{beamer}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\pgfmathdeclarefunction{mainfunct}{1}{%
\pgfmathparse{cos(deg(2*#1))+1.5}%
}
% The Simpson interpolating function
\pgfmathdeclarefunction{Simpsonfunct}{3}{%
\pgfmathparse{%
mainfunct(#2)*(((#1-((#2+#3)/2))*(#1-#3))/((#2-((#2+#3)/2))*(#2-#3))) +
mainfunct((#2/2+#3/2))*(((#1-#2)*(#1-#3))/((((#2+#3)/2)-#2)*(((#2+#3)/2)-#3))) +
mainfunct(#3)*(((#1-#2)*(#1-((#2+#3)/2)))/((#3-#2)*(#3-((#2+#3)/2))))
}%
}
\newcommand\DrawSimpson[1]{
\pgfmathsetmacro{\tmp}{#1+#1}
\foreach \Valor in {#1,\tmp,...,12}
{
\addplot[red!75!blue,very thick,domain=\Valor-#1:\Valor] {Simpsonfunct(x,\Valor-#1,\Valor)};
\addplot[no marks,red!75!blue] coordinates {(\Valor,0) (\Valor,{mainfunct(\Valor)})};
}
}
\setbeamertemplate{navigation symbols}{}
\begin{document}
\begin{frame}
\centering
\begin{tikzpicture}[]
\begin{axis}[
height=0.8\textheight,
width=11.5cm,
ytick=\empty,
xticklabel style={font=\small, text height=1.5ex, anchor=north},
xtick={-4,-2,...,16},
ymin=0,
xmin=-4,
xmax=16,
]
% The function
\addplot[very thick, cyan!75!blue,domain=-pi:5*pi,samples=200] {mainfunct(x)};
% The vertical line from (0,0) to the function (it's constant for all plots)
\only<2->{\addplot[no marks,red!75!blue] coordinates {(0,0) (0,{mainfunct(0)})};}
\only<2>{\DrawSimpson{6}}
\only<3>{\DrawSimpson{4}}
\only<4>{\DrawSimpson{3}}
\only<5>{\DrawSimpson{2}}
\only<6>{\DrawSimpson{1}}
\only<7>{\DrawSimpson{0.5}}
\only<8>{\DrawSimpson{0.25}}
\end{axis}
\end{tikzpicture}
\end{frame}
\end{document}
动画是使用 ImageMagick 制作的,
convert -verbose -delay 80 -loop 0 -density 300 simpson.pdf simpson.gif
答案2
这是一个pgfplots
版本(但没有插值)。用于gimp
制作动画(将生成的 PDF 作为图层导入,反转图层,然后导出到gif
)。
\documentclass[tikz,border=5]{standalone}
\usepackage{pgfplots}
\begin{document}
\foreach \samples in {3,...,30}{%
\begin{tikzpicture}[declare function={f(\x)=sin(\x*180)*100+150;}]
\begin{axis}[ymin=0, ymax=300, xmin=0, xmax=10,
xlabel=$x$, ylabel=$f(x)$]
\addplot [thick, cyan, domain=0:10, samples=250] (x, {f(x)});
\addplot [ycomb, purple, domain=2:8, samples=\samples] (x, {f(x)});
\addplot [thick, red, domain=2:8, samples=\samples] (x, {f(x)});
\end{axis}
\end{tikzpicture}}
\end{document}