大家好,我想绘制一个具有 1 个变量及其总和的函数,因此基本上我的函数是这样的:
sum(11/(2*k-1)*sin(2*50*\%pi*(2*k-1)*x)
我想用 Gnuplot 来实现它,但它给了我一个错误消息:
Package pgfplots Error: Sorry, the gnuplot-result file 'test.pgf-plotlot <file>.gnuplot' manually on the respective gnuplot file.. }
\documentclass{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
xlabel = $t$,
xtick = {-.5,.5},
xticklabels = {$-\frac{T}{2}$, $\frac{T}{2}$},
ytick = {-1,-.5,.5,1},
yticklabels = {$-A$, $-\frac{A}{2}$, $\frac{A}{2}$, $A$},
domain = -.75:.75,
samples = 200,
]
\addplot[mark = none] gnuplot {(x - floor(x +.5)) < 0 ? -1 : 1};
\addlegendentry{Signal}
\foreach \i in {2,4,6,8}{%
\addplot+[mark = none] gnuplot[raw gnuplot] {%
set samples 200;
fourier(k, x) = (11 /(2*k-1)*sin(2*50*\%pi*(2*k-1)*x)
plot[-.75:.75] sum [k=0:\i] fourier(k,x)};
\addlegendentryexpanded{\number\numexpr 2*\i+1\relax{} first terms}
}
\end{axis}
\end{tikzpicture}
\end{document}
答案1
gnuplot 代码中有一些错误。它应该是pi
而不是\%pi
,并且在 的定义后缺少右括号和分号fourier
。我删除了 的外括号fourier
并添加了分号。
% arara: pdflatex: { shell: yes }
\documentclass{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
xlabel = $t$,
xtick = {-.5,.5},
xticklabels = {$-\frac{T}{2}$, $\frac{T}{2}$},
ytick = {-1,-.5,.5,1},
yticklabels = {$-A$, $-\frac{A}{2}$, $\frac{A}{2}$, $A$},
domain = -.75:.75,
samples = 200,
]
\addplot[mark = none] gnuplot {(x - floor(x +.5)) < 0 ? -1 : 1};
\addlegendentry{Signal}
\foreach \i in {2,4,6,8}{%
\addplot+[mark = none] gnuplot[raw gnuplot] {%
set samples 200;
fourier(k, x) = 11 /(2*k-1)*sin(2*50*pi*(2*k-1)*x);
plot[-.75:.75] sum [k=0:\i] fourier(k,x);
};
\addlegendentryexpanded{\number\numexpr 2*\i+1\relax{} first terms}
}
\end{axis}
\end{tikzpicture}
\end{document}