In [2]:
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
In [3]:
c1 = lambda x: x + 1
c2 = lambda x: -x + 2
x1 = np.linspace(0.01, 2, 10)
x2 = np.linspace(-2, -0.01, 10)
plt.plot(x1, c1(x1), label=r"$y = x + 1$")
plt.plot(x2, c2(x2), label=r"$y = -x + 2$")
plt.plot(0, 2, 'wo', markersize=7)
plt.plot(0, 1, 'wo', markersize=7)
ax = plt.axes()
ax.set_ylim(0, 4)
plt.legend(loc=3)
Out[3]:
Even though the value of $y$ for when $x = 0$ is undefined we can say something about the limits of this function.
If we approach from a position where $x \gt 0$ we can say that as $x$ approaches 0 the limit of the function is $x + 1$.
$$\lim_{x^+\to 0}f(x) = \lim_{x\to 0}x + 1 = 1$$On the other hand, if we approach from a position where $x \lt 0$ then we see that as $x$ approaches 0 the limit of the function is $-x + 2$.
$$\lim_{x^-\to 0}f(x) = \lim_{x\to 0}-x + 2 = 2$$And now that we have defined limits we can also define what it means for a function to be continuous at a certain value. A function $f$ is continuous at $x_0$ when $\lim_{x\to x_0}f(x) = f(x_0)$
In [4]:
g = lambda x: np.sin(x) / x
h = lambda x: (1 - np.cos(x)) / x
x = np.linspace(-3 * np.pi, 3 * np.pi, 100)
ax = plt.axes()
ax.set_xlim(-3 * np.pi, 3 * np.pi)
ax.set_ylim(-1, 1.25)
plt.plot(x, g(x), label=r"$y = g(x) = \frac{\sin x}{x}$")
plt.plot(x, h(x), label=R"$y = h(x) = \frac{1 - \cos x}{x}$")
plt.plot(0, 1, 'wo', markersize=7)
plt.plot(0, 0, 'wo', markersize=7)
plt.legend(loc=4)
Out[4]:
Note that dividing by zero is an undefined operation, both of these functions are undefined for when $x = 0$ so we'll have two little circles in the plot. However we can see that $\lim_{x^+\to 0}g(x) = 1$ and that $\lim_{x^-\to 0}g(x) = 1$ so generally we can say that $\lim_{x\to 0}g(x) = 1$. We can also see that $\lim_{x^+\to 0}h(x) = 0$ and $\lim_{x^-\to 0}h(x) = 0$ so $\lim_{x\to 0}h(x) = 0$.
Because for both functions $\lim_{x^+\to 0} = \lim_{x^-\to 0}$ we can say that these functions have a removable discontinuity at $x = 0$
In [5]:
f = lambda x: 1/x
x1 = np.linspace(-0.5, -0.01, 1000)
x2 = np.linspace(0.01, 0.5, 1000)
ax = plt.axes()
#ax.spines['left'].set_position(('data', 0))
#ax.spines['bottom'].set_position(('data', 0))
ax.set_xlim(-0.1, 0.1)
plt.plot(x1, f(x1), 'b')
plt.plot(x2, f(x2), 'b')
Out[5]:
Now we see that $\lim_{x^+\to 0}\frac{1}{x} = \infty$ and $\lim_{x^-\to 0}\frac{1}{x} = -\infty$ and even though some people might say that these limits are undefined they are going in a definite direction so if able we should specify what they are.
However we cannot say that $\lim_{x\to 0}\frac{1}{x} = \infty$ even though this is sometimes done it's usually because people are sloppy and only considering $y = \frac{1}{x}$ for when $x \gt 0$.
There's an interesting thing we can observe when we plot the derivative of this function.
In [6]:
f0 = lambda x: 1/x
f1 = lambda x: -1/x**2
x1 = np.linspace(-0.5, -0.01, 1000)
x2 = np.linspace(0.01, 0.5, 1000)
p1 = plt.subplot(211)
p1.set_xlim(-0.1, 0.1)
plt.plot(x1, f0(x1), 'b', label=r"$y = 1/x$")
plt.plot(x2, f0(x2), 'b')
plt.legend(loc=4)
p2 = plt.subplot(212)
p2.set_xlim(-0.1, 0.1)
p2.set_ylim(-2000, 0)
plt.plot(x1, f1(x1), 'g', label=r"$y = -1/x^2$")
plt.plot(x2, f1(x2), 'g')
plt.legend(loc=4)
Out[6]:
If we take the derivative of an odd function we get an even function.
In [7]:
f = lambda x: np.sin(1/x)
x1 = np.linspace(-0.1, -0.01, 100)
x2 = np.linspace(0.01, 0.1, 100)
ax = plt.axes()
ax.set_xlim(-0.1, 0.1)
ax.set_ylim(-1.2, 1.2)
plt.plot(x1, f(x1))
plt.plot(x2, f(x2))
Out[7]:
As we approach $x = 0$ it will oscilate into infinity. There is no left or right limit in this case.
Let's start with the question of what is a derivative? We'll look at a few different aspects:
We'll start with the geometric interpretation.
Find the tangent line to the graph of some function $y = f(x)$ at some point $P = (x_0, y_0)$. We also know this line can be written as the equation $y - y_0 = m(x - x_0)$. In order to figure out this equation we need to know two things, point $P$ which is $(x_0, y_0)$ where $y_0 = f(x_0)$ and the value of $m$ which is the slope of the line. In calculus we also call this the derivative or $f'(x)$.
In [69]:
f = lambda x: x**2
fig, ax = plt.subplots()
ax.spines['top'].set_visible(False)
ax.spines['right'].set_visible(False)
ax.spines['bottom'].set_position(('data', 0))
ax.spines['left'].set_position(('data', 0))
ax.xaxis.set_ticks_position('bottom')
ax.yaxis.set_ticks_position('left')
ax.xaxis.set_ticklabels(['$x_0$', '$x$'])
ax.yaxis.set_ticklabels(['$y_0$', '$y$'])
ax.xaxis.set_ticks([1, 1.5])
ax.yaxis.set_ticks([1, f(1.5)])
ax.set_xlim(-1, 2)
ax.set_ylim(-1, 3)
x = np.linspace(-1, 2, 100)
plt.plot(x, f(x))
plt.plot(1, f(1), 'ko')
plt.plot(1.5, f(1.5), 'ko')
plt.plot([1, 1.5], [f(1), f(1)], 'k--')
plt.plot([1.5, 1.5], [f(1), f(1.5)], 'k--')
plt.plot([1, 1.5], [f(1), f(1.5)], 'k--')
plt.annotate('$P$', (0.8, 1))
plt.annotate('$Q$', (1.3, f(1.5)))
plt.annotate('$\Delta{x}$', (1.25, 0.75))
plt.annotate('$\Delta{f}$', (1.55, 1.5))
Out[69]:
We can now define, $f'(x_0)$ (the derivative) of $f$ at $x_0$ is the slope of the tangent line to $y = f(x)$ at the point $P$. The tangent line is equal to the limit of secant lines $PQ$ as $Q\to P$ where $P$ is fixed. In the picture above we can see that the slope of our our secant line $PQ$ is simply defined as $\frac{\Delta{f}}{\Delta{x}}$. However we can now define the slope $m$ of our tangent line as
$$m = \lim_{\Delta{x}\to 0}\frac{\Delta{f}}{\Delta{x}}$$The next thing we want to do is to write $\Delta{f}$ more explicitly. We already have $P = (x_0, f(x_0))$ and $Q = (x_0 + \Delta{x}, f(x_0 + \Delta{x})$. With this information information we can write down:
$$f'(x_0) = m = \lim_{\Delta{x}\to 0}\frac{f(x_0 + \Delta{x}) - f(x_0)}{\Delta{x}}$$
In [71]:
f = lambda x: 1 / (1 + x**2)
x = np.linspace(-2, 2, 100)
y = f(x)
ax = plt.axes()
ax.set_ylim(0, 1.25)
plt.plot(x, y, label=r"$y = \frac{1}{1 + x^2}$")
plt.legend()
Out[71]:
Now let's compute $f'(x)$.
$$ \begin{align} f'(x) & = \lim_{\Delta{x}\to 0}\frac{f(x + \Delta{x}) - f(x)}{\Delta{x}} \\ & = \lim_{\Delta{x}\to 0}\frac{\frac{1}{1 + (x + \Delta{x})^2} - \frac{1}{1 + x^2}}{\Delta{x}} \\ & = \lim_{\Delta{x}\to 0}\frac{1}{\Delta{x}}\frac{1 + x^2 - (1 + (x + \Delta{x})^2)}{(1 + (x + \Delta{x})^2)(1 + x^2)} \\ & = \lim_{\Delta{x}\to 0}\frac{1}{\Delta{x}}\frac{1 + x^2 -1 - x^2 - 2x\Delta{x} - \Delta{x}^2}{(1 + (x + \Delta{x})^2)(1 + x^2)} \\ & = \lim_{\Delta{x}\to 0}\frac{-2x - \Delta{x}}{(1 + (x + \Delta{x}^2)(1 + x^2)} \\ & = \frac{-2x}{(1 + x^2)^2} \end{align} $$
In [11]:
f_acc = lambda x: (-2 * x) / ((1 + x**2)**2)
x = np.linspace(-2, 2, 100)
plt.plot(x, f_acc(x))
Out[11]: