cpartial

Partial derivative of a multivariate, vector-valued function using the central difference approximation.

Back to Numerical Differentiation Toolbox Contents.

Contents

Syntax

pf = cpartial(f,x0,k)
pf = cpartial(f,x0,k,h)

Description

pf = cpartial(f,x0,k) numerically evaluates the partial derivative of $\mathbf{f}$ with respect to $x_{k}$ at $\mathbf{x}=\mathbf{x}_{0}$ using the central difference approximation with a default relative step size of $h=\varepsilon^{1/3}$, where $\varepsilon$ is the machine zero.

pf = cpartial(f,x0,k,h) numerically evaluates the partial derivative of $\mathbf{f}$ with respect to $x_{k}$ at $\mathbf{x}=\mathbf{x}_{0}$ using the central difference approximation with a user-specified relative step size $h$.

Input/Output Parameters

Variable Symbol Description Format
Input f multivariate, vector-valued function () 1×1
function_handle
x0 evaluation point n×1
double
k element of to differentiate with respect to 1×1
double
h (OPTIONAL) relative step size 1×1
double
Output pf partial derivative of with respect to , evaluated at m×1
double

Note

Example #1: Partial derivative of a scalar-valued function.

Approximate the partial derivative of $f(x,y)=x^{3}\sin{y}$ with respect to $y$ at at $(x,y)=(5,1)$ using the cpartial function, and compare the result to the true result of

$$\left.\frac{\partial f}{\partial y}\right|_{(x,y)=(5,1)}=5^{3}\cos{(1)}$$

First, we rewrite this function as $f(\mathbf{x})=x_{1}^{3}\sin{x_{2}}$.

f = @(x) x(1)^3*sin(x(2));

Since the second component of $\mathbf{x}$ represents $y$, to approximate the derivative, we use

k = 2;

Approximating the partial derivative using the cpartial function,

pf = cpartial(f,[5;1],k)
pf =

   67.5378

Calculating the error,

error = pf-5^3*cos(1)
error =

  -2.2063e-09

Example #2: Partial derivative of a vector-valued function.

Approximate the partial derivative of

$$\mathbf{f}(\mathbf{x})=\pmatrix{\sin{x_{1}}\sin{x_{2}}\cr\cos{x_{1}}\cos{x_{2}}}$$

with respect to $x_{1}$ at $\mathbf{x}=\mathbf{x}_{0}=(1,2)$ using the cpartial function, and compare the result to the true result of

$$\left.\frac{\partial\mathbf{f}}{\partial x_{1}}\right|_{\mathbf{x}=
\mathbf{x}_{0}}=\pmatrix{\cos{(1)}\sin{(2)}\cr-\sin{(1)}\cos{(2)}}$$

Defining the function in MATLAB,

f = @(x) [sin(x(1))*sin(x(2));cos(x(1))*cos(x(2))];

Approximating the partial derivative using the cpartial function,

x0 = [1;2];             % evaluation point
k = 1;                  % element of x to differentiate with respect to
pf = cpartial(f,x0,k)   % differentiation
pf =

    0.4913
    0.3502

Calculating the error,

error = pf-[cos(1)*sin(2);-sin(1)*cos(2)]
error =

   1.0e-11 *

   -0.9040
   -0.8263

See also

fpartial | ipartial