1

I need to graph a function that have multiple given parameters: enter image description here

EDIT

The code is almost done but I have a little and tricky problem, the solid line is supposed to start at y=3 (just as the dashed one), and now is just a little bit above of 3. This is my figure: enter image description here

And this is the expected one:

enter image description here

I have reviewed the code for many hours but I cannot find the error that generates that difference that for me is too important, can any one help me please? This is my code:

\documentclass{article}
\usepackage{tikz}
\usepackage{pgfplots} 
\pgfplotsset{compat=1.17} 
\pgfplotsset{every non boxed x axis/.append style={x axis line style=-},
     every non boxed y axis/.append style={y axis line style=-}}
\begin{document}
\begin{figure}
\centering
\begin{tikzpicture} 
\begin{axis}[xlabel={signal},
            ylabel={equilibrium bid},
            axis lines=left,
            declare function={H(\x,\n,\a,\b,\R)=
            pow(\R,(\b*(\n-1)+1))*%0
            pow(((\n*\b)+\a-1),(\b*(\n-1)))*%0
            pow(((\n*\b)+\a),(-\b*(\n-1)))*%0
            pow((\x),(-\b*(\n-1)))+(((\n*\b)+\a)/((\n*\b)+\a-1))*\x*%1
            ((\n-1)*\b)/((((\n-1)*\b)+1))*%0
            (1-pow(\R,((\b*(\n-1))+1))*%1
            pow(((\n*\b)+\a-1),(\b*(\n-1)+1))*%0
            pow(((\n*\b)+\a),((-\b*(\n-1))-1))*%0
            pow((\x),((-\b*(\n-1))-1)));},%-1
            domain=2.1:2.7,no marks,samples=11
            ,smooth, ytick distance=0.01, ymin=2.995, ymax=3.055, xmin=2.1, xmax=2.7] 
 \addplot[color=black, domain=2.1429:2.7]{H(x,2,2.5,0.5,3)}; 
 \addplot[color=black, dashed, domain=2.25:2.7]{H(x,3,2.5,0.5,3)}; 
\end{axis} 
\end{tikzpicture}
\end{figure}
\end{document}

I have checked parentheses and multiplication signs many times and really can't find my mistake.

I appreciate any advice or modification because I really need to come up with that result. The function formula is at the beginning of the post and maybe there is an error there but I am not able to find it.

oldenios
  • 340
  • 1
    There are many examples on functions of several variables, and they are supported. It would just be cumbersome to type in your function from a screen shot. –  May 27 '20 at 21:58
  • Yes! The function is so cumbersone, that's why I replace all the variables and only put H(x) after that :( – oldenios May 27 '20 at 22:01
  • 2
    This is not what I meant. By just posting screen shots you force those who may write an explicit answer to type in the function from a screen shot. –  May 27 '20 at 22:05
  • Yes, my bad. I just edited the post and added my attempt code (which obviously fails). Sorry for the inconvenience, I am a beginner in this :( – oldenios May 27 '20 at 22:19

2 Answers2

1

You can define functions of several variables, and take one of them (or a combination thereof) to be the plot variable, just like in any (other) computer algebra system. I was not motivated to type in your full function but "only" the first term, but this is more than enough to illustrate the point.

\documentclass[tikz,border=3mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.17}
\begin{document}
\begin{tikzpicture}
\begin{axis}[declare function={H(\x,\n,\a,\b,\R)=pow(\R,\b*(\n-1)+1)*
pow(\n*\b+\a-1,\b*(\n-1))*pow(\n*\b+\a,-\b*(\n-1))*pow(\x,-\b*(\n-1));},
    domain=2:2.7,no marks]
 \addplot{H(x,2,2.5,0.5,3)};
 \addplot[color=red]{H(x,2,2.5,0.5,4)};
\end{axis}
\end{tikzpicture}
\end{document}

enter image description here

As you see, in the second plot the parameter 3 was changed to 4.

ADDENDUM: As for the code from your comment, this works if you add the missing multiplication signs. E.g. \b(\n-1) needs to become \b*(\n-1).

\documentclass[tikz,border=3mm]{standalone} 
\usepackage{pgfplots} 
\pgfplotsset{compat=1.17} 
\begin{document} 
\begin{tikzpicture} 
\begin{axis}[declare function={H(\x,\n,\a,\b,\R)=pow(\R,\b*(\n-1)+1)*% 
  pow(\n*\b+\a-1,\b*(\n-1))*pow(\n*\b+\a,-\b*(\n-1))*pow(\x,-\b*(\n-1))+%
  (\n*\b+\a)/(\n*\b+\a-1)*\x*(((\n-1)*\b)/((\n-1)*\b+1))*%
  (1-pow(\R,\b*(\n-1)+1)*pow(\n*\b+\a-1,\b*(\n-1)+1)*%
  pow(\n*\b+\a,-\b*(\n-1)-1)*%
  pow(\x,-\b*(\n-1)-1));},
  domain=2.1:2.7,no marks,samples=11,smooth] 
 \addplot[color=black]{H(x,2,2.5,0.5,3)}; 
 \addplot[color=red]{H(x,2,2.5,0.6,3)}; 
\end{axis} 
\end{tikzpicture} 
\end{document} 

enter image description here

One should add that this function is perhaps already beyond what one could reasonably do with pgfplots alone. If you increase the samples you will see why.

  • Thanks man! Sorry for my mistakes in my question. I wrote the function but the result is not the expected and I think is because the axis. How could I change those details to make the figure looks similar to the posted? – oldenios May 28 '20 at 00:31
  • \documentclass[tikz,border=3mm]{standalone} \usepackage{pgfplots} \pgfplotsset{compat=1.17} \begin{document} \begin{tikzpicture} \begin{axis}[declare function={H(\x,\n,\a,\b,\R)=pow(\R,\b(\n-1)+1) pow(\n\b+\a-1,\b(\n-1))pow(\n\b+\a,-\b(\n-1))pow(\x,-\b(\n-1))+(\n\b+\a)/(\n\b+\a-1)\x(((\n-1)\b)/((\n-1)\b+1))(1-pow(\R,\b(\n-1)+1)pow(\n\b+\a-1,\b(\n-1)+1)pow(\n\b+\a,-\b(\n-1)-1)*pow(\x,-\b(\n-1)-1));}, domain=2:2.7,no marks] \addplot[color=red, dashed]{H(x,2,2.5,0.5,3)}; \end{axis} \end{tikzpicture} \end{document} – oldenios May 28 '20 at 00:31
  • 2
    @oldenios The reason why it is not as expected is that you forgot a few multiplication signs. That is, while in Mathematica you can skip them, in LaTeX you have to indicate multiplication explicitly. I added the corresponding code to the answer. –  May 28 '20 at 02:36
  • Hi! Do you know a way to corroborate the "declare function"? The output that I got is just a little bit different from the expected one (plotted in desmos.com) and I need to be just exactly D: – oldenios May 30 '20 at 03:22
  • 1
    @oldenios I am sorry, I don't. First of all, I do not know what the target output should be. There can be numerical differences, but they are independent of declare function as long as you declared the correct function. –  May 30 '20 at 03:34
  • I just published an edition of the post, if you have time, could you review it please? I promise you that I've been looking for the error for hours and I can't, I'm desperate :( – oldenios May 30 '20 at 04:02
  • 1
    @oldenios I am sorry, I cannot find the discrepancy. You could ask a new question, then others will look at it. –  May 30 '20 at 04:15
1

If I may, I'd like to complete @Schrodingerscat with a useful trick (he taught me).

When you want to draw a function with less than 9 parameters you want to play with, wrap your tikzpicture in a \newcommand :

enter image description here

\documentclass[tikz,export]{standalone}
% convert -density 300 -delay 12 -loop 0 -alpha remove MyGraph.pdf MyGraph.gif 
\usepackage{animate}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}

\tikzset{
    declare function={
    H(\x,\n,\a,\b,\R)= pow(\R,\b*(\n-1)+1)*
                       pow(\n*\b+\a-1,\b*(\n-1))*
                       pow(\n*\b+\a,-\b*(\n-1))*
                       pow(\x,-\b*(\n-1));}}

\newcommand{\MyGraphWithParameters}[4]{
%   \def\ParOne{#1}
%   \def\ParTwo{#2}
%   \def\ParThree{#3}
%   \def\ParFour{#4}

    \begin{axis}[domain=2:2.7,no marks,clip=true, ymin=1,ymax=5]
%   \addplot{H(x,\ParOne,\ParTwo,\ParThree,\ParFour)};
    \addplot{H(x,#1,#2,#3,#4)};
    \addplot[color=red]{H(x,2,2.5,0.5,4)};  
    \end{axis}
}

\begin{document}
\foreach \i in {1,...,10}   %each will be an independant frame in pdf you can call with `\includegraphics{}`
{\begin{tikzpicture}
    \MyGraphWithParameters{\i}{2.5}{0.5}{3}
\end{tikzpicture}
}
\end{document}

You can then create easily animations or gifs as explained here

JeT
  • 3,020