感谢您阅读我的问题!
介绍
现在我想画一幅图,其中我需要计算路口两条曲线的交点,并确定交点的 y 值是否为零。如果不为零,我需要画一条通过该点垂直于 x 轴的直线。
我将使用循环(foreach
) 来完成操作,但现在我在获取循环总数时遇到了问题。总数循环应该是总数,intersections
但现在看来,总数intersections
没有正确复制到宏中\t
。
这是我的代码: 我的环境是 VSCode + TexLive + LaTeX Workshop
\documentclass[tikz]{standalone}
\usepackage{pgfplots,pgf,tikz}
\usepackage{amsmath,amssymb,amsfonts,amsthm}
\pgfplotsset{compat=1.18}
\usetikzlibrary{intersections}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}
\def\sigmarayleigh{1}
\def\gaussmean{4}
\def\gaussvar{1.5}
\def\xmin{0}
\def\xmax{8}
\def\ymin{0}
\def\ymax{0.7}
\begin{axis}[title = Fig1,xlabel = $Voltage$,ylabel = $Probability Density$,xmin = \xmin, xmax = \xmax, ymin = \ymin, ymax = \ymax, grid = both, legend pos = north east]
\coordinate (O) at (\xmin,\ymin);
\coordinate (X) at (\xmax,\ymin);
\coordinate (Y) at (\xmin,\ymax);
\addplot[name path = rayleigh, domain = 0:8, samples = 100, color = black, dashed, line width = 1pt]{x*exp(-x^2/(2*\sigmarayleigh^2))/(\sigmarayleigh^2)};
\addplot[name path = gauss, domain = 0:8, samples = 100, color = black, line width = 1pt]{1/(sqrt(2*pi*\gaussvar))*exp(-(x-\gaussmean)^2/(2*\gaussvar))};
\path [name intersections={of=rayleigh and gauss, name = ii, total=\t}];
\foreach \s in {1,...,\t}{
\typeout{Total intersections: \s};
};
\end{axis}
\end{tikzpicture}
\end{document}
我将测试我的循环是否有效,但此代码无法成功编译。
以下是编译日志:
然后我对我的代码做了简单的修改,把 替换成\t
固定的数字9,然后这段代码就能成功运行了,修改后的代码如下:
\documentclass[tikz]{standalone}
\usepackage{pgfplots,pgf,tikz}
\usepackage{amsmath,amssymb,amsfonts,amsthm}
\pgfplotsset{compat=1.18}
\usetikzlibrary{intersections}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}
\def\sigmarayleigh{1}
\def\gaussmean{4}
\def\gaussvar{1.5}
\def\xmin{0}
\def\xmax{8}
\def\ymin{0}
\def\ymax{0.7}
\begin{axis}[title = Fig1,xlabel = $Voltage$,ylabel = $Probability Density$,xmin = \xmin, xmax = \xmax, ymin = \ymin, ymax = \ymax, grid = both, legend pos = north east]
\coordinate (O) at (\xmin,\ymin);
\coordinate (X) at (\xmax,\ymin);
\coordinate (Y) at (\xmin,\ymax);
\addplot[name path = rayleigh, domain = 0:8, samples = 100, color = black, dashed, line width = 1pt]{x*exp(-x^2/(2*\sigmarayleigh^2))/(\sigmarayleigh^2)};
\addplot[name path = gauss, domain = 0:8, samples = 100, color = black, line width = 1pt]{1/(sqrt(2*pi*\gaussvar))*exp(-(x-\gaussmean)^2/(2*\gaussvar))};
\path [name intersections={of=rayleigh and gauss, name = ii, total=\t}];
\foreach \s in {1,...,9}{
\typeout{Total intersections: \s};
};
\end{axis}
\end{tikzpicture}
\end{document}