2  Calculus Prerequisites

You will recognize some of these concepts from your Calculus course. Other concepts are typically not introduced until Multivariate Calculus or Differential Equations. Fortunately, the set of tools we’ll need is relatively limited, so I’m confident you’ll understand everything after this chapter and classwork.

2.1 Derivatives

A derivative is a fundamental concept in calculus that measures how a function changes as its input changes. In simpler terms, the derivative represents the rate of change or the slope of the function at any given point.

Definition: For a function \(f(x)\), the derivative at a point \(x\) is defined as: \[f'(x) = \lim_{h \rightarrow 0} \frac{f(x + h) - f(x)}{h}\]

This limit, if it exists, gives us the instantaneous rate of change of the function at \(x\).


Notation:

  • \(f'(x)\): represents the derivative of \(f(x)\).
  • \(\frac{dy}{dx}\): another common notation, where \(y = f(x)\).

Geometric interpretation: The derivative at a point \(x\) is the slope of the tangent line to the curve \(y = f(x)\) at that point. If \(f(x)\) is a straight line, its derivative is the constant slope of the line.

Basic Rules of Differentiation:

  1. Constant Rule: The derivative of a constant is zero. \(\frac{d}{dx} [c] = 0\)

  2. Power Rule: For any real number \(n\), \(\frac{d}{dx} [x^n] = n x^{n-1}\)

  3. Sum Rule: The derivative of a sum is the sum of the derivatives. \(\frac{d}{dx} [f(x) + g(x)] = f'(x) + g'(x)\)

  4. Product Rule: For two functions \(f(x)\) and \(g(x)\), \(\frac{d}{dx} [f(x) g(x)] = f'(x) \cdot g(x) + f(x) g'(x)\)

  5. Chain Rule: For a composition of functions \(f(g(x))\), \(\frac{d}{dx} [f(g(x))] = f'(g(x)) \cdot g'(x)\)

Here are some functions I’ll use as examples:

library(tidyverse)

ggplot() +
  stat_function(fun = function(x) 3, color = "red") +
  stat_function(fun = function(x) 3 * x, color = "orange") +
  stat_function(fun = function(x) 3 * x^2, color = "yellow") +
  stat_function(fun = function(x) 3 * x^3 + 6 * x, color = "green") +
  xlim(-1.5, 1.5)

Practice Question: What is the derivative of \(f(x) = 3\) (red line in the figure above), evaluated at \(x = -1\), \(x = 0\), and \(x = 1\)?






Practice Question: What is the derivative of \(f(x) = 3x\) (orange line in the figure above), evaluated at \(x = -1\), \(x = 0\), and \(x = 1\)?






Practice Question: What is the derivative of \(f(x) = 3x^2\) (yellow line in the figure above), evaluated at \(x = -1\), \(x = 0\), and \(x = 1\)?






Practice Question: What is the derivative of \(f(x) = 3x^3 + 6x\) (green line in the figure above), evaluated at \(x = -1\), \(x = 0\), and \(x = 1\)?






2.2 Functions of Several Variables

Functions of several variables extend the concept of a single-variable function to cases where the output depends on more than one input. These functions are crucial in many fields, including economics, engineering, and physics.

A function of several variables is a rule that assigns a single value to a set of input values. For example, a function \(f\) of two variables \(x\) and \(y\) is written \(f(x, y)\).

Notation and Examples

  1. Temperature Distribution: \(T(x, y)\) might represent the temperature at a point \((x, y)\) on a surface.

  2. Utility Function: \(u(x_1, x_2)\) could represent the utility a consumer derives from consuming quantities \(x_1\) of good 1 and \(x_2\) of good 2.


You can visualize a function of two variables as a surface in three dimensions. This plot visualizes the function \(f(x, y) = xy\).

2.3 Partial Derivatives

Partial derivatives extend the concept of derivatives to functions of several variables. They measure how the function changes as one of the input variables changes, while the others are held constant. Geometrically, partial derivaties answer the question: on the surface \(f(x, y)\), if you move a little in the \(x\) direction, how much will \(f(x, y)\) change?

The partial derivative of \(f(x, y)\) with respect to \(x\), denoted \(\frac{\partial f}{\partial x}\), is: \[\frac{\partial f}{\partial x} = \lim_{h \rightarrow 0} \frac{f(x + h, y) - f(x, y)}{h}\] Similarly, the partial derivative with respect to \(y\) is: \[\frac{\partial f}{\partial y} = \lim_{h \rightarrow 0} \frac{f(x, y + h) - f(x, y)}{h}\]


The rules for taking partial derivatives are similar to those for ordinary derivatives, with the key difference being that we treat all other variables as constants. Here are some common rules:

  1. Power rule: for \(f(x, y) = x^n\), \(\frac{\partial f}{\partial x} = n x^{n-1}\) while treating \(y\) as a constant.

    Examples: \[\frac{\partial f}{\partial x}[x^2 y] = 2xy\] \[\frac{\partial f}{\partial y}[x^2 y] = x^2\]

  2. Sum rule: The partial derivative of a sum is the sum of the partial derivatives. \(\frac{\partial f}{\partial x}[f(x, y) + g(x, y)] = \frac{\partial f}{\partial x} + \frac{\partial g}{\partial x}\)

    Examples: \[\frac{\partial f}{\partial x}[x^2 + y] = 2x\] \[\frac{\partial f}{\partial y}[x^2 + y] = 1\]

  3. Product rule: For two functions \(f(x, y)\) and \(g(x, y)\), \(\frac{\partial f}{\partial x}[f(x, y) \cdot g(x, y)] = \frac{\partial f}{\partial x} \cdot g(x, y) + f(x, y) \cdot \frac{\partial g}{\partial x}\)

Practice Question: What is the partial derivative of \(f(x, y) = x^2y + 3xy\) with respect to \(x\)?






2.4 Total Differentials

The concept of a total differential is an extension of the derivative for functions of several variables. It provides a way to approximate the change in a function based on the changes in its input variables.

For a function \(f(x, y)\) of two variables, the total differential \(df\) is an expression that approximates the change in \(f\) given small changes in \(x\) and \(y\). It is given by: \(df = \frac{\partial f}{\partial x} dx + \frac{\partial f}{\partial y} dy\), where \(dx\) and \(dy\) represent small changes in the variables \(x\) and \(y\).

Geometrically, the total differential \(df\) can be interpreted as the tangent plane to the surface defined by \(f(x, y)\) at a particular point.

Example:

Consider the function \(f(x, y) = x^2 y + 3 xy\). To find the total differential \(df\), follow these steps:

  1. Compute the partial derivatives:

    • \(\frac{\partial f}{\partial x} = 2xy + 3y\)
    • \(\frac{\partial f}{\partial y} = x^2 + 3x\)
  2. Form the total differential:

    • \(df = (2xy + 3y) dx + (x^2 + 3x) dy\)

The total differential gives you insights about the sensitivity of the function to each variable, aiding in sensitivity analysis and optimization.

You can use the total differential to approximate the change in \(f(x, y)\) given small changes in \(x\) and \(y\). For instance, if \(x\) changes from 1 to 1.01 and \(y\) changes from 2 to 2.02, how much do we approximate \(f(x, y) = x^2 y + 3xy\) will change by? Plug in numbers to the total differential:

\[\begin{align} df &= (2(1)(2) + 3(2)) (1.01 - 1) + (1^2 + 3(1)) (2.02 -2)\\ df &= 0.18 \end{align}\]

Note, that’s pretty close to the true change in \(f\), which I can find by evaluating \(f\) at (1, 2) versus at (1.01, 2.02):

f <- function(x, y) x^2 * y + 3 * x * y

f(1.01, 2.02) - f(1, 2)
[1] 0.181202

2.5 Classwork 2

CW02: R as a Graphing Calculator

Answer questions a-g in a quarto document.

Numeric vectors: In R, data is held in vectors. A vector is a list of elements, where all the elements have the same type (for example, a numeric vector). You can create a vector by using the c() function, which stands for “combine.” For instance, c(6, 3, 8, 2) creates a numeric vector containing the values 6, 3, 8, and 2, resulting in a vector of length 4. To determine the length of a vector, you can use the length() function. For example, length(c(6, 3, 8, 2)) will return 4, indicating that the vector contains four elements.

  1. Combine the numbers 1 and 0 to create a numeric vector of length 5, and then verify its length using the length() function.

Character vectors: In R, character strings represent text, and unlike numbers, they must be enclosed in quotes. For example, to create a character string for the word “apple”, you need to put it in quotation marks. This distinguishes text data from numeric data.

  1. Combine several character strings to form a character vector of length 3, using the names of some of your favorite fruits. For example, c("apple", "banana") will create a character vector containing two elements. You can then confirm the length of this vector using the length() function.

Variable Assignment: In R, you can create a variable by using the assignment operator, which is represented by a left-facing arrow <-. This arrow assigns a value to a variable. For example, if you want to create a variable named x that holds the numeric vector c(6, 3, 8, 2), you would write: x <- c(6, 3, 8, 2). You can think of this as saying, “x gets the value c(6, 3, 8, 2).” Once you run this line of code, x will appear in your global environment (typically in the upper right-hand pane of your RStudio interface) as a variable that refers to this numeric vector.

You can then use x in various operations without changing its original value. For example, you can find its length with length(x). If you want to see what the values of x would be if each element were doubled, you can use 2 * x. Similarly, x + 5 will show you what the values would be if you added 5 to each element. Note that these operations do not change the value of x; they simply show you the results of these calculations.

  1. Create a variable y and assign it a numeric vector of length 3. Then, use operations to see what happens when you double y and when you add five to each element of y, keeping in mind that these operations do not alter the original value of y.

Functions: In the previous problem, you created a small program where you had some data (stored in a variable called y) and manipulated that data by doubling it or adding five. In R, any such program can be wrapped inside a function. A function allows you to package code into a reusable form that can take inputs, perform operations, and return an output.

The basic syntax for creating a function in R looks like this:

function(arguments) {
  body
    return(output)
}
  • arguments: This is a list of inputs that the function will accept, such as data.
  • body: This is the code that defines what the function does with the input arguments.
  • output: This is the result that the function returns.

For example, here’s a simple function that takes a vector x and returns the vector with each element doubled:

function(x) {
    return(2 * x)
}

If you omit the return statement, the function will automatically return the last expression it evaluates. So, an even simpler way to write this function is:

function(x) {
    2 * x
}

If the function body is just one line of code, you can simplify it even further by leaving out the curly brackets and writing everything in one line:

function(x) 2 * x
  1. Write a function that takes a vector y and adds five to every element.

Evaluating functions: Once you’ve created a function, you can evaluate it by passing in different inputs. The general syntax for evaluating a function is f(x), where f is the function and x represents the input values.

In the previous example, we created a function that doubles each element of a vector. Since this function is anonymous (meaning it doesn’t have a specific name), we can evaluate it directly by wrapping it in parentheses and providing the input values right after:

(function(x) 2 * x)(x = c(5, 8, 2))
[1] 10 16  4

In this example, x = c(5, 8, 2) is the input vector, and the function returns a vector with each element doubled.

In R, you can provide inputs either by name x (as in the example above) or by their position. This means the following code is also valid (you can leave out the x = part):

(function(x) 2 * x)(c(5, 8, 2))
[1] 10 16  4

Here, the vector c(5, 8, 2) is passed to the function as its first (and only) argument, and it produces the same output.

  1. Evaluate the function you wrote in part d on some vector of inputs.

  2. Assign the function you wrote in part d to a variable named f using the assignment operator <-. This will give your function a name, allowing you to reuse it easily. Once you’ve done this, you should be able to evaluate the function by running f(y = c(1, 2, 3)), and it should return the result c(6, 7, 8), which adds 5 to each element of the input vector y.

  3. Consider the following chunk of code from Chapter 2 of the workbook. Label all the functions (named and anonymous). Label all the arguments the functions take. Label all the character strings. Additionally, experiment with the code by changing the arguments for xlim. What effect does xlim have on the output? (Remember that before this chunk of code can run, you need to attach the tidyverse to your current R session: library(tidyverse) because functions like ggplot() come from the tidyverse.

ggplot() +
  stat_function(fun = function(x) 3, color = "red") +
  stat_function(fun = function(x) 3 * x, color = "orange") +
  stat_function(fun = function(x) 3 * x^2, color = "yellow") +
  stat_function(fun = function(x) 3 * x^3 + 6 * x, color = "green") +
  xlim(-1.5, 1.5)