3

How to plot this the streamlines xy=C by latex? I attached my code but it does not generate the wanted figure.

enter image description here

this is my code

\documentclass[tikz,border=10pt,multi]{standalone}
\usetikzlibrary{shapes.symbols}

\makeatletter

\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\makeatother



    \begin{document}

    \begin{tikzpicture}
    \begin{axis}[
        axis lines = left,
        xlabel = $x$,
        ylabel = {$f(x)$},
    ]
    %Below the red is defined
    \addplot [
        domain=-2:2, 
       samples=20, 
        color=red,
    ]
    { 1/x};
    \addlegendentry{$xy=-1$}
    %Here  is defined
    \addplot [
        domain=-2:2, 
        samples=20, 
        color=green,
        ]
        {-3/x};
    \addlegendentry{$xy=3$}
    \addplot [
        domain=-2:2, 
        samples=20, 
        color=green,
        ]
        {5/x};
    \addlegendentry{$xy=-5$}

    \end{axis}
    \end{tikzpicture}

    \end{document}
3

Here I present a solution using non-linear spacing that also works for quite low values for C or "high" values for Max (see code). For example test with values 0.1, 0.2 and 0.3 respectively or a Max value of 10. They work perfectly fine using only 22 samples per (full) line.

For details on how the solution works, please have a look at the comments in the code.

% used PGFPlots v1.16
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
    % load library to use a "cool" `cycle list'
    \usetikzlibrary{
        pgfplots.colorbrewer,
    }
    \pgfplotsset{
        % use this `compat' level or higher to use the advanced positioning
        % for the axis labels
        compat=1.3,
        /pgf/declare function={
            % declare a variable to store the min and max value for the function
            Max=5;
            % declare the function to use
            f(\C,\x) = \C/\x;
            %
            % -----------------------------------------------------------------
            % calculate the lower and upper boundaries (the domain values)
            lb(\C) = sqrt(\C);
            ub = Max;
            %
            %%% non-linear spacing: <https://stackoverflow.com/a/39140096/5776000>
            % "non-linearity factor"
            a = 1.0;
            % function to use for the non-linear spacing
            Y(\x) = exp(a*\x);
            % rescale to former limits
            X(\C,\x)  = (Y(\x) - Y(lb(\C)))/(Y(ub) - Y(lb(\C))) * (ub - lb(\C)) + lb(\C);
            % -----------------------------------------------------------------
        },
        % load a "cool" cycle list
        cycle list/Blues-5,
    }
\begin{document}
\begin{tikzpicture}[
    % define a style for the plot labels
    Label/.style={
        node font=\tiny,
        black,
        pos=#1,
    },
]
    \begin{axis}[
        % axis lines should be centered
        axis lines=center,
        % set axis labels
        xlabel={$x$},
        ylabel={$f(x)$},
        % set them right and above the axis lines, respectively
        xlabel style={
            at={(xticklabel* cs:1)},
            anchor=west,
        },
        ylabel style={
            at={(yticklabel* cs:1)},
            anchor=south,
        },
        % axis unit ratio should be the same for both axis lines
        axis equal image=true,
        % don't show any ticks (and tick labels)
        ticks=none,
        % the lines should be `smooth'
        smooth,
        % when using the non-linear spacing approach you don't need that much samples
        samples=11,
%        % ---------------------------------------------------------------------
%        % for debugging purposes only
%        % ---------------------------------------------------------------------
%        % add small marks to the lines
%        every axis plot post/.append style={
%            mark=*,
%            mark size=0.3pt,
%        },
%        % ---------------------------------------------------------------------
    ]

        % first draw the lines for $C = 0$
        \pgfplotsinvokeforeach {0} {
            % the horizontal line
            % used `forget plot' so the `cycle list index' isn't increased
            \addplot+ [domain=-Max:Max,forget plot,>->] {#1}
                node [Label=0.4,above] {$C = #1$}
                node [Label=0.6,above] {$C = #1$}
            ;
            % the vertical line
            \addplot+ [domain=-Max:Max,>->] (#1,x)
                node [Label=0.25,left] {$#1$}
                node [Label=0.75,left] {$#1$}
            ;
        }

            % define a length for the shift of the plot labels
            % (using "above left" and similar causes the labels to be
            %  "too far away" from the plot they belong to and are almost
            %  *in* the next plot)
            \pgfmathsetlengthmacro{\Shift}{3pt}

            % define a factor to enlarge the domain to get the parabolic effect
            % of the end points of the different "C" plots
            % (for linear spacing use the value 0.25)
            \pgfmathsetmacro{\Factor}{0.01}

        % now draw plots for $C > 0$
        \pgfplotsinvokeforeach {1,2,3} {
            % to do so we split each line into two part
            % this is advantage because of the symmetry of the lines and
            % thus we can avoid the need for a lot of "samples" in the steep
            % parts of the curve.
            %
            % -----------------------------------------------------------------
            % linear spacing approach
            % -----------------------------------------------------------------
%            % quadrant I
%            \addplot+ [domain=+sqrt(#1):{Max+\Factor*#1},forget plot,->]  {f(#1,x)}
%                node [Label=0,shift={(\Shift,\Shift)}] {$+#1$};
%            % the steep parts can be drawn using a parametric plot
%            \addplot+ [domain=+sqrt(#1):{Max+\Factor*#1},forget plot,-<]  ({f(#1,x)},x);
%
%            % quadrant II
%            \addplot+ [domain={-Max-\Factor*#1}:-sqrt(#1),forget plot,>-] {-f(#1,x)}
%                node [Label=1,shift={(-\Shift,\Shift)}] {$-#1$};
%            \addplot+ [domain=+sqrt(#1):{Max+\Factor*#1},forget plot,->]  ({-f(#1,x)},x);
%
%            % quadrant III
%            \addplot+ [domain={-Max-\Factor*#1}:-sqrt(#1),forget plot,>-] {f(#1,x)}
%                node [Label=1,shift={(-\Shift,-\Shift)}] {$+#1$};
%            \addplot+ [domain={-Max-\Factor*#1}:-sqrt(#1),forget plot,<-] ({f(#1,x)},x);
%
%            % quadrant IV
%            \addplot+ [domain=+sqrt(#1):{Max+\Factor*#1},forget plot,->]  {-f(#1,x)}
%                node [Label=0,shift={(\Shift,-\Shift)}] {$-#1$};
%            \addplot+ [domain={-Max-\Factor*#1}:-sqrt(#1),>-]             ({-f(#1,x)},x);

            % -----------------------------------------------------------------
            % non-linear spacing approach
            % -----------------------------------------------------------------
            % quadrant I
            \addplot+ [domain=+sqrt(#1):Max+\Factor*(#1)^2,forget plot,->]  ({X(#1,x)},{f(#1,X(#1,x))})
                node [Label=0,shift={(\Shift,\Shift)}] {$+#1$};
            % the steep parts can be drawn using a parametric plot
            \addplot+ [domain=+sqrt(#1):Max+\Factor*(#1)^2,forget plot,-<]  ({f(#1,X(#1,x))},{X(#1,x)});

            % quadrant II
            \addplot+ [domain=+sqrt(#1):Max+\Factor*(#1)^2,forget plot,-<] ({-X(#1,x)},{f(#1,X(#1,x))})
                node [Label=0,shift={(-\Shift,\Shift)}] {$-#1$};
            \addplot+ [domain=+sqrt(#1):Max+\Factor*(#1)^2,forget plot,->]  ({-f(#1,X(#1,x))},{X(#1,x)});

            % quadrant III
            \addplot+ [domain=+sqrt(#1):Max+\Factor*(#1)^2,forget plot,-<] ({-X(#1,x)},{-f(#1,X(#1,x))})
                node [Label=0,shift={(-\Shift,-\Shift)}] {$+#1$};
            \addplot+ [domain=+sqrt(#1):Max+\Factor*(#1)^2,forget plot,->] ({-f(#1,X(#1,x))},-{X(#1,x)});

            % quadrant IV
            \addplot+ [domain=+sqrt(#1):Max+\Factor*(#1)^2,forget plot,->]  ({X(#1,x)},{-f(#1,X(#1,x))})
                node [Label=0,shift={(\Shift,-\Shift)}] {$-#1$};
            \addplot+ [domain=+sqrt(#1):Max+\Factor*(#1)^2,-<]             ({f(#1,X(#1,x))},{-X(#1,x)});
        }

    \end{axis}
\end{tikzpicture}
\end{document}

image showing the result of above code

1

@Barry -- There's a lot going on in your stagnation point flow, but here's a start, with lots more work on labels and arrowheads needed.

\documentclass[tikz,border=10pt,multi]{standalone}
\usetikzlibrary{shapes.symbols}

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

\begin{document}

\begin{tikzpicture}
\begin{axis}[
    axis x line=center,
    axis y line=center,
    xlabel = $x$,
    ylabel = {$y$},
    ymax=3,
    ymin=-3,
    ticks=none,
]
\node[right] at (-2,2) {$C=-2$};
\node[right] at (-0.7,0.2) {$C=0$};

%Below the red is defined
\addplot [
    domain=-2:-0.1, 
   samples=50, 
    color=red,
]
{1/x};
\addplot [
    domain=0.1:2, 
   samples=50, 
    color=red,
]
{(1/x)};  
\addplot [
    domain=-2:-0.1, 
   samples=50, 
    color=red,
]
{ -1/x};
\addplot [
    domain=0.1:2, 
   samples=100, 
    color=red,
]
{(-1/x)};

% Now the teal
   \addplot [
    domain=-2.:-0.1, 
   samples=50, 
    color=teal,
]
{(0.5/x)};
   \addplot [
    domain=0.1:2, 
   samples=50, 
    color=teal,
]
{(0.5/x)};
\addplot [
    domain=-2:-0.1, 
   samples=100, 
    color=teal,
]
{ -0.5/x};   
\addplot [
    domain=0.1:2, 
   samples=50, 
    color=teal,
]
{(-0.5/x)};

%Here green  is defined
    \addplot [
    domain=-2:-0.1, 
    samples=50, 
    color=green,
    ]
    {-2/x};
\addplot [
    domain=-2:-0.1, 
    samples=50, 
    color=green,
    ]
    {2/x};
\addplot [
    domain=0.1:2, 
    samples=50, 
    color=green,
    ]
    {-2/x};
\addplot [
    domain=0.1:2, 
    samples=50, 
    color=green,
    ]
    {-2/x};

 \end{axis}
\end{tikzpicture}

\end{document}

enter image description here

  • Does anyone know how to do the arrowheads? – Barry Jun 2 '18 at 2:05
  • There are some solutions in the questions on this board, but there may be other approaches. I did not have time to investigate in detail. – John Jun 2 '18 at 2:22
  • I do not know why the label $\node[right] at (-2,2) {$C=-2$}; \node[right] at (-0.7,0.2) {$C=0$}; does not appear when i generated the code. – Barry Jun 2 '18 at 6:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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