2

I have a function that I want to graph. However, there are 10 variations of it with 5 different constants each. I do not want to simply plug in a number 50 times. Is there an easier way to do this? Also, the graph comes out strangely. Is there an alternative to pgfplotsthat I use to generate an image and then just use includegraphics, preferably something with vector graphics so that it is of the highest quality. Thank you.

\documentclass[letterpaper]{article}
\usepackage{pgfplots} \pgfplotsset{width=10cm,compat=1.16}

\begin{document}


\[ y^{\prime} ( t ) = \frac{\lambda \phi ( \beta - \alpha )}{t \left[ 1 + ( t/\gamma)^{-\phi} \right]^\lambda \left[ 1 + (t/\gamma)^{\phi} \right]} \] 


\begin{figure}[htbp!]
  \centering
  \begin{tikzpicture}
     \begin{axis}[
      legend pos=north east,
      title = {},
      xlabel = {time},
      ylabel = {temperature},
      xmin = 0, xmax = 15,
      ymin = 0, ymax = 8,
      xtick = {0, 5, 10, 15},
      ytick = {0, 2, 4, 6, 8},
    ]

      \addplot[black, no marks, domain=0.01:15, smooth]{(1.86*3.38*(42.2 - 23.3))/(x*(1 + (x/3.15)^(-3.38))^(1.86)*(1 + (x/3.15)^(3.38)))};
      \addlegendentry{fitted function}

      \addplot[red, no marks, domain=0.01:15, smooth]{(12*3.01*(43.6 - 23.8))/(x*(1 + (x/1.38)^(-3.01))^(12)*(1 + (x/1.38)^(3.01)))};
      \addlegendentry{fitted function2}

    \end{axis}
  \end{tikzpicture}

\end{figure}

\end{document}
Ilyankor
  • 353

1 Answers1

2

You can declare functions with the key declare function. (The TikZ library math has additional means to define functions, but this does not harmonize that well with pgfplots because pgfplots uses fpu.) Here is an example. This reproduces your plot with a declared function.

\documentclass[letterpaper]{article}
\usepackage{pgfplots} 
\pgfplotsset{width=10cm,compat=1.16}

\begin{document}


\[ y^{\prime} ( t ) = \frac{\lambda \phi ( \beta - \alpha )}{t \left[ 1 + ( t/\gamma)^{-\phi} \right]^\lambda \left[ 1 + (t/\gamma)^{\phi} \right]} \] 


\begin{figure}[htbp!]
  \centering
  \begin{tikzpicture}[declare function={f(\x,\alpha,\beta,\gamma,\lambda,\phi)=%
  (\lambda*\phi*( \beta - \alpha ))/(\x*pow( 1 + pow( \x/\gamma,-\phi),\lambda) 
  *( 1 + pow(\x/\gamma,\phi)));}]
     \begin{axis}[
      legend pos=north east,
      title = {},
      xlabel = {time},
      ylabel = {temperature},
      xmin = 0, xmax = 15,
      ymin = 0, ymax = 8,
      xtick = {0, 5, 10, 15},
      ytick = {0, 2, 4, 6, 8},
    ]

      \addplot[black, no marks, domain=0.01:15, smooth]
      {f(x,23.3,42.2,3.15,1.86,3.38)};
      %\addplot[black, no marks, domain=0.01:15, smooth]{(1.86*3.38*(42.2 - 23.3))/(x*(1 + (x/3.15)^(-3.38))^(1.86)*(1 + (x/3.15)^(3.38)))};
      \addlegendentry{fitted function}

      \addplot[red, no marks, domain=0.01:15, smooth]
      {f(x,23.8,43.6,1.38,12,3.01)};
      %\addplot[red, no marks, domain=0.01:15, smooth]{(12*3.01*(43.6 - 23.8))/(x*(1 + (x/1.38)^(-3.01))^(12)*(1 + (x/1.38)^(3.01)))};
      \addlegendentry{fitted function2}


    \end{axis}
  \end{tikzpicture}
\end{figure}
\end{document}

enter image description here