Why is this not working properly:



        \begin{axis}[xmin = 0,xmax= 1,ylabel = $C_A/C_A^*$,xlabel=$z$]
            \foreach \x in {.01,.1,1,10}
                \addplot[domain=0:1] {1/(1+exp(\x^.5))*(exp(\x^.5)*exp(\x^.5*-x)+exp(\x^.5*x))};


enter image description here

While it should produce:

enter image description here

I have tried to increase the samples in the addplot options to no avail.

  • 3
    you are using x and \x in the same equation which confuses it. change all instances of \x with something like \myvar and it works – ArTourter Apr 14 '15 at 17:22
  • @ArTourter Thanks, that works. But why so? – jak123 Apr 14 '15 at 17:27
  • 2
    Just a suggestion - use parenthesis around every variable in an expression, as when the variable has a negative value, - sign may change the order of calculation. Example: \x^2 is not the same as `(\x)^2 when x is negative. Correct me if I am wrong, but that was my impression from playing with pgfplot. See this: tex.stackexchange.com/a/125896/37570 – ajeh Apr 14 '15 at 18:31

pgfplots understands the macro and the character versions of the variables. From the manual

What really goes on is a loop which assigns the current sample coordinate to the macro \x.

pgfplots defines a math constant x which always has the same value as \x. In short: it is the same whether you write \x or just x inside of math expressions.

So that fixes the first problem but if you use this foreach loop for other variables in the options say to use as a color modifier etc. it still won't work. This is the common problem of having a macro within an \addplot command unexpanded such as this explanation : How to node foreach value in math equation?

Hence, it is a good practice to loop with pgfplots's own loop macro as below(I would also use sqrt(#1) instead):

  \begin{axis}[xmin = 0,xmax= 1,ylabel = $C_A/C_A^*$,xlabel=$z$]

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.