如何在 TikZ 中绘制前向时间差图?

如何在 TikZ 中绘制前向时间差图?

我正在尝试绘制前向时间差分法的网格点图(\frac{u_{i}^{n+1}-u_{i}^{n}}{\Delta t}我正在尝试绘制Ti 中的Z,但我无法绘制它。我只能使用

\begin{document}
\usepackage{tikz}

\begin{tikzpicture}[scale=1.5]
\draw[step=1cm,gray,very thin] (-2.9,-2.9) grid (4.9,4.9);
\draw[black,thick,] (-2,0) -- (3,0)node [black, very thick]{x };
\draw[black,thick,] (0,0) -- (0,4) node {t };

\end{tikzpicture}

\end{document}

我急需它来完成我的论文。

答案1

我不确定你想要获得什么,像显示前向差分方法如何工作的图表之类的东西?

在此处输入图片描述

为了得到直线和曲线之间的交点,我使用了 Jake 的代码PGFplot 中的交叉点

正如您在下面的代码中看到的u_{n+1},标签的坐标u_n等都是用眼睛设置的,因为我找不到自动的方法。

\documentclass{standalone}

\usepackage{amsmath}

\usepackage{tikz}
\usetikzlibrary{intersections}

\usepackage{pgfplots}
\pgfplotsset{compat=1.12}

\begin{document}

\begin{tikzpicture}
\begin{axis}[%
title={Forward difference method},
ticks=none,
clip=false,
xlabel={$t$},
xmin=0,
xmax=10.5,
every axis x label/.style={at={(current axis.right of origin)},anchor=north west},
ylabel={$u$},
ymin=0,
ymax=5.75,
every axis y label/.style={at={(current axis.above origin)},anchor=north east},
axis x line=bottom,
axis y line = left]

%%%% Plot
\draw[name path global=CurveLine] (0,1) .. controls (5,1) and (9,3) .. (10,5);

\addplot [name path global=StraightLine] coordinates{(1,0.9) (9,3.5)};

\fill [name intersections={of=StraightLine and CurveLine, name=i, total=\t}][black] 
    \foreach \s in {1,...,\t}{(i-\s) circle (1.25pt)
        node (\s) {}};

%%%% Dashed lines and labels
\begin{scope}[dashed,very thin]

\draw (1) -- node[pos=1,below](a){$t_n$}++ (0,-1.5);
\draw (2) -- node[pos=1,below](b){$t_{n+1}$}++ (0,-3.73);


\draw (1) -- node[pos=1,left](c){$u_n$}++ (-2.5,0);
\draw (2) -- node[pos=1,left](d){$u_{n+1}$}++ (-9.15,0);

\end{scope}

\draw[<->] (a) --node[midway,fill=white]{$\Delta t$} (b);
\draw[<->] (c) --node[midway,fill=white]{$\Delta u$} (d);

\node at (4.5,4.5) {$\dfrac{\mathrm{d}u}{\mathrm{d}t}\approx\dfrac{\Delta u}{\Delta t}=\dfrac{u_{n+1}-u_n}{t_{n+1}-t_n}$};

\end{axis}
\end{tikzpicture}

\end{document}

编辑:最后,我找到了一种自动对齐节点的方法(TiKZ 节点位于 A 左侧但在 B 下方\Delta u)。这是改进后的代码,其中带有和的线条\Delta t已正确绘制:

\documentclass{standalone}

\usepackage{amsmath}

\usepackage{tikz}
\usetikzlibrary{intersections,positioning}

\usepackage{pgfplots}
\pgfplotsset{compat=1.12}

\begin{document}

\begin{tikzpicture}
\begin{axis}[%
title={Forward difference method},
ticks=none,
clip=false,
xlabel={$t$},
xmin=0,
xmax=10.5,
every axis x label/.style={at={(current axis.right of origin)},anchor=north west},
ylabel={$u$},
ymin=0,
ymax=5.75,
every axis y label/.style={at={(current axis.above origin)},anchor=north east},
axis x line=bottom,
axis y line = left]

%%%% Plot
\draw[name path global=CurveLine] (0,1) .. controls (5,1) and (9,3) .. (10,5);

\addplot [name path global=StraightLine] coordinates{(1,0.9) (9,3.5)};

\fill [name intersections={of=StraightLine and CurveLine, name=i, total=\t}][black] 
    \foreach \s in {1,...,\t}{(i-\s) circle (1.25pt)
        node (\s) {}};

%%%% Dashed lines and labels
\begin{scope}[dashed,very thin]

    \node[below=1.25cm of 1] (a) {$t_n$};
    \node (b) at (a -| 2) {$t_{n+1}$};

    \draw (1) -- (a);
    \draw (2) -- (b);

    \node[left=1.2cm of 1] (c) {$u_n$};
    \node (d) at (c |- 2) {$u_{n+1}$};

    \draw (1) -- (c);
    \draw (2) -- (d);

\end{scope}

\draw[<->] (a) --node[midway,fill=white]{$\Delta t$} (b);
\draw[<->] (c) --node[midway,fill=white]{$\Delta u$} (d);

\node at (4.5,4.5) {$\dfrac{\mathrm{d}u}{\mathrm{d}t}\approx\dfrac{\Delta u}{\Delta t}=\dfrac{u_{n+1}-u_n}{t_{n+1}-t_n}$};

\end{axis}
\end{tikzpicture}

\end{document}

得出的结果为:

在此处输入图片描述

相关内容