0

How can I plot a function in latex?for example, if I simply want to draw a decreasing function in first quadrant say f(x) = 1/x and draw dashed line to join (2,0) to (2,1/2) and solid line to join (2,1/2) to (0,1/2).

Willoughby
  • 3,649
UserA
  • 113
  • 4
  • 1
    See https://tex.stackexchange.com/a/105584/110998 as a starting point. Or https://tex.stackexchange.com/a/121799/110998. Or https://latexdraw.com/plot-a-function-and-data-in-latex/ Just google for latex plot function – gernot Nov 25 '21 at 11:23
  • 1
    Have a look at the tikz package. For example, within a tikzpicture environment you can do \draw (2, 0) -- (2, 0.5); to draw a line from (2, 0) to (2, 0.5). For more complicated plots there is a plot command, \draw plot (\x, f(\x)); where f is some function that either you define or a built in function. For even more complicated plots have a look at pgfplots. – Willoughby Nov 25 '21 at 11:25
  • Can you please draw above one only, because I'm a beginner – UserA Nov 25 '21 at 11:30

1 Answers1

1

The best way to learn is experimenting, you could try using tikz and pgfplots:

\usepackage{pgfplots}
\pgfplotsset{compat = newest}

\begin{document}

\begin{tikzpicture} \begin{axis}[ xmin = 0, xmax = 30, ymin = 0, ymax = 2.0] \draw [dashed] (2, 0) -- (2, 0.5); \draw (2, 0.5) -- (0, 0.5); \addplot[ domain = 0:30, samples = 100, smooth, thick, ] {1/x}; \end{axis} \end{tikzpicture}

\end{document}

screenshot

Jaap
  • 359
  • Can you please add a screenshot of your result to the answer? – Mensch Nov 25 '21 at 11:50
  • Thank you for answering. Can you please add how to label axes as X and Y and function as f(x) and lines as x=a and y=b on this picture. It will be very helpful. – UserA Nov 30 '21 at 10:06
  • Add xlabel={x-axis label} and ylabel={y-axis label} to the \begin{axis} command – Jaap Nov 30 '21 at 11:18