拟合 "linearinterp" 的 Matlab 积分返回错误 "First input argument must be a function handle"

Matlab integrate of fitted "linearinterp" returning error "First input argument must be a function handle"

正在尝试对一些实验收集的数据进行积分。

在使用包络函数和 abs 函数后,我使用拟合函数得到我希望积分的方程(不幸的是,“poly”没有给出足够接近的数据拟合):

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

dat = abs(yupper);

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

然后当我尝试

q = integral(f,3e4,9e4);

我收到错误:

Error using integral (line 82) First input argument must be a function handle.

Error in findenergyfromfitcurve (line 10) q = integral(f,3e4,9e4);

我以为 f 是一个(数学)函数,不明白错误告诉我什么。当我尝试使用“poly3”时,如果是 linearinterp 搞砸了,我仍然会收到该错误。

TIA

  • f 是一个函数,但它的类型是 cfit 而不是 function handle.

  • integral()函数需要函数句柄,你能做什么 在获取之前将 cfit 转换为 function handle 积分

代码如下

x = rand(5,1);
dat = rand(5,1);
f = fit(x,dat,'linearinterp');

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

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


2) What does the ... 'Array valued', 1) do as well? It didn't work till I put that in so it must do something

f是一个分段函数,下图是假设f是一个2分段线性function,但它也可以用于 n-piecewise function

fit() 函数的任务是查找参数:

  • a
  • b
  • c
  • d
  • k

在代码方面f看起来像

function y = f(x,a,b,c,d,k)
    % PIECEWISELINE   A line made of two pieces
    % that is not continuous.

    y = zeros(size(x));

    % This example includes a for-loop and if statement
    % purely for example purposes.
    for i = 1:length(x)
        if x(i) < k
            y(i) = a.* x(i) + b;
        else
            y(i) = c.* x(i) + d;
        end
    end
end

要绘制函数句柄,只需使用 fplot(f)

这是 f 的图表


To sum up, f probably has more than one expression, that's why I set 'ArrayValued' to true so that integral() function knowns f has more than one expression, omitting it means f has a single expression which is not true.