# How to plot a discontinuous function curve and diagram of Markov chain with TikZ?

Please apologize my ingnorance of tips to use for asking questions. I would like to realise the plots and the diagram like those shown on page 158,

160

and 161

on this document Chaine de Markov.

-
It would be good if you posted an image figure you are referring to, rather than posting a link to some other document. Exactly which part of those figures are you having difficulty with? Please post what you have tried so far. – Peter Grill Mar 27 '13 at 4:28
To draw the Markov chain you can have a look to the answers of How to draw graphs in Latex?. – Claudio Fiandrino Mar 27 '13 at 9:14
Could you consider to accept one or the other answer? You've been member for more than two years asking 15 questions and accepted only one answer even though some of them sound technically corrected and appears to be very useful. – Claudio Fiandrino Jun 13 '14 at 9:05

My solutions adopts a command \samplepath able to create a sample path with the following syntax:

\samplepath{+|0.5,+|0.25,-|1.5,+|1,+|0.5,+|0.75}


where

• +/- represent the vertical changes of the path;
• numbers represent the time duration of the samples.

Similarly, there is another command \discretesamplepath which uses the same syntax, but it represents samples in discrete time.

There are actually two methods available:

• the first one uses the package xstring with the syntax described above;
• the second method does not use that package and replaces the delimiter | with /.

The code with xstring:

\documentclass[png,border=10pt,tikz]{standalone}
\usepackage{xstring}
\usepackage{tikz}
\usetikzlibrary{calc}

\pgfkeys{/tikz/.cd,
vertical factor/.initial=0.5,
vertical factor/.get=\vertfactor,
vertical factor/.store in=\vertfactor,
start coordinate/.initial={0,\vertfactor},
start coordinate/.get=\startcoord,
start coordinate/.store in=\startcoord,
sample color/.initial=black,
sample color/.get=\samplecol,
sample color/.store in=\samplecol,
sample size/.initial=1pt,
sample size/.get=\samplesize,
sample size/.store in=\samplesize,
sample line width/.initial=very thick,
sample line width/.get=\samplelinewidth,
sample line width/.store in=\samplelinewidth,
}

\newcommand{\samplepath}[1]{%
\coordinate (start) at (\startcoord) ;
\foreach \samples[count=\xi from 1] in {#1}{%
\StrCut{\samples}{|}{\vertdir}{\hordir}
\ifnum\xi=1
\draw[\samplelinewidth,\samplecol](start)
--++(\hordir,0) coordinate (start);
\else
\IfStrEq{\vertdir}{+}{%true
\draw[\samplelinewidth,\samplecol]($(start)+(0,\vertfactor)$)
--++(\hordir,0)coordinate(start);
}{%false
\relax
}
\IfStrEq{\vertdir}{-}{%true
\draw[\samplelinewidth,\samplecol]($(start)+(0,-\vertfactor)$)
--++(\hordir,0)coordinate(start);
}{%false
\relax
}
\fi
}
}

\tikzset{sample/.style={
circle,
inner sep=\samplesize,
fill=\samplecol,
}
}

\newcommand{\discretesamplepath}[1]{%
\coordinate (start) at (\startcoord) ;
\foreach \samples[count=\xi from 1] in {#1}{%
\StrCut{\samples}{|}{\vertdir}{\hordir}
\ifnum\xi=1
\path(start)node[sample]{}
--++(\hordir,0) coordinate (start);
\else
\IfStrEq{\vertdir}{+}{%true
\path($(start)+(0,\vertfactor)$)node[sample]{}
--++(\hordir,0)coordinate(start);
}{%false
\relax
}
\IfStrEq{\vertdir}{-}{%true
\path($(start)+(0,-\vertfactor)$)node[sample]{}
--++(\hordir,0)coordinate(start);
}{%false
\relax
}
\fi
}
}

\begin{document}
\begin{tikzpicture}
% axis
\draw[-stealth] (0,-1)--(0,4) node[left]{$X(t)$};
\draw[-stealth] (-1,0)--(5,0) node[below]{$t$};
\samplepath{+|0.5,+|0.25,-|1.5,+|1,+|0.5,+|0.75}
\end{tikzpicture}
\end{document}


The code without xstring:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}

\def\up{+}
\def\down{-}

\pgfkeys{/tikz/.cd,
vertical factor/.initial=0.5,
vertical factor/.get=\vertfactor,
vertical factor/.store in=\vertfactor,
start coordinate/.initial={0,\vertfactor},
start coordinate/.get=\startcoord,
start coordinate/.store in=\startcoord,
sample color/.initial=black,
sample color/.get=\samplecol,
sample color/.store in=\samplecol,
sample size/.initial=1pt,
sample size/.get=\samplesize,
sample size/.store in=\samplesize,
sample line width/.initial=very thick,
sample line width/.get=\samplelinewidth,
sample line width/.store in=\samplelinewidth,
}

\newcommand{\samplepath}[1]{%
\coordinate (start) at (\startcoord) ;
\foreach \vertdir/\hordir[count=\xi from 1] in {#1}{%
\ifnum\xi=1
\draw[\samplelinewidth,\samplecol](start)
--++(\hordir,0) coordinate (start);
\else
\ifx\vertdir\up%true
\draw[\samplelinewidth,\samplecol]($(start)+(0,\vertfactor)$)
--++(\hordir,0)coordinate(start);
\else
\relax
\fi
\ifx\vertdir\down%true
\draw[\samplelinewidth,\samplecol]($(start)+(0,-\vertfactor)$)
--++(\hordir,0)coordinate(start);
\else
\relax
\fi
\fi
}
}

\tikzset{sample/.style={
circle,
inner sep=\samplesize,
fill=\samplecol,
}
}

\newcommand{\discretesamplepath}[1]{%
\coordinate (start) at (\startcoord) ;
\foreach \vertdir/\hordir[count=\xi from 1] in {#1}{%
\ifnum\xi=1
\path(start)node[sample]{}
--++(\hordir,0) coordinate (start);
\else
\ifx\vertdir\up%true
\path($(start)+(0,\vertfactor)$)node[sample]{}
--++(\hordir,0)coordinate(start);
\else
\relax
\fi
\ifx\vertdir\down%true
\path($(start)+(0,-\vertfactor)$)node[sample]{}
--++(\hordir,0)coordinate(start);
\relax
\relax
\fi
\fi
}
}

\begin{document}
\begin{tikzpicture}
% axis
\draw[-stealth] (0,-1)--(0,4) node[left]{$X(t)$};
\draw[-stealth] (-1,0)--(5,0) node[below]{$t$};
\samplepath{+/0.5,+/0.25,-/1.5,+/1,+/0.5,+/0.75}
\end{tikzpicture}
\end{document}


The result for both cases:

Some keys have been defined in order to customize the aspect: it is possible to use them via \tikzset or as option of the tikzpicture. In the following examples some of them will be used. Please, notice that these examples are with the method exploiting xstring.

First example:

\begin{tikzpicture}[start coordinate={0,0},font=\footnotesize]
\begin{scope}
% axis
\draw[-stealth] (0,0)--(4.5,0) node[below]{$t$};
\node at (0,1) {$X(t)$};
\samplepath{+|0.5,+|0.25,-|1,+|0.15,-|1.5,+|0.3,-|0.75}
\node at (4.5,1) {$0<\lambda \ll \mu$};
\end{scope}
\begin{scope}[yshift=-2cm]
% axis
\draw[-stealth] (0,0)--(4.5,0) node[below]{$t$};
\node at (0,1) {$X(t)$};
\samplepath{+|0.55,+|0.75,-|0.65,+|1.5,-|1}
\node at (4.5,1) {$0<\lambda = \mu$};
\end{scope}
\begin{scope}[yshift=-4cm]
% axis
\draw[-stealth] (0,0)--(4.5,0) node[below]{$t$};
\node at (0,1) {$X(t)$};
\samplepath{+|0.2,+|0.4,-|0.15,+|0.4,-|0.3,+|0.2,
-|0.2,+|0.2,-|0.4,+|0.2,-|0.3,+|0.5,-|0.2,+|0.4,-|0.4}
\node at (4.5,1) {$0\ll \lambda = \mu$};
\end{scope}
\end{tikzpicture}


Result:

Second example:

\begin{tikzpicture}[font=\footnotesize]
\begin{scope}[start coordinate={0,0}]
% axis
\draw[-stealth] (0,0)--(4.5,0) node[above]{$t$};
\node at (0,2) {$X(t)$};
\samplepath{+|0.5,+|1,+|0.75,-|0.5,-|0.25,+|0.35,+|0.4,+|0.55}
\end{scope}
\begin{scope}[yshift=-3cm,start coordinate={0.25,0},sample color=blue,sample size=1.25pt]
% axis
\draw[-stealth] (0,0)--(4.5,0) node[above]{$n$};
\node at (0,2) {$\hat{X}(n)$};
\discretesamplepath{+|0.5,+|1,+|0.75,-|0.5,-|0.25,+|0.35,+|0.4,+|0.55}
\end{scope}
\end{tikzpicture}


Result:

-

Another piece of the puzzle:

Which was produced by the macro \brokenline I wrote. This macro draws one of those graphs, and takes four arguments. The first one is the vertical distance among the "steps" of the graph. The second one is a comma delimited list of the lengths of each segment. The third and fourth are the labels to appear at the left and right of the graph.

This is the complete code:

\documentclass{article}
\usepackage{tikz}
\usepackage{calc}
\usetikzlibrary{calc}

\def\brokenline#1#2#3#4{
\coordinate (prev) at (0,0);
\foreach \x [count=\i] in {#2} {
\draw[very thick] (prev) -- +(\x, 0) coordinate (cur);
\ifodd\i\def\gap{#1}\else\def\gap{-#1}\fi
\coordinate (prev) at ($(cur)+(0,\gap)$);
}
\draw[thin] (0,0) -- (prev|-0,0) node[right] (t) {$t$};
\draw[very thick,->] (prev|-0,0)  +(-1pt,0) -- +(1pt,0);
\draw (0,#1) node[above] {#3}
(t|-0,#1) node[above left, inner xsep=0pt] {#4};
}

\begin{document}
\begin{tikzpicture}[>= stealth, xscale=1.5]
\brokenline{0.5}{0.5, 0.3, 1, 0.2, 2, 0.4, 1.6}
{$X(t)$}{$0<\lambda\ll\mu$}
\end{tikzpicture}

\vskip5mm

\begin{tikzpicture}[>= stealth, xscale=1.5]
\brokenline{0.5}{0.5, 1.2, 0.8, 2, 1.5}
{$X(t)$}{$0<\lambda=\mu$}
\end{tikzpicture}

\vskip5mm

\begin{tikzpicture}[>= stealth, xscale=1.5]
\brokenline{0.5}{0.3, 0.4, 0.1, 0.4, 0.25, 0.2, 0.45, 0.2, 0.25, 0.45, 0.05, 0.1, 0.1, 0.5, 0.3, 0.1, 0.2, 0.2, 0.3, 0.3, 0.85}
{$X(t)$}{$0\ll\lambda=\mu$}
\end{tikzpicture}

\end{document}

-
Thanks for your help! – Zbigniew Mar 28 '13 at 11:04
Please tex.stackexchange.com/users/13304/claudio-fiandrino I can't run the first scipt. – Zbigniew Mar 28 '13 at 11:06
there is the error code: – Zbigniew Mar 28 '13 at 11:07
runsystem(latex -jobname 'ChainedeMarkov1' '\expandafter \def \csname sa@inter nal@run\endcsname {1}\input {ChainedeMarkov1}')...quotation error in system co mmand. runsystem(dvips ChainedeMarkov1.dvi)...disabled (restricted). runsystem(gswin32c -dSAFER -dBATCH -dNOPAUSE -sDEVICE=png16m -r300 -sOutputFile =ChainedeMarkov1-%d.png Chainede*Markov1.ps)...disabled (restricted). – Zbigniew Mar 28 '13 at 11:08
I apologizehttp://tex.stackexchange.com/users/12571/jldiaz and thank you for your help. Notify you that I am on Windows 7 and I use Miketex with WinEdt. I have changed the file name in vain. – Zbigniew Mar 28 '13 at 11:56