Learn R Programming

vectorialcalculus (version 1.0.5)

total_differential_nd: Total differential of a scalar field in R^n

Description

Computes the gradient of a scalar field f(x) at a point x0 using central finite differences. It also returns the total differential, understood as the linear map v -> grad f(x0) %*% v.

Usage

total_differential_nd(f, x0, h = NULL)

Value

A list with components:

  • point: the numeric vector x0,

  • value: the numeric value f(x0),

  • gradient: numeric vector with the gradient at x0,

  • differential: a function d(v) that returns sum(gradient * v).

Arguments

f

Function of one argument x (numeric vector) returning a numeric scalar.

x0

Numeric vector giving the evaluation point.

h

Step size for finite differences. Can be NULL (automatic), a scalar, or a vector of the same length as x0.

Details

The function f must take a single numeric vector x and return a single numeric value.

Examples

Run this code
f <- function(x) x[1]^2 + 3 * x[2]^2
out <- total_differential_nd(f, c(1, 2))
out$gradient
out$differential(c(1, 0))  # directional derivative in direction (1,0)

Run the code above in your browser using DataLab