代码如下
% Parameters for distributions
mu_X = 0; sigma_X = 1;
mu_Y = 5; sigma_Y = sqrt(5);
% Generate 1000 samples from X and Y
X = normrnd(mu_X, sigma_X, [1000, 1]);
Y = normrnd(mu_Y, sigma_Y, [1000, 1]);
% Generate Z = X + Y
Z = X + Y;
% Plot histograms
figure;
subplot(3,1,1);
hist(X, 'Normalization', 'pdf');
title('Histogram of X');
xlabel('X');
ylabel('Density');
subplot(3,1,2);
hist(Y, 'Normalization', 'pdf');
title('Histogram of Y');
xlabel('Y');
ylabel('Density');
subplot(3,1,3);
hist(Z, 'Normalization', 'pdf');
title('Histogram of Z');
xlabel('Z');
ylabel('Density');
相似问题