Developing rules for calculating derivatives

… then jump into SymPy

Plan

  1. Introduce notation, language, and rules to make concrete the idea of “getting closer and closer to a point but not be equal to that point”
  2. Develop shortcuts for calculating derivatives
  3. Learn how to calculate derivatives by hand using these shortcuts
  4. Jump into using SymPy, a way to do symbolic computation in Python

“Getting closer and closer”

Suppose a function \(f\) is defined for all \(x\) near \(a\), but not necessarily at \(x=a\). We write \[\lim_{x\to a} f(x)=A\] if \(f(x)\) gets closer and closer to \(A\) whenever \(x\) gets closer and closer to \(a\).

  1. \(x\) gets closer and closer to \(a\): \(x\to a\)
  2. \(f(x)\) gets closer and closer to \(A\): \(f(x)\to A\)

Using the terminology

\[\lim_{x\to a} f(x)=A\]

  1. \(A\) is the limit of \(f\) as \(x\) gets closer and closer to \(a\)
  2. Note that it is possible that the limit may not exist (but we do not cover it here yet).
  3. Note that \(f(a)\) itself does not have to be defined.

Working with limits

If \(\displaystyle\lim_{x\to a} f(x)=A\) and \(\lim_{x\to a} g(x)=B\), then

  1. \(\displaystyle \lim_{x\to a} \left(f(x)\pm g(x)\right)=A\pm B\)
  2. \(\displaystyle \lim_{x\to a} f(x) \cdot g(x)=A \cdot B\)
  3. Provided that \(B\neq 0\), \(\displaystyle \lim_{x\to a} \frac{f(x)}{g(x)}=\frac{A}{B}\)
  4. Provided that \(A^r\) is defined and \(r\) is any real number, \(\displaystyle \lim_{x\to a} \left(f(x)\right)^r=A^r\)

Pattern matching

  1. Provided that \(c\) is a constant, \(\displaystyle \lim_{x\to a} c=c\) (Building block)
  2. \(\displaystyle \lim_{x\to a} x=a\) (Building block)
  3. \(\displaystyle \lim_{x\to a} x^2=\displaystyle a^2\)
  4. \(\displaystyle \lim_{x\to a} (x^2+5x)=\displaystyle a^2+5a\)
  5. \(\displaystyle \lim_{x\to a} \sqrt{x^2+5x}=\displaystyle \sqrt{a^2+5a}\)
  6. \(\displaystyle \lim_{x\to a} \frac{x^2-a^2}{x-a}=\lim_{x\to a}\ (x+a)=2a\)

Definition of derivative

Definition 1 (Derivative at a point) The derivative of the function \(f\) at point a, denoted by \(f^{\prime}(a)\), is given by the formula \[f^{\prime}(a)=\lim_{h\to 0}\frac{f(a+h)-f(a)}{h}.\]

Given a function \(f(x)\), the derivative it itself a function \(f^\prime(x)\) provided that \(x\) is a valid input.

Example 01

Let \(f(x)=mx+b\). Compute the derivative of \(f\).

  1. We found the Newton quotient to be \[\frac{f(a+h)-f(a)}{h}=m.\]
  2. Then \(\displaystyle f^\prime(a)=\lim_{h\to 0} \frac{f(a+h)-f(a)}{h}=?\)
  3. Therefore, if \(f(x)=mx+b\), then \(f^\prime(x)=?\)

Example 02

Let \(g(r)=\pi r^2\). Compute the derivative of \(g\).

  1. We found the Newton quotient to be \[\frac{g(a+h)-g(a)}{h}=2\pi a +\pi h.\]
  2. Then \(\displaystyle g^\prime(a)=\lim_{h\to 0} \left(2\pi a +\pi h\right) = ?\)
  3. Therefore, if \(g(r)=\pi r^2\), then \(g^\prime(x)=?\)

Example 03

Let \(f(x) = \sqrt{x}\). Compute the derivative of \(f\).

  1. We found the Newton quotient to be \[\frac{f(a+h)-f(a)}{h}=\frac{1}{\sqrt{a+h}+\sqrt{a}}.\]
  2. Then \(\displaystyle f^\prime(a)=\lim_{h\to 0} \frac{1}{\sqrt{a+h}+\sqrt{a}} = ?\)
  3. Therefore, if \(f(x) = \sqrt{x}\), then \(f^\prime(x)=?\)

Example 04

Let \(c(t) = \displaystyle \frac{t}{t^2+4}\). Compute the derivative of \(c\).

  1. We found the Newton quotient to be \[\frac{c(a+h)-c(a)}{h}=\frac{-a^2+4-ah}{\left[(a+h)^2+4\right](a^2+4)}.\]
  2. Then \(\displaystyle c^\prime(a)=\lim_{h\to 0} \frac{-a^2+4-ah}{\left[(a+h)^2+4\right](a^2+4)} = ?\)
  3. Therefore, \(c^\prime(t)=?\)

Shortcut 01

We can already just directly use the idea that \[f^\prime(x)=\lim_{h\to 0}\frac{f(x+h)-f(x)}{h},\] with the understanding that \(x\) has to be a valid input.

Shortcut 02

  1. You already found that if \(f(x)=mx+b\), then \(f^{\prime}(x)=m\).
  2. What does this mean when \(m=0\)?

Shortcut 03

  1. Let \(c\) be a constant and \(g(x)=cf(x)\).
  2. What is \(g^\prime(x)\)?

Shortcut 04

  1. You already found that if \(f(x)=mx+b\), then \(f^\prime(x)=m\).
  2. Set \(m=1\). What do you notice?
  3. You already found that if \(g(r)=\pi r^2\), then \(g^\prime(r)= 2\pi r\). So if \(f(x)=x^2\), can you guess what is \(f^\prime(x)\)?
  4. You already found that if \(f(x)=\sqrt{x}\), then \(f^\prime(x)=\displaystyle\frac{1}{2\sqrt{x}}\).
  5. Notice a pattern?

The pattern you see is actually the Power Rule at work.

  1. Let \(r\) be an arbitrary constant. It states that if \(f(x)= x^r\), then \[f^\prime(x)=rx^{r-1}.\]
  2. We cannot prove this for real now, but we will next week.

Shortcut 05

  1. You already found that if \(f(x)=mx+b\), then \(f^\prime(x)=m\).
  2. But you can think of \(mx+b\) as the sum of two functions \(mx\) and \(b\).
  3. Applying the shortcuts you have to get the derivatives of each function, you get derivatives \(m\) and \(0\), respectively.

The pattern you see is actually the Sum Rule at work:

If \(F(x)=f(x)\pm g(x)\), then \[F^\prime(x)=f^\prime(x)\pm g^\prime(x).\]

The rule extends to sums of many functions.

Shortcut 06

Product Rule: If \(F(x)=f(x)\cdot g(x)\), then \[F^\prime(x)=f(x)g^\prime(x)+g(x)f^\prime(x).\]

Quotient Rule: If \(F(x)=\dfrac{f(x)}{g(x)}\), then \[F^\prime(x)=\frac{g(x)f^\prime(x)-f(x)g^\prime(x)}{\left(g(x)\right)^2}.\]

We actually encountered a quotient of two functions already, \[c(t)=\frac{t}{t^2+4}.\]

Use the Quotient Rule to find \(c^\prime(t)\).

Why can’t we use the Product Rule to find \(c^\prime(t)\) for \[c(t)=\frac{t}{t^2+4}=t(t^2+4)^{-1}?\]

Shortcut 07

  1. From one of the exercises in HW01, you (hopefully) found that \[\lim_{h\to 0} \frac{e^h-1}{h}=1.\]
  2. We did not prove the previous result, but only gave an indication that it might be true.
  3. Take comfort in the fact that it is true. If \(f(x)=e^x\), then \(f^\prime(x)=e^x\).

Jumping into SymPy

We will be using SymPy, a part of Python which enables you to do symbolic computation.

Go to the SymPy Live Shell at https://live.sympy.org.

# Load SymPy
from sympy import *
# Declare symbols
x = symbols('x', real = True)
# Encode the expression
expr = sqrt(x)
# Take derivative
diff(expr, x)

\(\displaystyle \frac{1}{2 \sqrt{x}}\)

What if you want \(f^\prime(x)\) if \(f(x)=mx+b\)?

# Load SymPy
from sympy import *
# Declare symbols
x = symbols('x', real = True)
m, b = symbols('m b', real = True)
# Encode the expression
expr = m*x+b
# Take derivative
diff(expr, x)

\(\displaystyle m\)

What if you want \(g^\prime(r)\) if \(g(r)=\pi r^2\)?

# Load SymPy
from sympy import *
# Declare symbols
r = symbols('r', real = True)
# Encode the expression
expr = pi*r**2
# Take derivative
diff(expr, r)

\(\displaystyle 2 \pi r\)

What if you want \(c^\prime(t)\) if \(c(t)=\dfrac{t}{t^2+4}\)?

# Load SymPy
from sympy import *
# Declare symbols
t = symbols('t', real = True)
# Encode the expression
expr = t/(t**2+4)
# Take derivative
diff(expr, t)

\(\displaystyle - \frac{2 t^{2}}{\left(t^{2} + 4\right)^{2}} + \frac{1}{t^{2} + 4}\)

# Simplify
factor(diff(expr, t))

\(\displaystyle - \frac{\left(t - 2\right) \left(t + 2\right)}{\left(t^{2} + 4\right)^{2}}\)

What if you want to calculate limits? For example, \[\lim_{x\to a} x^2=?\]

# Load SymPy
from sympy import *
# Declare symbols
x, a = symbols('x a', real = True)
# Encode the expression
expr = x**2
# Compute limit
limit(expr, x, a)

\(\displaystyle a^{2}\)

Limit of a Newton quotient as \(h\to 0\)? For example, \[\lim_{h\to 0}\frac{\sqrt{a+h}-\sqrt{a}}{h}=?\]

# Load SymPy
from sympy import *
# Declare symbols
a, h = symbols('a h', real = True)
# Encode the expression
expr = (sqrt(a+h)-sqrt(a))/h
# Compute limit
limit(expr, h, 0)

\(\displaystyle \frac{1}{2 \sqrt{a}}\)

Recall \[\lim_{h\to 0}\frac{e^{h}-1}{h}=?\]

# Load SymPy
from sympy import *
# Declare symbols
h = symbols('h', real = True)
# Encode the expression
expr = (exp(h)-1)/h
# Compute limit
limit(expr, h, 0)

\(\displaystyle 1\)

What’s the point of all of these if SymPy is there to calculate everything for us?

  • Fair question.
  • SymPy let you avoid the tedious stuff.
  • But you are still involved in the setup.
  • It is still important to know how to start and to learn the various ways of thinking about the derivative.

Looking forward

  • Surely, there has to be shortcuts in calculating the derivative. (Still have some left)
  • How do we interpret these derivatives? We actually already started.
  • We may have to dig in a bit into the concept of getting “closer and closer”.
  • What if we have even more complicated functions? (Still have some left)
  • Where is the economics?