2

I recently asked a question about how to use Tikz to create phase line diagrams. However I had a follow up question. The question is how I would go by creating a rational function or other types of functions during the plot portion of the code. The previous question is here - Is it possible to graph and draw phase lines in LaTex?


What I initially wanted was to use Tikz to draw a graph plotting a horizontal asymptote at I=1 as a dashed line. I also wanted to draw two curves. One curve which approached I=1 from above, and another curve that approached I=1 from below. I looked everywhere for hours, but could not find what I needed to help me do this. This is the output of the code below

What I ended up doing was just messing around with the coding until I moved the two curves, and dashed line to look like I wanted it. I am hoping there is a better way to do this, as it took me a very long time.

\documentclass{amsart}
\usepackage{fullpage}
\usepackage{amsmath}
\usepackage{amsthm}
\usepackage{graphicx}
\usepackage{caption}
\usepackage{epstopdf}
\usepackage{wrapfig}
\usepackage{pgfplots}
\usepackage{geometry}
\usepackage{amsmath}
\usepackage{pgfplots}
\pgfplotsset{compat=1.8}
\usepackage{mathtools}
\usepackage{tikz}

\newcommand*{\TickSize}{2pt}%

\newcommand*{\AxisMin}{0}%
\newcommand*{\AxisMax}{0}%

\newcommand*{\DrawHorizontalPhaseLine}[4][]{%
% #1 = axis tick labels
% #2 = right arrows positions as CSV
% #3 = left arrow positions as CSV
\gdef\AxisMin{0}%
\gdef\AxisMax{0}%
\edef\MyList{#2}% Allows for #1 to be both a macro or not
\foreach \X in \MyList {
    \draw  (\X,\TickSize) -- (\X,-\TickSize) node [below] {$\X$};
    \ifnum\AxisMin>\X
        \xdef\AxisMin{\X}%
    \fi
    \ifnum\AxisMax<\X
        \xdef\AxisMax{\X}%
    \fi
}

\edef\MyList{#3}% Allows for #2 to be both a macro or not
\foreach \X in \MyList {% Right arrows
    \draw [->] (\X-0.1,0) -- (\X,0);
    \ifnum\AxisMin>\X
        \xdef\AxisMin{\X}%
    \fi
    \ifnum\AxisMax<\X
        \xdef\AxisMax{\X}%
    \fi
}

\edef\MyList{#4}% Allows for #3 to be both a macro or not
\foreach \X in \MyList {% Left arrows
    \draw [<-] (\X-0.1,0) -- (\X,0);
    \ifnum\AxisMin>\X
        \xdef\AxisMin{\X}%
    \fi
    \ifnum\AxisMax<\X
        \xdef\AxisMax{\X}%
    \fi
}

\draw  (\AxisMin-1,0) -- (\AxisMax+1,0) node [right] {#1};
}%

\newcommand*{\DrawVerticalPhaseLine}[4][]{%
% #1 = axis tick labels
% #2 = up arrows positions as CSV
% #3 = down arrow positions as CSV
\gdef\AxisMin{0}%
\gdef\AxisMax{0}%
\edef\MyList{#2}% Allows for #1 to be both a macro or not
\foreach \X in \MyList {
    \draw  (-\TickSize,\X) -- (\TickSize,\X) node [right] {$\X$};
    \ifnum\AxisMin>\X
        \xdef\AxisMin{\X}%
    \fi
    \ifnum\AxisMax<\X
        \xdef\AxisMax{\X}%
    \fi
}

\edef\MyList{#3}% Allows for #2 to be both a macro or not
\foreach \X in \MyList {% Up arrows
    \draw [->] (0,\X-0.1) -- (0,\X);
    \ifnum\AxisMin>\X
        \xdef\AxisMin{\X}%
    \fi
    \ifnum\AxisMax<\X
        \xdef\AxisMax{\X}%
    \fi
}

\edef\MyList{#4}% Allows for #3 to be both a macro or not
\foreach \X in \MyList {% Down arrows
    \draw [<-] (0,\X+0.1) -- (0,\X);
    \ifnum\AxisMin>\X
        \xdef\AxisMin{\X}%
    \fi
    \ifnum\AxisMax<\X
        \xdef\AxisMax{\X}%
    \fi
}

\draw  (0,\AxisMin-1) -- (0,\AxisMax+1) node [above] {#1};
}%
\begin{document} 

\begin{tikzpicture}
  \draw[->] (0,0) -- (8,0) node[right] {$t$};
  \draw[->] (0,0) -- (0,5) node[above] {$I$};
   \draw[scale=0.5,domain=0:4,smooth,variable=\y,red]  plot ({\y*\y},{-   \y+8.1});  
   \draw[scale=0.5,domain=0:4,smooth,variable=\y,red]  plot ({\y*\y},{\y});  
  \draw[scale=0.5,domain=0:16,dashed,variable=\y,black] plot({\y},{4.045})node[right] {$I=1$};
 \end{tikzpicture}
  \end{document}
3

As this is a graph and not a picture, I would recommend you use pgfplots instead of just tike:

enter image description here

Code:

\documentclass[border=2pt]{standalone}
\usepackage{pgfplots}

%% http://tex.stackexchange.com/questions/17438/how-to-properly-scale-a-tikz-pgf-picture-which-has-a-beginaxis-endaxis
\pgfkeys{/pgfplots/Axis Labels At Tip/.style={
        xlabel style={
            at={(current axis.right of origin)}, 
            anchor=west,
        },
        ylabel style={
            at={(current axis.above origin)}, 
            yshift=1.5ex, anchor=center
        }
    }
}


\begin{document}
\begin{tikzpicture}
\begin{axis}[
    scale=0.5, 
    smooth, 
    xmax=16,
    ymax=8.5,
    axis x line=middle, 
    axis y line=center,
    ylabel=$I$,
    xlabel=$t$,
    axis on top=true,
    Axis Labels At Tip,
    clip=false,
    ]
  \addplot[domain=0:4, red] ({\x*\x},{-   \x+8.1});
  \addplot[domain=0:4, red] ({\x*\x},{\x});
  \addplot[domain=0:16, dashed, black] (\x,4.045) node[right] {$I=1$};
\end{axis}
\end{tikzpicture}
\end{document}
  • Well the issue is I am working with many different equations that have arbitrary values which are not given. I simply know that one is a parabola opening downwards with two zeros at x=0 and x=1. Now I don't particularly care what the maximum value of the parabola is, as long as it crossed at those two points and points downwards. I am more worried about drawing phase lines and solution curves as you can see in the OP as well as the linked OP. So I don't really have any values to graph, I just know what I want generally, if that makes any sense at all. I used random numbers to get the graph. – H5159 Dec 8 '15 at 6:47
  • 2
    Well you are going to have to either do the math or program the math in a way that you get the desired results. Without a clearer explanation of exactly what you want to specify and what the desired results are it is be difficult to help further. Nevertheless, I would strongly recommend that if you are doing graphs use pgfplots and not tikz directly. If it is specifying the domain you are worried about, you could let pgfplots figure it out on the first pass and then adjust it on the second pass to get the desired results. – Peter Grill Dec 8 '15 at 6:54

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.