# Is it possible to graph and draw phase lines in LaTex?

I'm working with modeling infectious diseases with compartmental models using differential equations. I want to do some qualitative analysis for my rate of infected individuals. Now, I know that it is a parabola opening downwards, and I can graph this easily because the intercepts are at 0 and 1. However, I want to be able to draw phase line on the x-axis, making the 0 unstable and 1 stable. I know of ways to draw the phase line, and to draw a graph, but I am unaware of how to combine them.

I'm also working with a lot of arbitrary variables.

Here is some clarification,

These are the phase lines I am speaking of, however I want to add the parabola or whatever function to it as well.

Drawing phase line for differential equations

Edit: Code that seems to not work, I would like to know what the error is, as I don't understand it. If I ignore the errors in the compiler, it will give me what I want, but I'd rather not have errors.

\documentclass[border=2pt]{standalone}
\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};
}%

\begin{document}
\begin{tikzpicture}[thick]
\DrawHorizontalPhaseLine[$I$]{-1,0,1,2}{.25,.5,.75}  {-.25,-.5,-.75,1.25,1.5,1.75}
\draw[domain=-0.5:1.5,smooth,variable=\x,red] plot ({\x},{-1*(\x-0.5)*(\x-0.5)+.25});
\end{tikzpicture}
\end{document}

• This is very vague. Can you provide some concrete examples of what you're looking for? Visuals? – Werner Dec 4 '15 at 4:50
• @Werner Yes I have updated it. – H5159 Dec 4 '15 at 4:52
• @Frumpy In case you find my answer useful, I would kindly ask you to accept and/or upvote it such that your question no longer shows up as unanswered. Otherwise, please add any specific points that are left open. – cryingshadow Dec 6 '15 at 18:29
• Please post complete code rather than a fragment as it is much more useful. – cfr Dec 8 '15 at 3:11
• Understood, I will make a new question. – H5159 Dec 8 '15 at 4:09

Sometimes TikZ doesn't care about zeros and sometimes it does. Here, it does. The error complains of . and missing a number because it is not seeing a number starting with a decimal point as a number at all. The solution is to simply add the zeros explicitly:

\DrawHorizontalPhaseLine[$I$]{-1,0,1,2}{0.25,0.5,0.75}{-0.25,-0.5,-0.75,1.25,1.5,1.75}


Then TikZ sees the numbers between -1 and 1 as numbers, too, and happily parses the lists without complaint.

Complete code (mostly Peter Grill's):

\documentclass[border=10pt,tikz,multi]{standalone}

% Peter Grill's answer at https://tex.stackexchange.com/a/170057/

\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}[thick]
\DrawHorizontalPhaseLine[$I$]{-1,0,1,2}{0.25,0.5,0.75}{-0.25,-0.5,-0.75,1.25,1.5,1.75}
\draw [domain=-0.5:1.5,smooth,variable=\x,red] plot (\x,{-1*(\x-0.5)*(\x-0.5)+.25});
\end{tikzpicture}
\end{document}

• I had one last issue, it seems that I do not fully comprehend how the line that deal with our function works. Specifically, plot (\x,{-1*(\x-0.5)*(\x-0.5)+.25}); If I were to want to graph say, a cubic such as $3x^2+3x-2$, how would I go by this? Do the * symbols imply multiplication? It seems as if the  command no longer works on this platform. It used to work the last time I used Stack Exchange. – H5159 Dec 8 '15 at 3:44
• Does (\x,{3*(\x^2)+3*\x-2}) not work? – cfr Dec 8 '15 at 3:50
• Ah yes, it does work. Could you explain to me why we need to do \x and if we wanted to create a fraction, would we use the normal fraction command or is there something we specific we must do with tikz? – H5159 Dec 8 '15 at 3:51
• You are plotting coordinates of the form (<x>,<y>). In this case, the x coordinate is the value of the variable \x and the y coordinate is the value of the function of that variable f(\x). – cfr Dec 8 '15 at 3:53
• Yes. I think the idea is that on Maths SE, you are interested in the maths, whereas here we are interested in the code. So enabling MathJax wouldn't make sense here. If you get stuck, just ask a follow-up question, linking this one if you think it would be useful. – cfr Dec 8 '15 at 4:06

I'm still not sure what exactly you mean by the phase lines, but based on this answer by Peter Grill, I would just add the function to the phase line:

\documentclass[border=2pt]{standalone}
\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};
}%

\begin{document}
\begin{tikzpicture}[thick]
\DrawHorizontalPhaseLine[$x$]{0,2,4}{-0.5, 4.7}{1, 2.5}%
\draw[domain=-0.5:1.5,smooth,variable=\x,blue] plot ({\x},{-1*(\x-0.5)*(\x-0.5)+0.25});
\end{tikzpicture}
\end{document}


The added command \draw[domain=-0.5:1.5,smooth,variable=\x,blue] plot ({\x},{-1*(\x-0.5)*(\x-0.5)+0.25}); works as follows: The parameter domain specifies for what range of x-values the function should be plotted. The parameter smooth says that the function should be a smooth curve rather than a sequence of straight lines. The variable parameter defines the variable that is fed the values from domain. The last parameter defines the color to use when plotting the function. Then we define the actual function (you can read it like f(x) = -1*(x*0.5)*(x*0.5)+0.25 which is just a normal parabola with factor -1 shifted 0.5 to the right and 0.25 upwards -- this should be the parabola you described in your question). You can have a look in the pgf manual on page 279.

Does that solve your problem?

• I would like to understand how I can create the function, given your code. I don't see how that line that draws the parabola works. I tried to change around the values to figure out what each input does but I can't seem to figure it out. – H5159 Dec 4 '15 at 19:49
• I added an explanation of that command and a link to the corresponding manual. – cryingshadow Dec 4 '15 at 20:02
• I added some code, that seems to not work, if I ignore the error and let TexShop fix it, I obtain what I want, but I'd like to know what I am doing wrong. It is in my original post. – H5159 Dec 8 '15 at 2:34