Sign up ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I need to make a plot of a non-linear functions. To get familiar with the 'declare function' capability of pgf-tikz, I tried the following code:

      declare function={
        func(\x,\a) = 1.0/(\x^\a);
% draw grid  
      \draw[very thin,color=gray] (0.0,0.0) grid (2.0,2.0); 
% draw axes 
      \draw[->] (0.0,0.0) -- (2.0,0.0) node[right] {$x$};
      \draw[->] (0.0,0.0) -- (0.0,2.0) node[above] {$y$};     
% draw functions
      \draw[blue]  plot[domain=0.5:2.0,samples=100] (\x,{func(\x,1.0)});
      \draw[red]   plot[domain=0.8:2.0,samples=100] (\x,{func(\x,2.0)}); 
      \draw[green] plot[domain=0.5:2.0,samples=100] (\x,{func(\x,0.5)}); 

enter image description here

So what I expect to see are three lines corresponding to 1/x, 1/x^2, and 1/sqrt(x). The last function call did not give the correct function. Since I'm new to using the math functionality of pgf-tikz, I suspect I'm doing something wrong or I'm overlooking something. I'd be glad to get some pointers that

  • help me fix this problem; and

  • get some advice about how to best/better approach this problem.

The function I will need to plot is going to be more complicated, but also smooth and under some limits approaches 1/x.

share|improve this question
Welcome to! As new user without image posting privileges simply include the image as normal and remove the ! in front of it to turn it into a link. A moderator or another user with edit privileges can then reinsert the ! to turn it into an image again. – Torbjørn T. Nov 17 '11 at 17:51
As far as your second question you should see this question on consistently specifying a function and use it for computation and plotting) – Peter Grill Nov 17 '11 at 17:53
Thanks for the useful link. This cleared up a lot of things for me. – user1362373 Nov 18 '11 at 7:22

2 Answers 2

TikZ Solution:

I believe there is some limitations in using the internal math engine with power type functions (but for some reason works just fine with pgfplots as in the solution below), but if you change you defintion from func(\x,\a) = 1.0/(\x^\a) to:

func(\t,\a) = 1.0/(exp((\a)*ln(\t)));

you get (and also added very thick option to \draw):

enter image description here

Pgfplots Solution:

However, I would recommend that you use pgfplots for graphing. As per Consistently specify a Function and use it for computation and plotting, I would recommend that you use the approach suggest there to define the function:


This method will allow you to be able to use this one definition for:

  1. Computation of values to:

  2. Graphing using pgfplot

  3. Graphing using pgfplots/gnuplot

enter image description here



    domain=0.5:2.0, samples=100,
    every axis plot post/.style= ultra thick]
    \addplot [blue] {\func(x,1.0)};%
    \addplot [red]  {\func(x,2.0)};%
    \addplot [green]{\func(x,0.5)};
share|improve this answer
Thanks for the very useful clarifications. – user1362373 Nov 18 '11 at 7:22

This is a problem with the exp function from TikZ, it is solved in the CVS version. Alternatively, you can use the code Jake provides here: Is plotting exponential graphs a known source of bugs in TikZ? in the preamble to solve the problem.

share|improve this answer
Thank you for sending the link. – user1362373 Nov 18 '11 at 7:22

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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