Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Matlab 对integral的嵌套调用失败_Matlab_Integration_Integral_Function Handle - Fatal编程技术网

Matlab 对integral的嵌套调用失败

Matlab 对integral的嵌套调用失败,matlab,integration,integral,function-handle,Matlab,Integration,Integral,Function Handle,试试这段代码,它很好用 a=1;b=2; % A two-variate function f2= @(x,y) x+y; derivedF2=@(x) integral(@(y) f2(x,y), a,b); % Test the evaluation of the derived function handle derivedF2(0); % Test the integration of the derived function handle % integralVal=integral

试试这段代码,它很好用

a=1;b=2;
% A two-variate function 
f2= @(x,y) x+y;
derivedF2=@(x) integral(@(y) f2(x,y), a,b);
% Test the evaluation of the derived function handle
derivedF2(0);
% Test the integration of the derived function handle
% integralVal=integral(derivedF2,a,b);
% integralVal=integral(@(x) derivedF2(x),a,b);
% Test plotting of the derived function handle
figure(11);
ezplot(derivedF2);
但是如果您取消注释以integralVal开头的行。密码被破解了


显然,派生函数句柄不支持集成操作,或者我遗漏了什么?

简短回答:您应该添加
'arrayvalue'
选项:

integralVal=integral(derivedF2,a,b, 'ArrayValued', true);
解释

您应该阅读错误消息:

函数的输出必须与输入的大小相同。如果FUN是数组值被积函数,则设置“ArrayValued” 选项为true

由于
derivedF2
是以向量化的方式计算的,即它通过提供
y
向量而不是单个标量,在不同的
y
坐标下同时计算
f
,因此MATLAB也无法以向量化的方式计算外部积分。因此,您应该将
“ArrayValue”
选项添加到外部积分,即:

integralVal=integral(derivedF2,a,b, 'ArrayValued', true);
请注意,
ezplot
也会生成以下相关警告:

警告:函数未能对数组输入求值;将函数矢量化可以加快其计算速度并避免 需要在数组元素上循环

请注意,该问题纯粹与对
integral
的嵌套调用有关,以下代码也会导致相同的错误:

integralVal=integral(@(x) integral(@(y) f2(x,y), a,b),a,b);
什么是函数?

。。。接受标量输入并返回向量、矩阵或N-D数组输出的函数

因此,
@(y)f2(x,y)
是一个数组值函数,如果
x
是一个数组,即它为
y
的标量输入返回一个数组

有两种可能避免数组值问题:

  • 避免
    @(y)f2(x,y)
    是数组值函数,即避免
    x
    是数组。这可以通过指示
    derivedF2
    是如上所述的数组值函数来实现,尽管严格来说,它不是数组值函数,即积分应该具有相同数量的输出和输入。但是,它在内部使用一个数组值函数,即
    @(x)f2(x,y)
    是一个数组值函数,因为默认情况下,Matlab以向量化的方式计算被积函数,即它使用一个向量表示
    y

  • 告诉Matlab,
    @(y)f2(x,y)
    是一个数组值函数:

    这可能是一种更直观的方法,但速度较慢,因为内部积分通常比外部积分更容易调用


数组值的另一种解释是告诉matlab不要使用矢量化,但对于这种解释,数组值的名称有点误导。

非常感谢,@m7913d。对于你们其余的人来说,阅读这个答案。修复ezplot警告的一种方法是将代码行更改为:“ezplot(@(x)arrayfun(derivedF2,x));”Hi@m7913d,随着我对“arrayvalue”选项的深入研究,我变得更加困惑。读到“如果将'ArrayValue'选项设置为true,则fun必须接受标量并返回固定大小的数组。”。并读取“将此标志设置为true,表示fun是一个接受标量输入并返回向量、矩阵或N-D数组输出的函数。”和“ArrayValued”,true表示被积函数是一个数组值函数。它让人想知道“数组值函数”到底是什么。。。抱歉,如果这是一个太基本的问题。
derivedF2=@(x) integral(@(y) f2(x,y), a,b, 'ArrayValued', true);