我正在尝试使用框架叠加和 ppgfplots 绘制曲线的一系列割线。我正在尝试的是
\documentclass{beamer}
\usetheme{Warsaw}
\usepackage[utf8]{inputenc}
\usepackage{tikz,pgfplots}
\usetikzlibrary{arrows,positioning,shapes,fit,calc,intersections}
\pgfplotsset{compat=newest}
\begin{document}
\begin{frame}
\begin{tikzpicture}
\begin{axis}[
axis x line=center,
axis y line=middle,
xlabel=$t$,
ylabel=$S$,
ytick={0,50,...,650},
xtick={0,1,...,10},
restrict y to domain=0:600,
domain=0:11
]
\addplot[blue,thick] {x+5*x^2};
\addplot[color=blue,only marks,mark=*] coordinates{(8,328)};
\foreach \ta in {4,4.5,5,5.5,6,6.5,7} {
\foreach \Sa in {\ta+5.0*\ta^2}
\only<+-+>{
\addplot[color=blue,only marks,mark=*] coordinates{(\ta,\Sa)};
\pgfmathsetmacro{\m}{(328.0-\Sa)/(8.0-\ta)};
\pgfmathsetmacro{\b}{328-\m*8.0};
\addplot[black,thick] {\m*x+\b};
\addplot[red,thick] {\m};
};
}
\end{axis}
\end{tikzpicture}
\end{frame}
\end{document}
这个想法是保持点 (8,328) 固定并且割线从左侧接近它。
我不确定发生了什么,但似乎我的割线的角度系数计算有误,正如我通过绘图看到的那样。系数序列m
应该是这样的[61.0,63.5,66.0,68.5,71.0,73.5,76.0,78.5]
,但这不是我得到的。
有什么建议吗?谢谢。
答案1
使用\pgfmathsetmacro{\Sa}{\ta+5.0*\ta^2}
:
代码:
\documentclass{beamer}
\usetheme{Warsaw}
\usepackage[utf8]{inputenc}
\usepackage{pgfplots}% loads also tikz
\pgfplotsset{compat=newest}
\begin{document}
\begin{frame}
\begin{tikzpicture}
\begin{axis}[
axis x line=center,
axis y line=middle,
xlabel=$t$,
ylabel=$S$,
ytick={0,50,...,650},
xtick={0,1,...,10},
restrict y to domain=0:600,
domain=0:11
]
\addplot[blue,thick] {x+5*x^2};
\addplot[color=blue,only marks,mark=*] coordinates{(8,328)};
\foreach \ta in {4,4.5,5,5.5,6,6.5,7} {
\pgfmathsetmacro{\Sa}{\ta+5.0*\ta^2}% <- changed
\only<+-+>{
\addplot[color=blue,only marks,mark=*] coordinates{(\ta,\Sa)};
\pgfmathsetmacro{\m}{(328.0-\Sa)/(8.0-\ta)};
\pgfmathsetmacro{\b}{328-\m*8.0};
\addplot[black,thick] {\m*x+\b};
\addplot[red,thick] {\m};
};
}
\end{axis}
\end{tikzpicture}
\end{frame}
\end{document}