1

I am trying to draw a diagram of mesh points for Forward time difference method (\frac{u_{i}^{n+1}-u_{i}^{n}}{\Delta t}) in TikZ but I am unable to draw it. I have only drawn coordinates by using

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

I need it urgently to finalize my thesis.

Alan
  • 11

1 Answers1

4

I'm not sure of what you want to obtain, something like a plot that shows how a forward difference method works?

enter image description here

To get the intersections between the straight line and the curve I used Jake's code in Intersections in PGFplots.

As you can see in the code below the coordinates of the labels u_{n+1}, u_n and so on are set by eye, since I could not find an automatic way.

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

EDIT: Finally, I've found a way of positioning the nodes automatically aligned (TiKZ Node positioning left of A but below B). This is the improved code, where the lines with \Delta u and \Delta t are correctly drawn:

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

That produces:

enter image description here

MarcoG
  • 1,437