Prev: W11 Next: W13

📗 Tuesday lectures: 4:00 to 4:50, Zoom, TopHat: Link (or Google Form: Form if TopHat not working). MATLAB.
📗 Programming Homework: P6

Slide:


# Numerical Differentiation

📗 Derivatives can be approximated using Newton's formula, \(\dfrac{d f}{d x} \approx \dfrac{f\left(x + h\right) - f\left(x\right)}{h}\), for some small \(h\) close to \(0\).
➩ The derivative of \(f\) is usually approximated by \(\dfrac{d f}{d x} \approx \dfrac{f\left(x + h\right) - f\left(x - h\right)}{2 h}\).
➩ There are more accurate approximations using finite differences, for example, \(\dfrac{d f}{d x} \approx \dfrac{-\left(f\left(x + 2 h\right)\right) + 8 f\left(x + h\right) - 8 f\left(x - h\right) + f\left(x - 2 h\right)}{12 h}\).
📗 Partial derivatives of a multivariate function are derivatives with respect to one of the variables holding the other variables constant: \(\dfrac{\partial f\left(x, y\right)}{\partial x} \approx \dfrac{f\left(x + h, y\right) - f\left(x - h, y\right)}{2 h}\) and \(\dfrac{\partial f\left(x, y\right)}{\partial y} \approx \dfrac{f\left(x, y + h\right) - f\left(x, y - h\right)}{2 h}\).
➩ The gradient of a multivariate function is hte vector of partial derivatives, one for each variable, \(\nabla f\left(x, y\right) = \begin{bmatrix} \dfrac{\partial f}{\partial x} \\ \dfrac{\partial f}{\partial y} \end{bmatrix}\).
TopHat Quiz
📗 \(\dfrac{d}{d x} x^{2} \bigg|_{x = 2}\)
📗 f = @(x)(x .^ 2); z = 2; h = 0.0001;
➩ A: (f(z + h) - f(z - h)) / h
➩ B: (f(z - h) - f(z + h)) / h
➩ C: (f(z + h) - f(z - h)) / (2 * h)
➩ D: (f(z - h) - f(z + h)) / (2 * h)
TopHat Quiz
📗 \(\dfrac{\partial }{\partial x_{2}} \left(x_{1}^{2} + x_{2}^{2} + x_{3}^{2}\right) \bigg|_{x = \begin{bmatrix} 1 \\ 2 \\ 3 \end{bmatrix}}\)
📗 f = @(x)(sum(x .^ 2)); z = [1 2 3]; h = 0.0001;
➩ A: (f(z + h) - f(z - h)) / (2 * h)
➩ B: (f(z + [h 0 0]) - f(z - [h 0 0])) / (2 * h)
➩ C: (f(z + [0 h 0]) - f(z - [0 h 0])) / (2 * h)
➩ D: (f(z + [0 0 h]) - f(z - [0 0 h])) / (2 * h)

# Derivative as a Function

📗 It is possible to compute the numerical derivative as a function (a MATLAB function, not a closed form expression of the function).
function d = df(f)
  h = 0.0001;
  d = @(x)((f(x + h) - f(x - h)) / (2 * h));
end
📗 diff(f) computes the derivative of \(f\) and returns a function. It requires MATLAB's Symbolic Math Toolbox.

# Numerical Integration or Quadrature

📗 Definite integrals can be approximated using finite Riemann sums.
➩ If the right Riemann sum is used, then \(\displaystyle\int_{x_{0}}^{x_{1}} f\left(x\right) d x \approx \displaystyle\sum_{i=1}^{n} f\left(x_{0} + i \cdot h\right) h\), for \(h = \dfrac{x_{1} - x_{0}}{n}\), for some large \(n\).
➩ The left Riemann sum (replace \(i\) by \(i - 1\)) can also be used.
➩ The mid-point rule can be used too (replace \(i\) by \(i - 0.5\)).
➩ The points at which the function is evaluated can be chosen adaptively or randomly.
TopHat Quiz
📗 \(\displaystyle\int_{0}^{1} x^{2}\)
📗 f = @(x)(x .^ 2)
➩ A: sum(f(0:0.01:1)) * 0.01
➩ B: sum(f(0.01:0.01:0.99)) * 0.01
➩ C: sum(f(0.005:0.01:0.995)) * 0.01
➩ D: sum(f(0:1))

# Indefinite Integral as a Function

📗 It is possible to compute the numerical integral as a function. Instead of having the \(+C\), an arbitrary constant can be used.
function d = intf(f)
  h = 0.01;
  d = @(x)(sum(f(h:h:x)) * h)
end
📗 integral(f, x0, x1) finds the numerical definite integral \(\displaystyle\int_{x_{0}}^{x_{1}} f\left(x\right) d x\).
📗 int(f) finds the indefinite integral of \(f\) and returns a function. It requires MATLAB's Symbolic Math Toolbox.

📗 Notes and code adapted from the course taught by Professors Beck Hasti and Michael O'Neill.
📗 You can expand all TopHat Quizzes and Discussions: .
📗 If there is an issue with TopHat during the lectures, please submit your answers on paper (include your Wisc ID and answers) or this Google form Form at the end of the lecture.





Last Updated: April 18, 2025 at 6:18 PM