| Curve Fitting Toolbox™ | ![]() |
d1 = differentiate(fun,x)
[d1,d2] = differentiate(...)
d1 = differentiate(fun,x) differentiates the cfit object fun at the points specified by the vector x and returns the result in d1. d1 is a column vector the same length as x.
[d1,d2] = differentiate(...) also returns the second derivative in d2. d2 is a column vector the same length as x.
For library models with closed forms, derivatives are calculated analytically. For all other models, the first derivative is calculated using the centered difference quotient
![]()
where x is the value at which the derivative is calculated, h is a small number (on the order of the cube root of eps), yx+h is fun evaluated at x+h, and yx–h is fun evaluated at x – h. The second derivative is calculated using the expression
![]()
Create a baseline sinusoidal signal:
xdata = (0:.1:2*pi)'; y0 = sin(xdata);
Add noise to the signal:
noise = 2*y0.*randn(size(y0)); % Response-dependent % Gaussian noise 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:
subplot(3,1,1)
plot(fit1,xdata,ydata) % cfit plot method
subplot(3,1,2)
plot(xdata,d1,'m') % double plot method
grid on
legend('1st derivative')
subplot(3,1,3)
plot(xdata,d2,'c') % double plot method
grid on
legend('2nd derivative')

Note that derivatives can also be computed and plotted directly with the cfit plot method, as follows:
plot(fit1,xdata,ydata,{'fit','deriv1','deriv2'})The plot method, however, does not return data on the derivatives.
![]() | dependnames | excludedata | ![]() |
| © 1984-2008- The MathWorks, Inc. - Site Help - Patents - Trademarks - Privacy Policy - Preventing Piracy - RSS |