Scientific workplace piecewise constant function

1,337

Solution 1

Another option using 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}

enter image description here

A change requested in a comment:

\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}

enter image description here

Solution 2

This should serve as a starting point for you. I have hard coded every thing so that it will look some what simple (though bulky).

\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}

enter image description here

Share:
1,337

Related videos on Youtube

Lochlan Boils
Author by

Lochlan Boils

Updated on October 21, 2020

Comments

  • Lochlan Boils
    Lochlan Boils about 3 years

    I want to sketch a piecewise constant function. Ie S(t) on y axis, time on x axis. The values of S(t) are arbitrary, I just want a piecewise constant function with time partition 1/10 to use:

    S(t)=10 for 0≤t<1/10

    S(t)=3 for 1/10≤t<2/10

    and so forth up to t=1.

    I want to have a closed circle at the left endpoint and an open one at the right endpoint to indicate the discontinuity, ie open circle at t=0, closed circle at t=1/10 for S(t)=10,and a line connecting the two points, open circle at t=1/10 for S(t)=3 open cicle at t=2/10 and so forth. I can't figure out how to do this.

  • Lochlan Boils
    Lochlan Boils about 11 years
    How can i change closed end point to left and open circle to right?
  • Gonzalo Medina
    Gonzalo Medina about 11 years
    @LochlanBoils You're welcome. Regarding your question, interchange soldot and holdot in my example code. Also, don't forget to accept (different from up-voting) the answer that you consider solved best your problem; in case of doubt, see How do you accept an answer?.
  • Lochlan Boils
    Lochlan Boils about 11 years
    I changed it but am having a problem with texnicentre which i just reinstalled, it wont compile pdf, keep getting error that the file could not be found, presume it is a problem with directory but i have configured it as i always have
  • Gonzalo Medina
    Gonzalo Medina about 11 years
    @LochlanBoils which file exactly can't be found? Please provide the complete error message you are getting.
  • Lochlan Boils
    Lochlan Boils about 11 years
    when i build there are no errors. When i go to view output i get the message: "There was an error opening this document the file cannot be found''
  • Gonzalo Medina
    Gonzalo Medina about 11 years
    @LochlanBoils this seems to be a problem between your editor and your viewer (not directly related to LaTeX), Unfortunately, I don't use TeXnicCenter so I can'y help you here more. However, check if you can open the .pdf file going to the file where it resides and if the result is expected.
  • Lochlan Boils
    Lochlan Boils about 11 years
    No worries i'll figure it out, if you could post the output of the graph for now that would be great
  • Gonzalo Medina
    Gonzalo Medina about 11 years
    @LochlanBoils sure! I've updated my answer.