How to fastly compute a double integral of a self-defined function?
2 次查看(过去 30 天)
显示 更早的评论
Hi everyone,
I need to compute
, where
is my self-defined function, M is infinity and in actual calculation I set it to be a large number and
is the probability density function of standard bivariate normal. My code for a simple example looks as follows (here
):
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/273685/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/273686/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/273687/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/273688/image.png)
clear;
tic
step=0.01;
grid=-100:step:100; lg=length(grid);
x=repmat(grid',1,lg);
y=repmat(grid,lg,1);
tempz=[x(:) y(:)];
z=tempz(:,1).*tempz(:,2);
fz=mvnpdf(tempz);
t=sum(z.*fz);
toc
This simple example already takes me 30 seconds on a computing node of our university. In the real application
is much more complex and defined by a separate function file. I'm wondering if there are any more efficient ways of doing this?
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/273689/image.png)
0 个评论
采纳的回答
Steven Lord
2020-2-26
Use the integral2 function.
5 个评论
Steven Lord
2020-2-26
fh = @(x, y) reshape(mvnpdf([x(:), y(:)]), size(x));
integral2(fh, -Inf, Inf, -Inf, Inf)
fh combines the two coordinate vectors integral2 will pass into it into the coordinate matrix mvnpdf requires, evaluates the PDF, and uses reshape to make the PDF values vector the same shape as the input.
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!