Matlab 使用匿名函数进行多维集成时出错
我尝试使用以下算法执行多维积分:Matlab 使用匿名函数进行多维集成时出错,matlab,integration,anonymous-function,Matlab,Integration,Anonymous Function,我尝试使用以下算法执行多维积分: y= @(a,b,c) a+b+c; %function - 3 dim y1=@(a,b) integral(@(c) y(a,b,c),-20,20); % L,H - limits for c` y2=@(a) integral(@(b) y1(a,b),-20,20); % L,H - limits for b y3=integral(@(a) y2(a),-20,20); % L,H - limits for a 但这会产生以下错误: 使用inte
y= @(a,b,c) a+b+c; %function - 3 dim
y1=@(a,b) integral(@(c) y(a,b,c),-20,20); % L,H - limits for c`
y2=@(a) integral(@(b) y1(a,b),-20,20); % L,H - limits for b
y3=integral(@(a) y2(a),-20,20); % L,H - limits for a
但这会产生以下错误:
使用integralCalc/finalInputChecks时出错(第515行)
函数的输出必须与输入的大小相同。
如果FUN是数组值被积函数,请将“arrayValue”选项设置为true。
integralCalc/IterateScalavalue中的错误(第315行)
最终支票(x,外汇);
integralCalc/vadapt中的错误(第132行)
[q,errbnd]=迭代Calavaled(u,tinterval,pathlen);
积分计算错误(第75行)
[q,errbnd]=vadapt(@AtoBInvTransform,interval);
积分误差(第88行)
Q=积分计算器(fun、a、b、opstruct);
积分误差(@(b)y1(a,b),-20,20)
integralCalc/IterateScalavalue中的错误(第314行)
fx=乐趣(t);
integralCalc/vadapt中的错误(第132行)
[q,errbnd]=迭代Calavaled(u,tinterval,pathlen);
积分计算错误(第75行)
[q,errbnd]=vadapt(@AtoBInvTransform,interval);
积分误差(第88行)
Q=积分计算器(fun、a、b、opstruct);
有人能帮我理解并纠正我的错误,或者建议一个更好的方法吗
附加说明我知道
integral3
函数,但我需要这个方法,因为我要尝试4,5,6。。。。尺寸稍后。我不确定这是否在任何可能的情况下都有效,但在这个简单的情况下效果非常好。就用符号数学吧
syms a b c
y=a+b+c;
y1=int(y,c,-20,20)
y2=int(y1,b,-20,20)
y3=int(y2,a,-20,20)
但是,要小心创建变量。不要动态创建yn
为了理解出现此错误的原因,让我们使用“常规”函数重写代码:
function q42536274
y3(-20,20);
end
function out = y(a,b,c)
out = a + b + c;
% The result of the above is some [1x150 double] vector. No problem here.
end
function out = y1(a,b,L,H)
out = integral(@(c)y(a,b,c),L,H);
% The result of the above is the scalar [-1.421085471520200e-14]. Problem!
end
function out = y2(a,L,H)
out = integral(@(b)y1(a,b,L,H),L,H);
end
function out = y3(L,H)
out = integral(@(a)y2(a,L,H),L,H);
end
这是发生错误时工作区的外观:
现在我们可以看到MATLAB在抱怨什么了:fx
和x
中的元素数量是不同的!在这种情况下,MATLAB应该如何进行数值积分<代码>0-顺序近似值?这是不明确的
我们需要告诉MATLAB如何摆脱这种混乱。我们可以这样做的方法之一是:
function out = y1(a,b,L,H)
out = ones(size(a))*integral(@(c)y(a,b,c),L,H);
% Now the result of the above is also a [1x150 double] vector. Yey!
end
function out = y2(a,L,H)
out = ones(size(a))*integral(@(b)y1(a,b,L,H),L,H);
% Same as above...
end
因此,我们得到了输出
-2.2737e-11
,它与0
的正确答案“非常接近”(谢谢,似乎没问题),以供将来参考:如果您需要调试帮助,请在问题中添加比“它不起作用”更多的信息。添加所需的行为或错误;)(非常感谢您的解释)