如何用TikZ绘制不连续函数曲线和马尔可夫链图?

如何用TikZ绘制不连续函数曲线和马尔可夫链图?

请原谅我对提问技巧的无知。我希望实现第 158 页所示的情节和图表,

在此处输入图片描述

160 在此处输入图片描述

和 161 在此处输入图片描述

关于本文件马尔可夫链

欢迎任何回答。

答案1

我的解决方案采用了一个能够创建示例路径的命令\samplepath,其语法如下:

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

在哪里

  • +/-表示路径的垂直变化;
  • 数字代表样本的时间持续时间。

类似地,还有另一个命令\discretesamplepath使用相同的语法,但它表示离散时间的样本。

实际上有两种方法可用:

  • 第一个使用包xstring使用上面描述的语法;
  • 第二种方法不使用该包,而是用 替换分隔|/

代码如下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}

代码不包含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}

两种情况的结果:

在此处输入图片描述

为了自定义方面,我们定义了一些键:可以通过\tikzset或 作为 的选项来使用它们tikzpicture。以下示例中将使用其中一些。请注意,这些示例使用利用方法xstring

第一个例子:

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

结果:

在此处输入图片描述

第二个例子:

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

结果:

在此处输入图片描述

答案2

另一个谜团是:

断线

这是由我编写的宏生成的\brokenline。该宏绘制其中一个图形,并采用四个参数。第一个参数是图形“步骤”之间的垂直距离。第二个参数是每个段的长度的逗号分隔列表。第三和第四个参数是显示在图形左侧和右侧的标签。

这是完整的代码:

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

相关内容