Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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.

share|improve this question
7  
Welcome to TeX.SE. Please have a look at What is the clearest way to graph a piecewise function?, and let us know if that provides a solution for you. If you can not get exactly what you want with the answers provided there, please post what you have so far and then others here can help you with a specific issue with this. –  Peter Grill Oct 21 '12 at 1:40

2 Answers 2

up vote 4 down vote accepted

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

share|improve this answer
    
Very nice gonzalo, thanks! –  Lochlan Boils Oct 21 '12 at 4:12
    
How can i change closed end point to left and open circle to right? –  Lochlan Boils Oct 21 '12 at 4:16
    
@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?. –  Gonzalo Medina Oct 21 '12 at 4:19
    
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 –  Lochlan Boils Oct 21 '12 at 5:09
    
@LochlanBoils which file exactly can't be found? Please provide the complete error message you are getting. –  Gonzalo Medina Oct 21 '12 at 5:13

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|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.