0

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?

Dev-iL
  • 23,742
  • 7
  • 57
  • 99
user2587726
  • 169
  • 2
  • 11
  • Please make sure that the code snippet you post contain the lines that give the error (as of now, this is not the case with `fitted = fitted.^2;`). Did you try the most basic form of numeric integration, which is evaluating your function at some fine grid, then multiplying the values by dx and summing? I bet it would work in your case. – Dev-iL Jun 17 '19 at 14:05
  • Oh my apologies, I'll add that in now :) – user2587726 Jun 17 '19 at 14:10

1 Answers1

2

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).

Dev-iL
  • 23,742
  • 7
  • 57
  • 99
  • Instructions unclear - do you mean replace the line fitted = @(x)fitted(x); with that line of code? – user2587726 Jun 17 '19 at 14:14
  • 1
    @user2587726 Judging by your question the instructions seem pretty clear :) In addition to what you said, you should obviously put `fsq` in the `integral`. – Dev-iL Jun 17 '19 at 14:19