6

I want to draw functions and have used this code. But I don't like to give every function a separate domain, I want to say this function has ymax=4 and ymin=-3. Who can help me?

enter image description here

\begin{tikzpicture}[scale=1, >=latex]
\draw[very thin, color=black, dotted, step=1 cm] (-7,-5) grid (7,5);
\draw[->] (-7.5,0) -- (7.5,0) node[right] {\scriptsize $x$};
\draw[->] (0,-5) -- (0,5.5) node[above] {\scriptsize $f(x)$};
\foreach \x in {-7,-6,...,7} \draw (\x, 1pt) -- (\x,-3pt) node[anchor=north] {\tiny \x};
\foreach \y in {-5,-4,...,5} \draw (1pt,\y) -- (-3pt,\y) node[anchor=east] {\tiny \y};
\draw[color=black, very thick, domain=-2.2:2.1, samples=100]   plot (\x,{-1/3*(\x)^2})
  node[right] {\tiny $p_1$};
\draw[color=black, dashed, domain=-1.1:1.1, samples=100]   plot (\x,{1/6*(\x)^2})
    node[right] {\tiny $p_2$};
\draw[color=black, thick, domain=-2.2:2.2, samples=100]   plot (\x,{-(\x)^2})
    node[right] {\tiny $p_3$};
\draw[color=black, thin, samples=100]   plot (\x,{0.1*(\x)^2}) node[right] {\tiny $p_4$};
\end{tikzpicture}
1
  • 1
    Welcome to TeX.SX! Please make your code compilable, starting with \documentclass{...} and ending with \end{document}. That may seem tedious to you, but think of the extra work it represents for TeX.SX users willing to help you. Help them help you: remove that one hurdle between you and a solution to your problem.
    – jub0bs
    Commented Aug 21, 2013 at 12:19

1 Answer 1

11

For this type of graph, I recommend using the pgfplots package over tikz, which is limited in terms of control over axis range and function domain. The code below should get you started with pgfplots.

Edit: Use the minor x tick num = n key (and equivalents for y- and z-axes) to specify how many minor ticks ('n', here) you want between two successive major ticks. Be mindful of potential off-by-one errors: n minor ticks translate to n+1 subintervals between two successive major ticks.

enter image description here

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.8}
\begin{document}
\begin{tikzpicture}[scale=1.7]
    \begin{axis}%
        [
            grid=major,  
            xtick={-7,-6,...,7},  
            minor x tick num=4, % 4 minor ticks => 5 subintervals
            xmin=-6,
            xmax=6,
            xlabel={\scriptsize $x$},
            axis x line=middle,
            ytick={-5,-4,...,5},
            minor y tick num=4,  % 4 minor ticks => 5 subintervals
            ymin=-3,
            ymax=4,
            ylabel={\scriptsize $f(x)$},
            axis y line=middle,
            no markers,
            samples=100,
            domain=-6:6,
        ]
        \addplot (x,{-1/3*x^2});
        \addplot (x,{1/6*x^2});
        \addplot (x,{-x^2});
        \addplot (x,{0.1*x^2});
    \end{axis}
\end{tikzpicture}
\end{document}
12
  • 2
    @Timperman You can use compat=newest to use whatever the newest version in your system but nevertheless try to update to the new version and use 1.8 key which is the current new version number.
    – percusse
    Commented Aug 21, 2013 at 13:07
  • 3
    @Timperman remove that line then. And can you put \pgfplotsversion in the document (not preamble) to see what is installed?
    – percusse
    Commented Aug 21, 2013 at 13:35
  • 1
    Thanks, but that wasn't giving anything, untill I have found how to install the updates with the Tex Live Utility. It updated 261 packages. Thanks everyone for the help! Commented Aug 21, 2013 at 13:57
  • 2
    Glad percusse and I could help, but remember that accepting and upvoting answers is the preferred way here to say “thank you” to users who helped you.
    – jub0bs
    Commented Aug 21, 2013 at 14:02
  • 1
    @Arne See my edit.
    – jub0bs
    Commented Aug 23, 2013 at 21:15

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .