matlabobjectcurve-fittingnumerical-integrationfunction-handle

How to square the result of cfit?


I'm using MATLAB for some data analysis of experimentally collected data. After using the envelope and abs functions I'm using the fit function to get the equation I'm after, which I need to square then integrate.

The code I have for getting the equation and integrating it is as below:

[yupper,ylower] = envelope(signal,1000,'peak');

dat = abs(yupper);

fitted = fit(x,dat,'linearinterp');

% Create a new function handle
fitted = @(x)fitted(x);

q = integral(fitted, 3e4,9e4,  'ArrayValued', 1)

To square the function I am using (used with and without the . in .^):

%square fitted
fitted = fitted.^2;

Where I'm struggling is when I try and square f before converting to function handle I get the error(s):

Undefined operator '.^' for input arguments of type 'cfit'.

Error in findenergyfromfitcurve (line 5) fitted = fitted.^2;

Undefined operator '^' for input arguments of type 'cfit'.

Error in findenergyfromfitcurve (line 5) fitted = fitted^2;

and when I convert to function handle then square I get the same errors for the function handle:

Undefined operator '^' for input arguments of type 'function_handle'.

Error in findenergyfromfitcurve (line 10)
fitted = fitted^2;

Undefined operator '.^' for input arguments of type 'function_handle'.

Error in findenergyfromfitcurve (line 10)
fitted = fitted.^2;

In short - how do I square the output of the fit function?


Solution

  • I think this would do the trick:

    fsq = @(x)f(x).^2;
    

    I expect this to work because you would be squaring numbers (the outputs of f(x)) and not function handles or cfit objects (the object f itself).