# Automatically define domain of function

I use the following code to depict mathematical functions. This is my coordinate system:

\newenvironment{graph}[2]{\begin{tikzpicture}[scale=0.6]
\draw[->] (0,0) -- (10,0) node[right] {$#1$};
\draw[->] (0,0) -- (0,10) node[above] {$#2$};
}
{
\end{tikzpicture}
}


To create a function I use the following command:

 \newcommand{\gf}[2]{\draw plot[variable=\t,samples=1000,scale=1,domain= #1 ,smooth] ({\t},{#2});}


It takes domain and the function itself as inputs.

The problem is, that I have to define the domain each time I draw a new function. As you can see from my coordinate system, I always depict graphs in a ten by ten coordinate system. I would like to make a new command that can determine the domain of whatever function I choose so that it is contained within my ten by ten coordinate system. Is this possible?

Example with two functions:

\begin{graph}{x}{y}
\gf{0:10}{\t^2}
\gf{0:10}{3-2\t}
\end{graph}


In this example the value of the first function evaluated at 10 is 100, which is out of bounds. Let us call the function g(x). I would like latex to find the value of x so that the value of the function is 10, g(x)=10, and insert this value as the upper bound of the domain.

The second function has a similar problem. Let us call it f(x). It becomes very negative, and exits the coordinate system in the bottom. In this case I would like latex to find the value of the function so that f(x)=0 and insert it as the upper bound.

• It seems to me that would love package pgfplots? Commented Mar 13, 2014 at 8:40
• AFAIK you cannot add dotted lines, insert arrows and points. All of which I often do. Commented Mar 13, 2014 at 9:11
• pgfplots is based on tikz/pgf. You can draw dotted lines, annotate your graphs and more. Please have a look at the manual, there is a very good tutorial. Commented Mar 13, 2014 at 9:15
• @user1067911 As Johannes_B already mentioned you can draw anything into the graphs that tikZ can draw. Above that pgfplots offers a so called axis coordinate system where you can specify coordinates in terms of plot coordinates. Therefore you do not even try around with arbitrary lengths that you have to map roughly onto the plot but just put in coordinates. Commented Mar 13, 2014 at 9:32

OK, here is an example with pgfplots. I agree with Johannes_B and Benedikt Bauer in using pgfplots.

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.9}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
axis lines=left,
scaled ticks=true,
xlabel=$x$,
ylabel=$y$,
xmax=11,
ymax=110,
small,
domain=1:10,
samples=100
]
\end{axis}
\end{tikzpicture}
\begin{tikzpicture}
\begin{axis}[
axis lines=left,
scaled ticks=true,
xlabel=$x$,
ylabel=$y$,
xmax=11,
ymax=11,
small,
domain=1:10,
samples=100
]

To conclude, pgfplots is very flexible and you can just use tikz inside the graph.
• If I use your code the graphs become very small, and if I scale them all the letters I add becomes very small in comparison. Eg. when I add a node: \draw node at (1,1) {x}; . Furthermore the y-axis is shorter than the x-axis, eventhough the length should be same (xmax=11, ymax=11). Commented Mar 13, 2014 at 15:12
• @user1067911 if you want the node at (1,1) try node at (axis cs:1,1) {x}. Further, please note that I have not specified ymin and xmin. These values are not equal (ymin \neq xmin). Hence axes are not qually long. You can make them equally long by adding axis equal just before axis lines=left,. Please refer to pgfplots documentation.