绘制具有不同高度的锯齿函数

绘制具有不同高度的锯齿函数

我正在准备一个关于处处连续、无处可微函数的例子的笔记。该构造涉及一个锯齿函数,即h在此处输入图片描述

我想要绘制以下函数,比如在域上[-1,1]

  1. h(x)
  2. h_1(x)
  3. h_2(x)
  4. h_3(x)
  5. h(x)+h_1(x)
  6. h(x)+h_1(x)+h_2(x)
  7. h(x)+h_1(x)+h_2(x)+h_3(x)

我正在尝试定义并绘制函数htikz因为这样,我就可以最轻松地完成其余的工作。这就是我陷入困境的地方。以下 MWE 总结了我的努力:

\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\begin{document}
    \begin{figure}
        \begin{tikzpicture}[font=\scriptsize, scale=3,%
            declare function={h(\x) = (\x>= {floor(\x)}, \x<= {floor(\x)+1/2})*(\x-floor(\x)) +
                and 
                (\x> {floor(\x)+1/2}, \x<= {floor(\x)+1})*(1-\x+floor(\x));}
            ]
            \draw plot[domain=-5:5] (\x,{h(\x)});
        \end{tikzpicture}
    \end{figure}
\end{document}

我如何定义h以便它可以计算h(x)任何实际值x?任何帮助都将不胜感激。在 Desmos 中完成的图表副本可以在这里找到

答案1

它看起来就像是您想要绘制的范德华登函数(或者更确切地说是它的构造)。

一般来说,对于这样的图形,我认为您不需要在整条线上定义函数,但在区间 [-1,1] 上有一个工作变量就足够了,并在必要时将其移位绘制。

在最近的分析书籍项目中,我们使用了h(x) = abs(x - round(x)),它实际上适用于所有真实的 x。我想你也可以在 tikz 中使用它。

无论如何,我写的书是用 ConTeXt 写的,图形是用 MetaPost/MetaFun 完成的,所以下面的例子就是这样做的。

\starttext
\startuseMPgraphic{vanderwaerdenbas}
u:=3cm ;

def vdwbas(expr x) = abs(x-round x) enddef ;

def vdw(expr n,x) =
  if n = 0:
    vdwbas(x)
  else:
    vdwbas(x*pow(4,n))/pow(4,n)
  fi
enddef ;

path xaxis, yaxis ;
xaxis = ((-1.1,0)--(1.1,0)) scaled u ;
yaxis = ((0,-0.1)--(0,0.6)) scaled u ;

drawarrow xaxis withpen pencircle scaled 0.25 ;
drawarrow yaxis withpen pencircle scaled 0.25 ;
\stopuseMPgraphic

\startuseMPgraphic{vanderwaerden0}
\includeMPgraphic{vanderwaerdenbas}
draw function(1,"x","vdw(0,x)",epsed(-1),epsed(1),1/1000) scaled u ;
\stopuseMPgraphic

\startuseMPgraphic{vanderwaerden1}
\includeMPgraphic{vanderwaerdenbas}
draw function(1,"x","vdw(1,x)",epsed(-1),epsed(1),1/1000) scaled u ;
\stopuseMPgraphic

\startuseMPgraphic{vanderwaerden2}
\includeMPgraphic{vanderwaerdenbas}
draw function(1,"x","vdw(2,x)",epsed(-1),epsed(1),1/1000) scaled u ;
\stopuseMPgraphic

\startuseMPgraphic{vanderwaerden3}
\includeMPgraphic{vanderwaerdenbas}
draw function(1,"x","vdw(3,x)",epsed(-1),epsed(1),1/1000) scaled u ;
\stopuseMPgraphic

\startuseMPgraphic{vanderwaerden01}
\includeMPgraphic{vanderwaerdenbas}
draw function(1,"x","vdw(1,x)+vdw(0,x)",epsed(-1),epsed(1),1/1000) scaled u ;
\stopuseMPgraphic

\startuseMPgraphic{vanderwaerden012}
\includeMPgraphic{vanderwaerdenbas}
draw function(1,"x","vdw(2,x)+vdw(1,x)+vdw(0,x)",epsed(-1),epsed(1),1/1000) scaled u ;
\stopuseMPgraphic

\startuseMPgraphic{vanderwaerden0123}
\includeMPgraphic{vanderwaerdenbas}
draw function(1,"x","vdw(3,x)+vdw(2,x)+vdw(1,x)+vdw(0,x)",epsed(-1),epsed(1),1/1000) scaled u ;
\stopuseMPgraphic

\startplacefigure[reference=fig:vdw]
\startcombination[nx=2,ny=4]
{\useMPgraphic{vanderwaerden0}}   {(a)}
{\useMPgraphic{vanderwaerden0}}   {(b)}
{\useMPgraphic{vanderwaerden1}}   {(c)}
{\useMPgraphic{vanderwaerden01}}  {(d)}
{\useMPgraphic{vanderwaerden2}}   {(e)}
{\useMPgraphic{vanderwaerden012}} {(f)}
{\useMPgraphic{vanderwaerden3}}   {(g)}
{\useMPgraphic{vanderwaerden0123}}{(h)}
\stopcombination
\stopplacefigure
\stoptext

范德华登

相关内容