This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English version of the page.

Note: This page has been translated by MathWorks. Click here to see
To view all translated materials including this page, select Country from the country navigator on the bottom of this page.


Differentiate cfit or sfit object


fx = differentiate(FO, X)
[fx, fxx] = differentiate(...)
[fx, fy] = differentiate(FO, X, Y)
[fx, fy] = differentiate(FO, [x, y])
[fx, fy, fxx, fxy, fyy] = differentiate(FO, ...)


For Curves

fx = differentiate(FO, X) differentiates the cfit object FO at the points specified by the vector X and returns the result in fx.

[fx, fxx] = differentiate(...) also returns the second derivative in fxx.

All return arguments are the same size and shape as X.

For Surfaces

[fx, fy] = differentiate(FO, X, Y) differentiates the surface FO at the points specified by X and Y and returns the result in fx and fy.

FO is a surface fit (sfit) object generated by the fit function.

X and Y must be double-precision arrays and the same size and shape as each other.

All return arguments are the same size and shape as X and Y.

If FO represents the surface z=f(x,y), then FX contains the derivatives with respect to x, that is, dfdx, and FY contains the derivatives with respect to y, that is, dfdy.

[fx, fy] = differentiate(FO, [x, y]), where X and Y are column vectors, allows you to specify the evaluation points as a single argument.

[fx, fy, fxx, fxy, fyy] = differentiate(FO, ...) computes the first and second derivatives of the surface fit object FO.

fxx contains the second derivatives with respect to x, that is, 2fx2.

fxy contains the mixed second derivatives, that is, 2fxy.

fyy contains the second derivatives with respect to y, that is, 2fy2.


For Curves

Create a baseline sinusoidal signal:

xdata = (0:.1:2*pi)';
y0 = sin(xdata);

Add response-dependent Gaussian noise to the signal:

noise = 2*y0.*randn(size(y0)); 											
ydata = y0 + noise;

Fit the noisy data with a custom sinusoidal model:

f = fittype('a*sin(b*x)');
fit1 = fit(xdata,ydata,f,'StartPoint',[1 1]);

Find the derivatives of the fit at the predictors:

[d1,d2] = differentiate(fit1,xdata);

Plot the data, the fit, and the derivatives:

plot(fit1,xdata,ydata) % cfit plot method
plot(xdata,d1,'m') % double plot method
grid on
legend('1st derivative')
plot(xdata,d2,'c') % double plot method
grid on
legend('2nd derivative')

You can also compute and plot derivatives directly with the cfit plot method, as follows:


The plot method, however, does not return data on the derivatives, unlike the differentiate method.

For Surfaces

You can use the differentiate method to compute the gradients of a fit and then use the quiver function to plot these gradients as arrows. The following example plots the gradients over the top of a contour plot.

x = [0.64;0.95;0.21;0.71;0.24;0.12;0.61;0.45;0.46;...
y = [0.42;0.84;0.83;0.26;0.61;0.58;0.54;0.87;0.26;...
z = [0.49;0.051;0.27;0.59;0.35;0.41;0.3;0.084;0.6;...
fo = fit( [x, y], z, 'poly32', 'normalize', 'on' );
[xx, yy] = meshgrid( 0:0.04:1, 0:0.05:1 );

[fx, fy] = differentiate( fo, xx, yy );

plot( fo, 'Style', 'Contour' );
hold on
h = quiver( xx, yy, fx, fy, 'r', 'LineWidth', 2 );
hold off
colormap( copper )

If you want to use derivatives in an optimization, you can, for example, implement an objective function for fmincon as follows.

function [z, g, H] = objectiveWithHessian( xy )
        % The input xy represents a single evaluation point
        z = f( xy );
        if nargout > 1
            [fx, fy, fxx, fxy, fyy] = differentiate( f, xy );
            g = [fx, fy];
            H = [fxx, fxy; fxy, fyy];


For library models with closed forms, the toolbox calculates derivatives analytically. For all other models, the toolbox calculates the first derivative using the centered difference quotient


where x is the value at which the toolbox calculates the derivative, Δx is a small number (on the order of the cube root of eps), f(x+Δx) is fun evaluated at x+Δx, and f(xxΔ) is fun evaluated at xΔx.

The toolbox calculates the second derivative using the expression


The toolbox calculates the mixed derivative for surfaces using the expression


See Also

| |

Introduced before R2006a