科学工作场所分段常数函数

科学工作场所分段常数函数

我想绘制一个分段常数函数。即 y 轴上为 S(t),x 轴上为时间。S(t) 的值是任意的,我只想要一个时间分区为 1/10 的分段常数函数来使用:

S(t)=10,其中 0≤t<1/10

S(t)=3,其中 1/10≤t<2/10

依此类推,直到 t=1。

我想要在左端点处有一个闭合圆,在右端点处有一个开口圆来表示不连续性,即 t=0 处有一个开口圆,当 S(t)=10 时,t=1/10 处有一个闭合圆,以及一条连接两点的线,当 S(t)=3 时,t=1/10 处有一个开口圆,t=2/10 处有一个开口圆,等等。我不知道该怎么做。

答案1

另一种选择是使用pgfplots

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.6}

\pgfplotsset{soldot/.style={color=blue,only marks,mark=*}} \pgfplotsset{holdot/.style={color=blue,fill=white,only marks,mark=*}}

\begin{document}

\begin{tikzpicture}
\begin{axis}[xlabel=$t$,ylabel=$S(t)$]
\foreach \start/\value [evaluate=\start as \end using \start+0.1] 
  in {0/10,0.1/3,0.2/5,0.3/0,0.4/2,0.5/-3,0.6/4,0.7/5,0.8/1,0.9/-1}
\addplot[domain=\start:\end,blue] {\value};
\addplot[holdot] coordinates{(0,10)(0.1,3)(0.2,5)(0.3,0)(0.4,2)(0.5,-3)(0.6,4)(0.7,5)(0.8,1)(0.9,-1)};
\addplot[soldot] coordinates{(0.1,10)(0.2,3)(0.3,5)(0.4,0)(0.5,2)(0.6,-3)(0.7,4)(0.8,5)(0.9,1)(1,-1)};
\end{axis}
\end{tikzpicture}

\end{document}

在此处输入图片描述

评论中请求的更改:

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.6}

\pgfplotsset{soldot/.style={color=blue,only marks,mark=*}} \pgfplotsset{holdot/.style={color=blue,fill=white,only marks,mark=*}}

\begin{document}

\begin{tikzpicture}
\begin{axis}[xlabel=$t$,ylabel=$S(t)$]
\foreach \start/\value [evaluate=\start as \end using \start+0.1] 
  in {0/10,0.1/3,0.2/5,0.3/0,0.4/2,0.5/-3,0.6/4,0.7/5,0.8/1,0.9/-1}
\addplot[domain=\start:\end,blue] {\value};
\addplot[soldot] coordinates{(0,10)(0.1,3)(0.2,5)(0.3,0)(0.4,2)(0.5,-3)(0.6,4)(0.7,5)(0.8,1)(0.9,-1)};
\addplot[holdot] coordinates{(0.1,10)(0.2,3)(0.3,5)(0.4,0)(0.5,2)(0.6,-3)(0.7,4)(0.8,5)(0.9,1)(1,-1)};
\end{axis}
\end{tikzpicture}

\end{document}

在此处输入图片描述

答案2

这应该可以作为您的起点。我对所有内容进行了硬编码,因此看起来有些简单(尽管很笨重)。

\documentclass[border=3pt]{standalone}
\usepackage{amsmath}
\usepackage{pgfplots}
\usetikzlibrary{calc}

\begin{document}
\begin{tikzpicture}
  \begin{axis}[xlabel=Time $\rightarrow$,ylabel=$S(t) \rightarrow$]
    \foreach \xStart/\xEnd  in {0/.1, .2/.3, .4/.5, .6/.7,.8/.9} {
        \addplot[domain=\xStart:\xEnd, blue, samples=10, ultra thick] {10};
    }   
    %draw discontinuos lines
    \draw [draw=red, dotted, thin] (axis cs: .1, 10) -- (axis cs: .1, 3);
    \draw [draw=red, dotted, thin] (axis cs: .2, 10) -- (axis cs: .2, 3);
    \draw [draw=red, dotted, thin] (axis cs: .3, 10) -- (axis cs: .3, 3);
    \draw [draw=red, dotted, thin] (axis cs: .4, 10) -- (axis cs: .4, 3);
    \draw [draw=red, dotted, thin] (axis cs: .5, 10) -- (axis cs: .5, 3);
    \draw [draw=red, dotted, thin] (axis cs: .6, 10) -- (axis cs: .6, 3);
    \draw [draw=red, dotted, thin] (axis cs: .7, 10) -- (axis cs: .7, 3);
    \draw [draw=red, dotted, thin] (axis cs: .8, 10) -- (axis cs: .8, 3);
    \draw [draw=red, dotted, thin] (axis cs: .9, 10) -- (axis cs: .9, 3);
    % Show discontinuty points
    \draw [draw=blue, fill=white, thick] (axis cs: .1, 10) circle (2.0pt);
    \draw [draw=blue, fill=white, thick] (axis cs: .3, 10) circle (2.0pt);
    \draw [draw=blue, fill=white, thick] (axis cs: .5, 10) circle (2.0pt);
    \draw [draw=blue, fill=white, thick] (axis cs: .7, 10) circle (2.0pt);
    \draw [draw=blue, fill=white, thick] (axis cs: .9, 10) circle (2.0pt);
    \foreach \xStart/\xEnd  in {.1/.2, .3/.4, .5/.6, .7/.8,.9/1} {
        \addplot[domain=\xStart:\xEnd, blue, samples=10, ultra thick] {3};
    }
    % Show discontinuty points
    \draw [draw=blue, fill=blue, thick] (axis cs: .1, 3) circle (2.0pt);
    \draw [draw=blue, fill=blue, thick] (axis cs: .3, 3) circle (2.0pt);
    \draw [draw=blue, fill=blue, thick] (axis cs: .5, 3) circle (2.0pt);
    \draw [draw=blue, fill=blue, thick] (axis cs: .7, 3) circle (2.0pt);
    \draw [draw=blue, fill=blue, thick] (axis cs: .9, 3) circle (2.0pt);    
  \end{axis}
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容