May I ask everyone to help me solve this problem

mu = [0, 0]; % 均值向量(行向量)
cov_2d = [1 0.5; 0.5 1]; % 协方差矩阵
% 定义二维正态分布函数
fun = @(x, y) mvnpdf([x, y], mu, cov_2d); % 直接传递 mu 作为行向量
% 使用 integral2 计算二维积分
p = integral2(fun, -2, 3, 3, 4); % x从-2到3,y从3到4
错误使用 mvnpdf (67 )
X 和 MU 的列数必须相同。
出错 jifen>@(x,y)mvnpdf([x,y],mu,cov_2d) (6 )
fun = @(x, y) mvnpdf([x, y], mu, cov_2d); % 直接传递 mu 作为行向量
出错 integral2Calc>tensor (240 )
Z = FUN(X,Y); NFE = NFE + 1;
出错 integral2Calc>integral2t (55 )
[Qsub,esub,FIRSTFUNEVAL,NFE] = tensor(thetaL,thetaR,phiB,phiT,[],[], ...
出错 integral2Calc (9 )
[q,errbnd] = integral2t(fun,xmin,xmax,ymin,ymax,optionstruct);
出错 integral2 (105 )
Q = integral2Calc(fun,xmin,xmax,yminfun,ymaxfun,opstruct);
出错 jifen (9 )
p = integral2(fun, -2, 3, 3, 4); % x从-2到3,y从3到4

回答 (1 件)

Torsten
Torsten 2025 年 7 月 24 日
編集済み: Torsten 2025 年 7 月 24 日

0 投票

I'm not sure, but if you want to integrate the normal distribution, use "mvncdf". integral2 can be very inaccurate when integrating probability density functions (not in the case given):
format long
mu = [0, 0];
cov_2d = [1 0.5; 0.5 1];
p = mvncdf([-2,3],[3 4],mu,cov_2d)
p =
0.001241214814729
fun = @(z) mvnpdf(z, mu, cov_2d);
p = integral2(@(x,y)arrayfun(@(X,Y)fun([X,Y]),x,y),-2,3,3,4)
p =
0.001241214813916
%or - most probably faster -
p = integral2(@(x,y)reshape(fun([x(:),y(:)]),size(x)),-2,3,3,4)
p =
0.001241214813916

質問済み:

2025 年 7 月 24 日

編集済み:

2025 年 7 月 24 日

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by