用matlab利用二项分布验证中心极限定理
时间: 2023-07-20 15:11:14 浏览: 170
在 MATLAB 中,我们可以使用 `binornd` 函数生成二项分布样本,使用 `mean` 和 `std` 函数计算样本的均值和标准差,然后使用 `normpdf` 和 `normcdf` 函数构造正态分布,并绘制二项分布和正态分布的概率密度函数和累积分布函数的图像。
下面是利用 MATLAB 进行模拟的示例:
``` matlab
% 设置参数
n = 100; % 试验次数
p = 0.5; % 成功概率
% 生成二项分布样本
sample = binornd(n, p, [1, 10000]);
% 计算样本的均值和标准差
mean_val = mean(sample);
std_val = std(sample);
% 构造正态分布
normal_dist = makedist('Normal', 'mu', mean_val, 'sigma', std_val);
% 绘制概率密度函数和累积分布函数
x = 0:1:n;
binom_dist = binopdf(x, n, p);
norm_dist = pdf(normal_dist, x);
figure;
plot(x, binom_dist, 'b', x, norm_dist, 'r');
legend('Binomial', 'Normal');
title('Probability Density Function');
xlabel('X');
ylabel('Probability');
grid on;
binom_cdf = binocdf(x, n, p);
norm_cdf = cdf(normal_dist, x);
figure;
plot(x, binom_cdf, 'b', x, norm_cdf, 'r');
legend('Binomial', 'Normal');
title('Cumulative Distribution Function');
xlabel('X');
ylabel('Probability');
grid on;
```
运行上述代码,可以得到二项分布和正态分布的概率密度函数和累积分布函数的图像。可以看出,当n足够大时,二项分布和正态分布的近似程度很高,验证了中心极限定理的有效性。
阅读全文
相关推荐















