08 Differential Calculus#

This branch focuses on the concept of a derivative, which can be loosely thought of as how much one quantity is changing in response to changes in some other quantity. Differential calculus is used to solve problems of finding maximum and minimum values of functions, rates of change, and slope of curves.

Derivative of Function f(x)#

For a general function \(f(x)\), the derivative \(f'(x)\) can be found by applying the rules of differentiation. Here are some basic rules:

  1. Constant Rule: The derivative of a constant is 0.

  2. Power Rule: The derivative of \(x^n\) is \(n*x^{n-1}\).

  3. Product Rule: The derivative of \(u*v\) is \(u'*v + u*v'\).

  4. Quotient Rule: The derivative of \(u/v` is `(u'*v - u*v') / v^2\).

  5. Chain Rule: The derivative of \(f(g(x)) ` is ` f'(g(x)) * g'(x)\).

Where u and v are functions of x, and u' and v' are their respective derivatives.

For example, if \(f(x) = x^2\), then using the power rule, \(f'(x) = 2*x^{2-1} = 2*x\).

First and Second Derivatives (Primes)#

Find the derivatives for function $\( f(x) = (1/3)x^3 - x^2 - 3x + 10 \)$.

To find the first derivative, we apply the power rule for each term:

  1. The derivative of \((1/3)x^3\) is \(x^2\).

  2. The derivative of \(-x^2\) is \(-2x\).

  3. The derivative of \(-3x\) is \(-3\).

  4. The derivative of a constant (like \(10\)) is \(0\).

So, the first derivative of f(x) is f'(x) = x^2 - 2x - 3.

To find the second derivative, we again apply the power rule to each term of the first derivative:

  1. The derivative of \(x^2\) is \(2x\).

  2. The derivative of \(-2x\) is \(-2\).

  3. The derivative of \(-3\) is \(0\).

So, the second derivative of \(f(x)\) is \(f''(x) = 2x - 2\).

In Python, you can use the sympy.diff function to compute derivatives. For example:

import sympy as sp

x = sp.symbols('x')
f = (1/3)*x**3 - x**2 - 3*x + 10

# First derivative
f_prime = sp.diff(f, x)
print(f"First derivative: {f_prime}")

# Second derivative
f_double_prime = sp.diff(f_prime, x)
print(f"Second derivative: {f_double_prime}")
First derivative: 1.0*x**2 - 2*x - 3
Second derivative: 2.0*x - 2

Optimization via Calculus#

Optimization using calculus involves finding the maximum or minimum values of a function. This is often done by finding the derivative of the function and setting it equal to zero, which gives the critical points. The second derivative can then be used to determine whether these points are maxima, minima, or neither.

Let’s optimize the function \(f(x) = 1/3x^3 - x^2 - 3x + 10\) on the interval \([-4, 4]\).

  1. First, find the derivative of f(x). The derivative of f(x) is f'(x) = x^2 - 2x - 3.

  2. Set the derivative equal to zero and solve for x to find the critical points. This is a quadratic equation i.e. \(f'(x) = 0\) or \(x^2 - 2x - 3 = 0\). You can represent the equation in the form of

\[y = ax^{2} + bx + c\]

where a = 1, b = -2, and c = -3. You can now use the quadratic formulae

\[ x = \frac{[-b ± \sqrt{b^2 - 4ac}]} {2a} \]
  • For b^2 - 4ac. it would be, (-2)^2 - 4*1*(-3) = 4 + 12 = 16. The square root of 16 is 4

  • Substitute b, a, and the square root of the discriminant into the quadratic formula to find the solutions. This gives x = [2 ± 4] / (2*1), which simplifies to \([-1, 3]\) or x1 = -1 and x2 = 3.

Here is Python example

import sympy as sp

x = sp.symbols('x')
equation = x**2 - 2*x - 3

# Solve the equation
solutions = sp.solve(equation, x)

print(f"Solutions: {solutions}")
Solutions: [-1, 3]

Local maxima or minima#

In calculus, the second derivative test is a method to classify the local maximum and local minimum of a function. Here’s how it works:

Compute the second derivative of the function and substitute each critical point into the second derivative:

  • If the second derivative at that point is positive \((f''(c) > 0)\), then the function has a local minimum at that point.

  • If the second derivative at that point is negative \((f''(c) < 0)\), then the function has a local maximum at that point.

  • If the second derivative at that point is zero \((f''(c) = 0)\), then the test is inconclusive, and you’ll need to use another method to classify the point.

Continuing the above optimization problem…

  1. Next will dertimine the local maxima or minima using the second derivative. The second derivative of \(f(x)\) is \(f''(x) = 2x - 2\).. Since f''(-1) = -4 is negative, x1 = -1 is a local maximum. Since f''(3) = 4 is positive, x2 = 3 is a local minimum.

Here’s how you can do this in Python:

import sympy as sp

x = sp.symbols('x')
f = (1/3)*x**3 - x**2 - 3*x + 10

# Find the derivative
f_prime = sp.diff(f, x)

# Solve for critical points
critical_points = sp.solve(f_prime, x)

# Find the second derivative
f_double_prime = sp.diff(f_prime, x)

# Determine maxima, minima, or neither
for point in critical_points:
    if f_double_prime.evalf(subs={x: point}) < 0:
        print(f"Local maximum at x = {point}, f(x) = {f.evalf(subs={x: point})}")
    elif f_double_prime.evalf(subs={x: point}) > 0:
        print(f"Local minimum at x = {point}, f(x) = {f.evalf(subs={x: point})}")
    else:
        print(f"x = {point} is neither a maximum nor a minimum.")
Local maximum at x = -1.00000000000000, f(x) = 11.6666666666667
Local minimum at x = 3.00000000000000, f(x) = 1.00000000000000

Global maxima and minima#

In calculus, global (or absolute) maxima and minima are the highest and lowest values that a function achieves on its entire domain, respectively.

  • A global maximum is a point where the function’s value is greater than or equal to the values of all other points in the entire domain of the function. It’s the highest peak of the function.

  • A global minimum is a point where the function’s value is less than or equal to the values of all other points in the entire domain of the function. It’s the lowest valley of the function.

Continuing the above optimization problem…

  1. Finally, evaluate the function at the critical points and the endpoints of the interval. The maximum and minimum values of the function on the interval are the highest and lowest of these values, respectively.

import sympy as sp

x = sp.symbols('x')
f = (1/3)*x**3 - x**2 - 3*x + 10

# endpoints [-4, 4]
endpoints = [-4, 4]

# Evaluate the function at the endpoints [-4, 4] and critical points
result = dict()
for point in endpoints + critical_points:
    result[point] = f.evalf(subs={x: point})
    # print(f"f({point}) = {f.evalf(subs={x: point})}")

global_maximum = max(result.items(), key=lambda x: x[1])
global_minimum = min(result.items(), key=lambda x: x[1])

print("On points ", endpoints + critical_points)
print(f"Global maximum is at point {global_maximum[0]} with value {global_maximum[1]}")
print(f"Global minimum is at point {global_minimum[0]} with value {global_minimum[1]}")
On points  [-4, 4, -1.00000000000000, 3.00000000000000]
Global maximum is at point -1.00000000000000 with value 11.6666666666667
Global minimum is at point -4 with value -15.3333333333333