如何绘制正弦曲线和零函数之间的交点。
我尝试了此代码但看起来不太好。
\documentclass{article}
\usepackage{pgfplots}
\usetikzlibrary{intersections}
% Grouping the common style settings here to make the code below easier to read
\pgfkeys{/pgfplots/Axis Style/.style={
width=13.5cm, height=5cm,
axis x line=center,
axis y line=middle,
samples=100,
% ymin=-1.5, ymax=1.5,
ymin=-1.0, ymax=1.0,
%xmin=-7.0, xmax=7.0,
xmin=0, xmax=9.0,
domain=0*pi:8*pi
}}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
Axis Style,
xtick={
0.4268, 3.1416, 5.8564, 6.2832, 6.7100
},
xticklabels={
$~$, $x_1$, $x_2$, $x_3$,
$x_4$, $x_5$
}
]
\addplot [name path=line 5] (0,1) -- (2050,0);
%\addplot [name path=line 0] (0,1) -- (1,1);
\addplot [name path=line 0] (0,0) -- (2850,0); % I want to draw stright line from (0,1) to (5,1) but unfortuntely i could not
\addplot [name path=line 1, ultra thick, red] {sin(deg(x))};
\addplot [name path=line 2, ultra thick, blue] {cos(deg(x))};
\fill[green,name intersections={of=line 1 and line 0,total=\t}]
\foreach \s in {1,...,\t}{(intersection-\s) circle (2pt) node {\footnotesize\s}};
\fill[green,name intersections={of=line 2 and line 5,total=\t}]
\foreach \s in {1,...,\t}{(intersection-\s) circle (2pt) node {\footnotesize\s}};
\end{axis}
\end{tikzpicture}
\end{document}
谁能有更好的方法?
答案1
正如我在问题下方的评论中提到的那样,您使用了一种奇怪的语法\addplot coordinates
。这里是正确的变体,它应该能产生所需的行为。我认为,使用它来找到“余弦”曲线与“零”线的交点会非常简单。
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
\usetikzlibrary{intersections}
% Grouping the common style settings here to make the code below easier to read
\pgfkeys{
/pgfplots/Axis Style/.style={
width=13.5cm, height=5cm,
axis x line=center,
axis y line=middle,
samples=100,
ymin=-1.0, ymax=1.0,
xmin=0, xmax=9.0,
domain=0*pi:8*pi
}
}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
Axis Style,
xtick={
0.4268, 3.1416, 5.8564, 6.2832, 6.7100
},
xticklabels={
$~$, $x_1$, $x_2$, $x_3$,
$x_4$, $x_5$
},
]
\addplot [name path=line 5] coordinates { (0,1) (8*pi,1) };
\addplot [name path=line 0] coordinates { (0,0) (8*pi,0) };
\addplot [name path=line 1, ultra thick, red] {sin(deg(x))};
\addplot [name path=line 2, ultra thick, blue] {cos(deg(x))};
\fill [green,name intersections={of=line 1 and line 0,total=\t}]
\foreach \s in {1,...,\t}{
(intersection-\s) circle (2pt)
% the nodes are almost invisible and partially clipped ...
% Are they really needed?
node {\color{black}\footnotesize\s}}
;
\fill [green,name intersections={of=line 2 and line 5,total=\t}]
\foreach \s in {1,...,\t}{
(intersection-\s) circle (2pt)
node {\color{black}\footnotesize\s}}
;
\end{axis}
\end{tikzpicture}
\end{document}