I want to display propagation of a signal, obtained as addition of 2. It works as I want writing:
\documentclass{standalone}
\usepackage[usenames,svgnames]{xcolor}
\usepackage{siunitx}
\usepackage{tikzscale}
\usepackage{pgf,tikz,pgfplots}
\usetikzlibrary{math}
\begin{document}
\begin{tikzpicture}
\tikzmath{
\omm=0;
\omM=10;
% sin sur une période, 0 sinon
function sina(\x) {
if and(\x>0,\x<1) then {
return sin(360\x);}
else {return 0;};};
% fréquence double
function sinb(\x) {
if and(\x>0,\x<1) then {
return sin(720\x);}
else {return 0;};};
}
\begin{axis}[width=.8\columnwidth,
height=5cm,
xmin=\omm,
xmax=\omM,
axis y line=left,
ylabel= \textcolor{blue}{Amplitude},
xlabel= position,
smooth,
]
\addplot+[blue,thin, mark=none, samples=200,domain = \omm:\omM] {sina(x)};
\addplot+[blue,thin, dashed, mark=none, samples=200,domain = \omm:\omM] {sinb(x)};
\addplot+[DarkRed,thick, mark=none, samples=200,domain = \omm:\omM] {sina(x)+sinb(x)};
\end{axis}
\begin{axis}[width=.8\columnwidth,
height=5cm,
yshift=-5cm,
xmin=\omm,
xmax=\omM,
axis y line=left,
ylabel= \textcolor{blue}{Amplitude},
xlabel= position,
smooth,
]
\addplot[blue,thin, mark=none, samples=200,domain = \omm:\omM] {sina(x-4)};
\addplot[blue,thin, dashed, mark=none, samples=200,domain = \omm:\omM] {sinb(x-4)};
\addplot+[DarkRed,thick, mark=none, samples=200,domain = \omm:\omM] {sina(x-4)+sinb(x)};
\end{axis}
\end{tikzpicture}
\end{document}
but I definitively wouldn't expect to write sina(x-4)+sinb(x) for the shifted addition, but sina(x-4)+sinb(x-4)...
Is it really the expected behavior? If it is, can the logic behind be explained, as I found the good syntaxe by mistake.
Edit: Another alternative syntax to get the correct result, maybe it can help someone to understand what's happening:
\addplot+[DarkRed,thick, mark=none, samples=200,domain = \omm:\omM] {sinb(\x-4) + sina(\x-4)};
Replacing the usual x variable in the \addplot syntax for functions by \x shifts as expected the function sina.
Edit 2: If in the definition of sina() or sinb() the name of the variable is changed, for example by \y, the expected behavior is back. By replacing the definition of sinb() by:
% fréquence double
function sinb(\y) {
if and(\y>0,\y<1) then {
return sin(720*\y);}
else {return 0;};};
then the expected syntax (sina(x-4)+sinb(x-4) ) gives the expected result.

\xin the second example. If you use, for example, (x - 1) as\x, you get sin(360x + 360) for the first function and sin(720x + 720) for the second function. Thus, the second function returns a result that is shifted relative to the first one. Since you only add the parts from the functions where 0 > (x - 1) > 1, these two parts will never overlap. At least to my humble understanding ... – Jasper Habicht Oct 28 '22 at 06:55xand\xare intended to be the same (so if they are not the same then something strange is going on). – Marijn Nov 09 '22 at 19:42xin the definitions of functions. – Hérisson Didier Nov 09 '22 at 20:29