在解决帖子中两个正弦波的 tikz 图,我试图修改代码以包含两个信号的乘积。你如何对两个信号的乘积进行编码?我最初尝试只对信号求和,但是,我没有得到输出。你能帮我得到两个信号的乘积吗?谢谢!
以下是经过轻微修改的代码,用于将两个信号相加:
\documentclass[pstricks,12pt,dvipsnames]{standalone}
\usepackage{amsmath}
\usepackage{pstricks-add}
\usepackage{pst-plot}
\usepackage{xcolor}
\usepackage[nomessages]{fp}
\FPeval\XMin{0-pi/6}
\FPeval\XMax{2*pi}
\FPeval\YMin{0-3/2}
\FPeval\YMax{5/2}
\FPeval\XOL{0-1/2} % of DeltaX
\FPeval\XOR{1/2} % of DeltaX
\FPeval\YOB{0-1/5} % of DeltaY
\FPeval\YOT{1/5} % of DeltaY
\FPset\xTrigLabelBase{6}
\FPeval\yTrigLabelBase{pi}
\FPset\Dx{1}
\FPset\Dy{1}
\FPeval\dx{pi/xTrigLabelBase*Dx}
\FPeval\dy{pi/yTrigLabelBase*Dy}
\FPeval\AxisL{XMin+dx*XOL}
\FPeval\AxisR{XMax+dx*XOR}
\FPeval\AxisB{YMin+dy*YOB}
\FPeval\AxisT{YMax+dy*YOT}
\newlength\Width\Width=12cm
\newlength\Height\Height=8cm
\newlength\llx\llx=-5pt
\newlength\urx\urx=15pt
\newlength\lly\lly=-5pt
\newlength\ury\ury=15pt
\psset
{
llx=\llx,
lly=\lly,
urx=\urx,
ury=\ury,
xtrigLabels=true,
%ytrigLabels=true,
xtrigLabelBase=\xTrigLabelBase,
%ytrigLabelBase=\yTrigLabelBase,
labelFontSize=\scriptstyle,
xAxisLabel=$x$,
yAxisLabel=$y$,
algebraic,
plotpoints=500,
}
\newpsstyle{mygrid}
{
dx=\dx,
dy=\dy,
%Dx=\Dx,
%Dy=\Dy,
labels=none,
subticks=5,
tickwidth=.4pt,
subtickwidth=.2pt,
tickcolor=Red!30,
subtickcolor=ForestGreen!30,
xticksize=\YMin\space \YMax,
yticksize=\XMin\space \XMax,
subticksize=1,
}
\def\f{sin(x)}
\def\g{sin(x+Pi/6)}
\def\h{sin(x) + sin(x+Pi/6)}
\begin{document}
\pslegend[rt]{%
\color{NavyBlue}\rule{12pt}{1pt} & \color{NavyBlue} $y=\sin x$ \\
\color{Red}\rule{12pt}{1pt} & \color{Red} $y=\sin(x+\pi/6) x$
}
\begin{psgraph}
[
dx=\dx,
dy=\dy,
Dx=\Dx,
Dy=\Dy,
linecolor=gray,
tickcolor=gray,
ticksize=-3pt 3pt,
axespos=top,
]{<->}(0,0)(\AxisL,\AxisB)(\AxisR,\AxisT){\dimexpr\Width-\urx+\llx}{!}%{\dimexpr\Height-\ury+\lly}
\psaxes[style=mygrid](0,0)(\XMin,\YMin)(\XMax,\YMax)
\psplot[linecolor=NavyBlue]{\XMin}{\XMax}{\f}
\psplot[linecolor=Red]{\XMin}{\XMax}{\g}
\psplot[linecolor=Cyan]{\XMin}{\XMax}{\h}
\pcline[linestyle=dashed,offset=.5,linecolor=Magenta]{|*-|*}(*{Pi 3 div} {\f})(*{Pi 2 div} {\g})\naput{ $\scriptstyle\Delta x = \tfrac{\pi}{6}$}
\end{psgraph}
\end{document}
答案1
这里有一个使用datavisulization
库的解决方案tikz
。为了创建pi
基于的,我使用了如下所述的x-axis
自定义库。tick typesetter
77.4.11在里面手册。
在你的问题中你问的是总数和函数的乘积。因为我不确定你真正需要哪一个,所以我绘制了两者。此外,你的绘图标签显示函数g
获得了额外的* x
,而你的函数定义g
不执行此计算。如果你需要* x
函数中的,你可以根据自己的需要调整以下代码。
根据评论要求,更新了包含较小网格的版本
\documentclass[tikz, border=5mm]{standalone}
\usetikzlibrary{datavisualization.formats.functions, arrows}
\def\mytypesetter#1{
\pgfmathparse{#1/pi}
\pgfkeys{/pgf/number format/precision=2}
\pgfmathroundtozerofill{\pgfmathresult}
\pgfmathifisint{\pgfmathresult}{
\pgfmathprintnumber{\pgfmathresult}$\pi$
}{
\pgfmathprintnumber[/pgf/number format/frac, frac denom=6, frac whole=false]{\pgfmathresult}$\pi$
}
}
\begin{document}
\begin{tikzpicture}
\datavisualization [
school book axes,
all axes={
grid={
major={style={red!50!black, opacity=.25}},
minor={style={green!25!black, opacity=.25}},
minor steps between steps=4,
}},
x axis={
label=$x$,
ticks and grid={
stack,
step=(pi/6),
tick typesetter/.code=\mytypesetter{##1},
}},
y axis={
label=$y$,
grid={step=1}
},
style sheet=vary hue,
visualize as line/.list={sin1, sin2, prod, sum},
sin1={label in legend={text={$\sin(x)$}}},
sin2={label in legend={text={$\sin\left(x+\frac{\pi}{6}\right)$}}},
prod={label in legend={text={$\sin(x) \sin\left(x+\frac{\pi}{6}\right)$}}},
sum={label in legend={text={$\sin(x) + \sin\left(x+\frac{\pi}{6}\right)$}}},
]
data [set=sin1, format=function] {
var x : interval [-pi/6:2*pi] samples 100;
func y = sin(\value x r);
}
data [set=sin2, format=function] {
var x : interval [-pi/6:2*pi] samples 100;
func y = sin((\value x + pi/6)r);
}
data [set=prod, format=function] {
var x : interval [-pi/6:2*pi] samples 100;
func y = sin(\value x r) * sin((\value x + pi/6)r);
}
data [set=sum, format=function] {
var x : interval [-pi/6:2*pi] samples 100;
func y = sin(\value x r) + sin((\value x + pi/6)r);
};
\draw [{Bar[]}-{Bar[]}, thick, dashed] (2*pi/6,1.25) -- ++(pi/6,0) node [above, midway] {\scriptsize $\Delta x = \frac{\pi}{6}$};
\end{tikzpicture}
\end{document}