May I ask everyone to help me solve this problem

7 views (last 30 days)
rte r
rte r on 24 Jul 2025
Edited: Torsten on 24 Jul 2025
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

Answers (1)

Torsten
Torsten on 24 Jul 2025
Edited: Torsten on 24 Jul 2025
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

Community Treasure Hunt

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

Start Hunting!