用匿名函数实现MATLAB积分

用匿名函数实现MATLAB积分,matlab,math,Matlab,Math,我正在尝试使用MATLABintegral()函数来沿表示时变值的分段线性函数的单个参数进行积分 我想在原始函数的基础上定义一个匿名函数: t1 = 1; t2 = 2; t3 = 4; t4 = 5; a0 = 1; a1 = 2; f = @(x) accel_profile(t1,t2,t3,t4,a0,a1,x); 这是accel_配置文件.m: function value = accel_profile(t1,t2,t3,t4,a0,a1, t) if t <= t1

我正在尝试使用MATLAB
integral()
函数来沿表示时变值的分段线性函数的单个参数进行积分

我想在原始函数的基础上定义一个匿名函数:

t1 = 1;
t2 = 2;
t3 = 4;
t4 = 5;

a0 = 1;
a1 = 2;

f = @(x) accel_profile(t1,t2,t3,t4,a0,a1,x);
这是accel_配置文件.m:

function value = accel_profile(t1,t2,t3,t4,a0,a1, t)

if t <= t1
    value = a0;
    return
elseif (t <= t2)
    value = ((t-t1)/(t2-t1)) * (a1-a0) + a0;
    return
elseif (t <= t3)
    value = a1;
    return
elseif (t <= t4)
    value = ((t-t3)/(t4-t3)) * (a0-a1) + a1;
    return
else
    value = a0;
    return
end
我得到以下堆栈跟踪:

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); 
515             error(message('MATLAB:integral:FxNotSameSizeAsX'));

我正在Windows 7上运行MATLAB 2015b。

问题是
integral
为函数使用矢量化参数,但函数不支持它

文件中的相关部分:

对于标量值问题,函数y=fun(x)必须接受向量参数x,并返回向量结果y。这通常意味着fun必须使用数组运算符而不是矩阵运算符。例如,使用。*(时间)而不是*(mtimes)。如果将“ArrayValue”选项设置为true,则fun必须接受标量并返回固定大小的数组

这意味着integral将使用类似于
f([1,2,3])
的参数调用函数
f
,并需要一个带有
[f(1),f(2),f(3)]


通用技术

问题是
积分
使用向量化参数作为函数,但函数不支持它

文件中的相关部分:

对于标量值问题,函数y=fun(x)必须接受向量参数x,并返回向量结果y。这通常意味着fun必须使用数组运算符而不是矩阵运算符。例如,使用。*(时间)而不是*(mtimes)。如果将“ArrayValue”选项设置为true,则fun必须接受标量并返回固定大小的数组

这意味着integral将使用类似于
f([1,2,3])
的参数调用函数
f
,并需要一个带有
[f(1),f(2),f(3)]


一般的技巧

Aww,比我快:(将
'arrayvalue'
设置为true也应该有效:然后只使用标量参数调用函数。这样就不需要矢量化(但应该这样做;这样会更快)@AndrasDeak:我以为这个选项应该用于返回向量的函数,我在这里没有使用它。我的matlab版本太旧了,没有这个选项,如果它真的解决了问题,请写一个答案。将“ArrayValued”设置为“true”成功了,谢谢!我读了这篇文章,认为这只是用于返回数组值的函数,所以我的bad!事实上,丹尼尔,它是有效的,我刚刚检查过。谢谢,但我不认为仅仅因为这个就有必要添加一个新的答案:)为了完整性,请随意添加到您自己的答案中。它是为数组值函数设计的,但实际上它只是避免了使用向量值输入来加速。哦,先告诉我:(将
'ArrayValued'
设置为true也会起作用:然后只使用标量参数调用函数。这样就不需要进行向量化(但你应该这样做;这样会快得多)@AndrasDeak:我以为这个选项应该用于返回向量的函数,我在这里没有使用它。我的matlab版本太旧了,没有这个选项,如果它真的解决了问题,请写一个答案。将“ArrayValued”设置为“true”成功了,谢谢!我读了这篇文章,认为这只是用于返回数组值的函数,所以我的bad!事实上,丹尼尔,它是有效的,我刚刚检查过。谢谢,但我不认为仅仅因为这个就有必要添加一个新的答案:)为了完整性,请随意添加到您自己的答案中。它用于数组值函数,但实际上它只是避免使用向量值输入来加速。
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); 
515             error(message('MATLAB:integral:FxNotSameSizeAsX'));