如何从 cfit 创建函数句柄,与其他函数句柄相乘并积分该项?

问题描述 投票:0回答:1

我尝试在Matlab中获得两个函数句柄的积分。第一个函数句柄是威布尔概率密度函数,第二个函数句柄基于我通过单点线性插值创建的 cfit。

x = 0:0.1:35;
fun1 = @(x) wblpdf(x,weibullAlpha,weibullBeta);
fun2 = @(x) feval(cfitObject,x);
fun3 = @(x) (fun(x).*fun2(x));
y = integral(fun3,0,35); % Using quad(fun3,0,35) doesn't work either.

我收到以下错误:

Error using integralCalc/finalInputChecks (line 515)
Output of the function must be the same size as the input. If FUN is an array-valued  integrand,
set the 'ArrayValued' option to true.

Error in integralCalc/iterateScalarValued (line 315)
            finalInputChecks(x,fx);

Error in integralCalc/vadapt (line 132)
        [q,errbnd] = iterateScalarValued(u,tinterval,pathlen);

Error in integralCalc (line 75)
    [q,errbnd] = vadapt(@AtoBInvTransform,interval);

Error in integral (line 88)
Q = integralCalc(fun,a,b,opstruct);

Error in test (line 7) % "test" is the name of the script file.
y = integral(fun3,0,35);

问题必须与“fun2”有关,因为代码可以正常工作,例如

fun2 = x.^2;

注意:如果我用 cfitObject 绘制 fun2 ,我不会收到错误。也可以使用quad() 来集成该函数。

x = 0:0.1:35;
fun2 = @(x) feval(cfitObject,x);
y = quad(fun2,0,35);
plot(x, fun2(x))

非常感谢任何帮助!

matlab curve-fitting numerical-integration function-handle
1个回答
0
投票

你的代码似乎没问题。问题可能是

fun2
无法接受向量化输入,可以通过修改
fun2
(
cfitObject
) 以能够处理向量输入或告诉软件积分中的函数是数组值来解决:

y = integral(fun3, 0, 35, 'ArrayValued', 1);
© www.soinside.com 2019 - 2024. All rights reserved.