绘制不连续函数的图形?

绘制不连续函数的图形?

我尝试绘制 -3π 到 3πi 区间内的以下不连续函数

f(t)=t^2 , 0 \leq t < \pi , f(t)= f(t+n\pi)

我的代码没有取得多大成功。我想到的是

    \begin{center}
        \begin{tikzpicture}
            \begin{axis}[
                height = 4cm, width = 12cm,
                axis lines =center,
                xmin=-9.4247, xmax=9.4247,
                ymin=0, ymax = 9.8696,
                xtick = {-9.4247,-6.2831,...,9.4247},
                xticklabels = {-3$\pi$,-2$\pi$,-$\pi$,0,$\pi$,2$\pi$,3$\pi$},
                ytick = {9.8696},
                yticklabels = {$\pi^2$},
                ]
                \addplot[domain = -9.4247:-6.2831,blue]{x*x};
                \addplot[domain = -6.2831:-3.1415,blue]{x*x};
                \addplot[domain = -3.1415:0,blue]{x*x};
                \addplot[domain = 0:3.1415,blue]{x*x};
                \addplot[domain = 3.1415:6.2831,blue]{x*x};
                \addplot[domain = 6.2831:9.4247,blue]{x*x};
            \end{axis}
        \end{tikzpicture}
    \end{center}

我希望最终的情节应该是这样的:我希望的结局是这样的

我的代码输出以下图表:

我的代码输出以下图表:

我该如何绘制我的函数?我认为我需要一种方法来将其向左和向右移动,或者也许有更简单的方法?

任何帮助将不胜感激。

答案1

Asymptote版本:

// discontf.asy
//
// run 
//    asy discontf.asy
// to get a standalone   discontf.pdf
//
settings.outformat="pdf";
import graph;
import math;
import fontsize;defaultpen(fontsize(8pt));
texpreamble("\usepackage{lmodern}"+"\usepackage{amsmath}"
+"\usepackage{amsfonts}"+"\usepackage{amssymb}");

real scx=pi/2, scy=pi^2/2;
int xCells=14, yCells=4;
add(shift(-7*scx,-1*scy)*scale(scx,scy)*grid(xCells,yCells,paleblue+0.2bp));

real pagew=9cm,pageh=yCells/xCells*9cm;
size(pagew,pageh,IgnoreAspect);
arrowbar arr=Arrow(HookHead,size=2);

real xmin=-3pi, xmax=3pi, ymin=0, ymax=pi^2*1.2,
  dxmin=0.6, dxmax=0.6, dymin=dxmin, dymax=dxmax;

xaxis(xmin-dxmin,xmax+dxmax,above=true,arr);
yaxis(ymin-dymin,ymax+dymax,above=true,arr);

for(int i=-3;i<=3;++i){
  xtick(z=(i*pi,0),dir=plain.S,size=(Ticksize+ticksize)/2); 
  labelx("$"+((abs(i)>1 || i==0)?string(i):"")
     +((i==0)?"":"\pi")+"$", z=(i*pi,0),align=3*plain.S);
}
ytick(z=(0,pi^2),plain.E,size=(Ticksize+ticksize)/2); 
labely("$\pi^2$",(0,pi^2),3*plain.E);

pair f(real x){return (x,x^2);}

guide g=graph(f,0,pi);
pair discontPoint0=relpoint(g,0), discontPoint1=relpoint(g,1);
transform tr;
for(int i=-3;i<3;++i){
  tr=shift(pi*i,0);
  draw(tr*g,blue+0.7bp);
  dot(tr*(discontPoint0^^discontPoint1),UnFill);
}
clip(box((-3.2pi,-0.8pi^2/2), (3.2pi, 1.3pi^2)));
shipout(bbox(Fill(paleyellow)));

在此处输入图片描述

答案2

使用纯 TikZ 绘制:

\documentclass[tikz, margin=3mm]{standalone}
\usetikzlibrary{arrows.meta,
                bending}

\begin{document}
    \begin{tikzpicture}[
    xscale = 0.2,
    yscale = 0.1,
         > = {Straight Barb[scale=0.5, bend]},
lbl/.style = {font=\scriptsize, fill=white, inner sep=2pt}
                        ]
\draw[->]   (-3.5*pi,0) -- (4*pi, 0);
\draw[->]   (0,-2) -- (0,pi*pi+1);
\draw       (0.3,pi*pi) -- ++ (-0.6,0) node[lbl,left] {$\pi^2$};
                        
    \foreach \i [count=\j from -2] in {-3,-2,...,2}%
{
\draw (\i*pi,0.6) -- ++ (0,-1.2) 
    \ifnum\i=0
        node[lbl,below] {0};
    \else
        node[lbl,below] {$\i\pi$};
    \fi
\draw[blue, semithick, <->] 
    plot[domain=0:pi,samples=61,] (\i*pi +\x,\x*\x);
}
\draw (3*pi,0.6) -- ++ (0,-1.2) node[lbl,below] {$3\pi$};

    \end{tikzpicture}
\end{document}

在此处输入图片描述

答案3

您的转变方向是正确的。我猜最简单的方法是使用参数图,即分别针对 x 和 y 值的状态函数。

请注意,我还对您的代码做了一些其他改进(我在括号中进行了注释)。视觉效果还可以进一步改善,但我想如果您愿意的话,您可以自己完成。

% used PGFPlots v1.17
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
    \pgfplotsset{
        compat=1.17,
    }
\begin{document}
\begin{tikzpicture}
    \begin{axis}[
        width=12cm,
        height=4cm,
        axis lines=center,
        % (also show the 0 as xticklabel)
        hide obscured x ticks=false,
        % (align all ticklabels at the base)
        typeset ticklabels with strut,
        % (you can use mathematical expressions)
        xmin=-3*pi,
        xmax=3*pi,
        ymin=0,
        ymax=3*pi,
        xtick={-3*pi,-2*pi,...,3*pi},
        xticklabels={$-3\pi$,$-2\pi$,$-\pi$,$0$,$\pi$,$2\pi$,$3\pi$},
        ytick={3*pi},
        yticklabels={$\pi^2$},
        % don't show markers
        no markers,
        % limit computation of the function points to domain
        % (since no `samples` is given the default number of samples is used)
        domain=0:pi,
    ]
        % for simplicity use a loop for repetition
        \foreach \i in {-3,...,3} {
            % use parametric plots to simply shift the x values
            \addplot [blue,<->] (x-\i*pi, x^2);
        }
    \end{axis}
\end{tikzpicture}
\end{document}

该图显示了上述代码的结果

相关内容