MATLAB: How to fit a curve to a function and then integrate over that curve using the Curve Fitting Toolbox 1.2.2 (R2008b)

Curve Fitting Toolbox

I would like to fit a curve to a dataset and then integrate over it.

Best Answer

One way to accomplish this goal is to use a combination of the FIT and INTEGRATE functions as shown in the documentation for the INTEGRATE function which can be found by typing 'doc integrate' at the MATLAB Command Prompt.
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 integral of the fit at the predictors:
int = integrate(fit1,xdata,0);
Plot the data, the fit, and the integral:
subplot(2,1,1)
plot(fit1,xdata,ydata) % cfit plot method
subplot(2,1,2)
plot(xdata,int,'m') % double plot method
grid on
legend('integral')