2

I tried to plot the following function but it didn't work:

y = 300 + 1000/x + 2.5x.

This is the document, but instead of the plot it only shows the axis.

\documentclass{article}

\usepackage{pgfplots}% This uses tikz
\pgfplotsset{compat=newest}% use newest version

\pgfmathdeclarefunction{Function}{1}{%
  \pgfmathparse{300 + 1000/x + 2.5}%
}

\tikzset{My Line Style/.style={smooth, thick, samples=400}}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
    xmin=0.0,
    xmax=250,
    ymin=0,
    ymax=50,
    axis x line=bottom,
    axis y line=left, 
    xlabel= $y$,
    ylabel=Cost, 
    xtick={250},
    xticklabels={250},    
    restrict x to domain=0:250,
    ]   
    \addplot+[My Line Style, color=black, samples at={1,2,3,4,...,249,250}] (\x,{Function(\x)});


\end{axis} 
\end{tikzpicture}
\end{document}
\documentclass{article}
  • 3
    You're limiting the y axis to the range from 0 to 50, but the minimum value of your function in your domain (x=0 .. 250) is 300. Remove the ymax=50 and you'll see the function. By the way, the function in your question and code are different (+2.5 vs. +2.5x) – Jake Jan 8 '14 at 12:32
  • Note also that you can use samples at={1,...,250} to get the same result that you get with samples at={1,2,3,4,...,249,250} – Jake Jan 8 '14 at 12:33
  • @Mark, would you care to write a self-answer on this one? Self-answers are perfectly admissible, and a well-written answer may earn you additional reputation. – Paul Gessler Mar 2 '14 at 3:49
4

Answering per information in the comments.

I set compat=1.9 instead of compat=newest so this solution will work even if a future update breaks compatibility.

I also updated the declaration of your function to match what is written in the question text. In your style My Line Style, samples=400 is redundant since you're using samples at=<...> below.

The problem was ymax=50, which clipped the plot with none of the function showing. I have commented this out to automatically set the limit, but you could also change it to a more appropriate value.

I also adjusted the contents of samples at=<...> to produce (in my opinion) better mark spacing. You could also use Jake's suggestion of samples at={1,...,250}, or, equivalently, samples=250, domain=0:250.

Code

\documentclass{article}

\usepackage{pgfplots}% This uses tikz
\pgfplotsset{compat=1.9}% use current version at time of writing

\pgfmathdeclarefunction{Function}{1}{%
  \pgfmathparse{300 + 1000/x + 2.5*x}% corrected to written form in question text
}

\tikzset{My Line Style/.style={smooth, thick}} % samples=400 is redundant since you specify samples at below

\begin{document}
\begin{tikzpicture}
\begin{axis}[
  xmin=0.0,
  xmax=250,
  ymin=0,
  %ymax=50, % <<< this was the problem
  axis x line=bottom,
  axis y line=left, 
  xlabel= $y$,
  ylabel=Cost, 
  xtick={250},
  xticklabels={250},    
  restrict x to domain=0:250,
]   
    \addplot+[My Line Style, color=black, samples at={1,2,3,4,6,9,16,...,250}] (\x,{Function(\x)});
\end{axis} 
\end{tikzpicture}
\end{document}

Output

enter image description here

|improve this answer|||||

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.