example_monte_carlo_simulationin_matlab.zip.m
来自「用Matlab实现蒙特卡罗模拟的源程序,非常的好用」· M 代码 · 共 27 行
M
27 行
% Example Monte Carlo Simulation in Matlab
% Function: y = x2^2/x1
%
% Generate n samples from a normal distribution
% r = ( randn(n,1) * sd ) + mu
% mu : mean
% sd : standard deviation
%
% Generate n samples from a uniform distribution
% r = a + rand(n,1) * (b-a)
% a : minimum
% b : maximum
n = 100000; % The number of function evaluations
% --- Generate vectors of random inputs
% x1 ~ Normal distribution N(mean=100,sd=5)
% x2 ~ Uniform distribution U(a=5,b=15)
x1 = ( randn(n,1) * 5 ) + 100;
x2 = 5 + rand(n,1) * ( 15 - 5 );
% --- Run the simulation
% Note the use of element-wise multiplication
y = x2.^2 ./ x1;
% --- Create a histogram of the results (50 bins)
hist(y,50);
% --- Calculate summary statistics
y_mean = mean(y)
y_std = std(y)
y_median = median(y)
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?