⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 chp3_19.m

📁 计算期望和方差的模拟程序
💻 M
字号:
%The Matlab code for Chp2.15
clear ; 
clc; 
close;

rand('seed',sum(100*clock));


Titles = ['n=1时  '
          'n=5时  '
          'n=25时 '
          'n=100时'];
Titlestr = cellstr(Titles);

X_n_bar=[0];        %the samples of the X_n_bar
X_n=[0];            %the samples of X_n
N=[1,5,12,100];
j=1;
num_X_n = 100;
num_X_n_bar = 100;

h_X_n_bar = figure(1);
for n = 1: num_X_n                      %题目要求n=1,5,12,100。这里取n从1到100 
    % a sample from the distribution of X_n_bar
    for i= 1 : num_X_n_bar
        X_n = rand(1,n);
        X_n_bar(i) = mean(X_n);     	%X_n 是我们模拟的对象
    end 
    
    mean_X_n_bar(n) = mean(X_n_bar);  	%均值
    var_X_n_bar(n) = var(X_n_bar);		%方差
    
     if(n==1||n==5||n==12||n==100)
        figure(h_X_n_bar)
        subplot(2,2,j);
        plot(X_n_bar);		
        grid on;
        hold on;  
    	xlabel('重复试验次数');
    	ylabel('Xn_bar');
    	axis([0 100 0 1]);
    	title(Titlestr(j));
          
        j=j+1; 
    end            
end

theory_mean_X_n_bar = [num_X_n];
theory_var_X_n_bar = [num_X_n];
for n=1:100
    theory_mean_X_n_bar(n) = 0.5;
    theory_var_X_n_bar(n) = 1/(12*n);
end

figure;
subplot(2,1,1);
plot(theory_mean_X_n_bar, '-r');

grid on;
hold on;  

plot(mean_X_n_bar,'bo-');
 
xlabel('n');
ylabel('样本均值的均值');
legend('theory value','sampled value');



subplot(2,1,2);
plot(theory_var_X_n_bar, '-r');
grid on;
hold on;
plot(var_X_n_bar,'bo-');
   
xlabel('n');
ylabel('样本均值的方差');
legend('theory value','sampled value');

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -