本文介绍了在Matlab中使用嵌套Integral2进行四重积分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试解决以下形式的问题:

I am trying to solve a problem of the following form:

f=@(x,y,z,w) x.*y.*z.*w;          % A complicated black box function
a=1;b=1;c=1;d=1;                  % Integration limits
I=integral2(@(x,y)integral2(@(z,w)f(x,y,z,w),c,-c,d,-d),a,-a,b,-b);

使用此实现,我得到以下错误:

Using this implementation I get the following error:

Error using  .*
Matrix dimensions must agree.

问题是x,y,z和w的大小不同.对于第一个函数评估,所有输入的大小均相同,但在第二个函数评估中,x和y与z和w的大小不同.

The problem is that x, y, z, and w are not the same size. For the first function evaluation all inputs are the same size but then on the second function evaluation x and y are not the same size as z and w.

如何解决此错误?

此问题与以下未解答的问题类似:使用嵌套的四倍积分集成的输入数组大小错误积分2

This question is similar to this unanswered question:Input array size error for a quadraple integration using nested integral2

================================================ ==================================

==================================================================================

回答:

I=integral(@(x)integral3(@(y,z,w)f(x,y,z,w),b,-b,c,-c,d,-d),a,-a,'ArrayValued',true);

这确实解决了问题,但是,对我来说,为什么这行得通并不明显.我以前实际上已经看过这种解决方案,但忘了在我的问题中提及它( http://www.mathworks.com/matlabcentral/answers/77571-how-to-perform-4d-integral-in-matlab ).

This does solve the problem, however, it is not obvious to me why this works. I had actually seen this solution before but forgot to mention it in my question (http://www.mathworks.com/matlabcentral/answers/77571-how-to-perform-4d-integral-in-matlab).

我想解决使用嵌套积分2的问题,因为我知道我的函数是不连续的,并且想使用迭代积分方法.我可以做这样的事情,但是只对内部积分进行迭代,所以我不确定这会如何影响精度:

I would like to solve using nested integral2 because I know my function is discontinuous and would like to use the iterated integration method. I could do something like this but only the inner integral is iterated so I am not sure how that affects accuracy:

I=integral(@(x)integral3(@(y,z,w)f(x,y,z,w),b,-b,c,-c,d,-d,'Method','iterated'),a,-a,'ArrayValued',true);

推荐答案

integral2用两个大小相同的矩阵参数调用其被积.问题是您不能像在问题中那样在函数调用f(x,y,z,w)中仅混合变量x,y,z,w,因为x和y的维数由外部integral2,而z和w的尺寸由内部integral2确定,因此不能保证尺寸相同. integralX函数无论如何都不进行矢量化,每个调用只能提供一个输出值.

integral2 calls its integrand with two matrix arguments of the same size. The problem is that you can't just mix the variables x,y,z,w in the function call f(x,y,z,w) as you did in your question, because the dimension of x and y is determined by the outer integral2, whereas the dimension of z and w is determined by the inner integral2, so is isn't guaranteed that the dimensions are the same. The integralX functions don't vectorize anyway, each call can provide just one output value.

函数integral提供了一个选项,使其仅使用标量值调用其被积数,并且通过Matlab标量扩展,可以与内部函数integral3提供的相同大小的3D数组一起使用.

The function integral provides an option so that it calls its integrand only with scalar values, and with Matlab scalar expansion, this works together with the same-sized 3D arrays the inner function integral3 provides.

I=integral(@(x)integral3(@(y,z,w)f(x,y,z,w),b,-b,c,-c,d,-d),a,-a,'ArrayValued',true);

您可以通过使用arrayfun封装内部inner2调用来实现相同的功能(使用标量调用):

You can achieve the same (calling with scalars) by encapsulating the inner integral2 call with arrayfun:

I=integral2(@(x,y)arrayfun(@(x,y)integral2(@(z,w)f(x,y,z,w),c1,c2,d1,d2),x,y),a1,a2,b1,b2)

在我的实验中,后者快了大约六倍.

The latter was about six times faster in my experiments.

这篇关于在Matlab中使用嵌套Integral2进行四重积分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-11 15:43