piecewise-linear-function.tex 947 B

123456789101112131415161718192021222324252627282930
  1. \documentclass[varwidth=false, border=2pt]{standalone}
  2. \usepackage{pgfplots}
  3. \usepackage{tikz}
  4. \begin{document}
  5. \begin{tikzpicture}
  6. \begin{axis}[
  7. axis x line=middle,
  8. axis y line=middle,
  9. grid = major,
  10. width=16cm,
  11. height=8cm,
  12. grid style={dashed, gray!30},
  13. xmin=-1, % start the diagram at this x-coordinate
  14. xmax= 1, % end the diagram at this x-coordinate
  15. ymin= 0, % start the diagram at this y-coordinate
  16. ymax= 1.2, % end the diagram at this y-coordinate
  17. xlabel=x,
  18. ylabel=y,
  19. /pgfplots/xtick={-1, -0.5, ..., 1}, % make steps of length 0.5
  20. /pgfplots/ytick={0, 0.5, ..., 1}, % make steps of length 0.5
  21. tick align=outside,
  22. enlargelimits=false]
  23. % plot the function
  24. \addplot[domain=-1:1, blue, ultra thick,samples=500] {x < -0.5 ? 0 : (x < 0.5 ? x +0.5 : 1)};
  25. \end{axis}
  26. \end{tikzpicture}
  27. \end{document}