2d-parted-function.tex 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. \documentclass[varwidth=true, border=2pt]{standalone}
  2. \usepackage{pgfplots}
  3. \usepackage{tikz}
  4. \begin{document}
  5. \begin{tikzpicture}
  6. \begin{axis}[
  7. legend pos=south west,
  8. axis x line=middle,
  9. axis y line=middle,
  10. grid = major,
  11. %width=9cm,
  12. %height=4.5cm,
  13. grid style={dashed, gray!30},
  14. xmin=-1, % start the diagram at this x-coordinate
  15. xmax= 6, % end the diagram at this x-coordinate
  16. ymin=-0.25, % start the diagram at this y-coordinate
  17. ymax= 2.25, % end the diagram at this y-coordinate
  18. axis background/.style={fill=white},
  19. xlabel=$x$,
  20. ylabel=$y$,
  21. %xticklabels={-2,-1.6,...,7},
  22. %yticklabels={-8,-7,...,8},
  23. tick align=outside,
  24. minor tick num=-3,
  25. enlargelimits=true,
  26. tension=0.08]
  27. \addplot[domain=0:1, red, thick,samples=20] {0.5*x*x};
  28. \addplot[domain=1:2, green, thick,samples=20] {x-0.5};
  29. \addplot[domain=2:3, blue, thick,samples=500] {-0.5*(x-2)*(x-2)+x-0.5};
  30. \addplot[domain=3:5, purple, thick,samples=20] {5-x};
  31. \addplot[domain=5:7, orange, thick,samples=3] {0};
  32. \addplot[domain=-3:0, orange, thick,samples=3] {0};
  33. %\addlegendentry{$f_1(x)=\frac{1}{2}x^2$}
  34. %\addlegendentry{$f_2(x)=x-\frac{1}{2}$}
  35. %\addlegendentry{$f_2(x)=-\frac{1}{2} (x-2)^2+x-\frac{1}{2}$}
  36. \end{axis}
  37. \end{tikzpicture}
  38. \end{document}