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

时间:2021-05-24 09:16:41

I try to get an integral of two function handles in Matlab. The first function handle would be a weibull probability density function and the second function handle is based on a cfit I created with linear interpolation of single points.

我试着在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.

I receive the following error:

我收到以下错误:

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

The problem must have to do something with "fun2" since the code works just fine with e.g.

这个问题必须要用“fun2”来做,因为代码可以很好地使用。

fun2 = x.^2;

Note: if I plot fun2 with the cfitObject I don't get an error. It's also possible to integrate the function using quad().

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

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

Any help is greatly appreciated!

非常感谢您的帮助!

1 个解决方案

#1


0  

Your code seems to be ok. Probably the problem is that fun2 cannot take vectorized input, it could be resolved by modifying fun2 (cfitObject) to be able to handle vector input or telling the software that the function in the integral is array valued:

你的代码看起来不错。可能的问题是,fun2不能接受矢量化的输入,它可以通过修改fun2 (cfitObject)来解决,从而能够处理矢量输入,或者告诉软件在积分中函数是数组值:

y = integral(fun3, 0, 35, 'ArrayValued', 1);

#1


0  

Your code seems to be ok. Probably the problem is that fun2 cannot take vectorized input, it could be resolved by modifying fun2 (cfitObject) to be able to handle vector input or telling the software that the function in the integral is array valued:

你的代码看起来不错。可能的问题是,fun2不能接受矢量化的输入,它可以通过修改fun2 (cfitObject)来解决,从而能够处理矢量输入,或者告诉软件在积分中函数是数组值:

y = integral(fun3, 0, 35, 'ArrayValued', 1);