# Functions: a graphical perspective¶

In [6]:
import numpy as np
import matplotlib.pyplot as plt
plt.rcParams['figure.dpi'] = 120


## Graphing relations¶

We know that relations are set of ordered pairs. We can graph them by mapping each element into a point in 2D-space.

## What does the graph of a function looks like?¶

If you draw a vertical line across a function's graph, it will only intersect with at most one point.

In [7]:
plt.subplot(131)
xs1 = np.linspace(-2, -1.0e-5, 50)
xs2 = np.linspace(1.0e-5, 2, 50)
plt.plot(xs1, 1.0/xs1, color='C0')
plt.plot(xs2, 1.0/xs2, color='C0')
plt.ylim((-10, 10))
plt.title(r'$f(x) = \frac{1}{x}$')
plt.subplot(132)
xs1 = np.linspace(-np.pi / 2.0  + 1.0e-5, np.pi / 2.0 - 1.0e-5, 50)
plot_tan_offset = lambda of : plt.plot(xs1 + of, np.tan(xs1 + of), color='C0')
plot_tan_offset(0.0)
plot_tan_offset(-np.pi)
plot_tan_offset(np.pi)
plt.title(r'$f(x)=\tan x$')
plt.ylim((-10, 10))
plt.subplot(133)
xs1 = np.linspace(-4.0 * np.pi, 4.0 * np.pi, 100)
plt.plot(xs1, np.cos(xs1))
plt.title(r'$f(x)=\cos x$')
plt.tight_layout()
plt.show()


## How to tell if a relation is not a function?¶

When you draw a vertical line across a relations graph, if it intersects with more than one points, then the relation is not a function.

In [8]:
plt.subplot(121)
xs = np.linspace(-3.0, 3.0, 50)
plt.plot(np.power(xs, 2.0), xs)
plt.subplot(122)
plt.plot(np.sin(xs), xs)
plt.show()


## What does the graph of an inverse relation looks like?¶

We know that the inverse of a relation is the set of ordered pairs with swapped elements. Therefore, to graph the inverse relation, one only needs to exchange the $x$ and $y$ coordinates of the points.

In [14]:
xs = np.linspace(-3.0, 3.0, 50)
ys = np.exp(xs)
plt.plot(xs, ys, label=r'$f(x)=e^x$')
plt.plot(ys, xs, label=r'$f^{-1}(x)=\ln x$')
plt.plot(xs, xs, label=r'$g(x)=x$')
plt.xlim((-3, 3))
plt.ylim((-3, 3))
plt.legend()
plt.grid(True)
plt.gca().set_aspect(1.0)
plt.show()

In [13]:
xs = np.linspace(-3.0, 3.0, 50)
ys = np.power(xs, 3.0)
plt.plot(xs, ys, label=r'$f(x)=x^3$')
plt.plot(ys, xs, label=r'$f^{-1}(x)=\sqrt[3]{x}$')
plt.plot(xs, xs, label=r'$g(x)=x$')
plt.xlim((-3, 3))
plt.ylim((-3, 3))
plt.legend()
plt.grid(True)
plt.gca().set_aspect(1.0)
plt.show()

In [12]:
xs = np.linspace(0.0, 3.0, 50)
ys = np.power(xs, 2.0)
plt.plot(xs, ys, label=r'$f(x)=x^2\ (x \geq 0)$')
plt.plot(ys, xs, label=r'$f(x)=\sqrt{x}$')
plt.plot(xs, xs, label=r'$g(x)=x$')
plt.xlim((0, 3))
plt.ylim((0, 3))
plt.legend()
plt.grid(True)
plt.gca().set_aspect(1.0)
plt.show()


All the inverse relations above are functions, because if you draw a vertical line, you always get at most one intersection with the inverse relation.

The inverse relations shown below are not functions.

In [23]:
xs = np.linspace(-3.0, 3.0, 100)
ys = np.power(xs, 2.0)
plt.plot(xs, ys, label=r'$f(x)=x^2$')
plt.plot(ys, xs, label=r'inverse of f(x)')
plt.plot(xs, xs, label=r'$g(x)=x$')
plt.xlim((-3.0, 3.0))
plt.ylim((-3.0, 3.0))
plt.legend()
plt.grid(True)
plt.gca().set_aspect(1.0)
plt.show()

In [24]:
xs = np.linspace(2.0 * -np.pi, 2.0 * np.pi, 100)
ys = np.sin(xs)
plt.plot(xs, ys, label=r'$f(x)=\sin x$')
plt.plot(ys, xs, label=r'inverse of f(x)')
plt.plot(xs, xs, label=r'$g(x)=x$')
plt.xlim((2.0 * -np.pi, 2.0 * np.pi))
plt.ylim((2.0 * -np.pi, 2.0 * np.pi))
plt.legend()
plt.grid(True)
plt.gca().set_aspect(1.0)
plt.show()