% Code for the left graph:
\documentclass{standalone}
\usepackage{pgfplots} % Plotting library
\begin{document}
\begin{tikzpicture}
\begin{axis}[
xlabel = $x$,
ylabel = $f(x)\ {=}\ 2x+3$,
y = 1cm,
x = 1cm,
xmin = -5, xmax = 5, ymin = -2, ymax = 5,
axis lines = middle,
xlabel style = {anchor = west, xshift = 5pt},
ylabel style = {anchor = south, yshift = 5pt}
]
\addplot [blue, very thick, samples = 200] {2*x + 3};
% Add a bunch of colorful horizontal lines. Notice how each horizontal line intersects only ONCE with the blue curve of f(x) = 2x + 3:
\pgfplotsinvokeforeach{0,...,8}{
\addplot +[mark = none, very thick] {#1*0.5 + 0.5};
}
\end{axis}
\end{tikzpicture}
\end{document}
% Code for the right graph:
\documentclass{standalone}
\usepackage{pgfplots} % Plotting library
\usetikzlibrary{intersections}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
xlabel = $x$,
ylabel = $f(x)\ {=}\ x^2$,
y = 1cm,
x = 1cm,
xmin = -5, xmax = 5, ymin = -2, ymax = 5,
axis lines = middle,
xlabel style = {anchor = west, xshift = 5pt},
ylabel style = {anchor = south, yshift = 5pt}
]
\addplot [blue, very thick, samples = 200, name path global = Square] {x^2};
% Add a red horizontal line at y = 2. Notice that this line intersects TWICE with the blue curve: once at a value of x between -2 and -1 (approx -1.414), and another time at a value of x between 1 and 2 (approx +1.414).
\addplot [mark = none, very thick, red, name path global = Line] {2};
% Mark intersections:
\fill [name intersections = {of = Square and Line, name = i, total = \t}, red, opacity = 1]
\foreach \s in {1,...,\t}{
(i-\s) circle (3pt)
};
\end{axis}
\end{tikzpicture}
\end{document}