📄 e182.m
字号:
%-----------------------------------------------------------------------
% Example 1.8.2: Monte-Carlo Integration
%-----------------------------------------------------------------------
% Initialize
clc % clear command window
clear % clear variables
n = 3000; % number of random points
a = 0; % lower limit
b = 1; % upper limit
seed = 2000; % select random sequence
ye = 0.25; % exact value of integral
z = 0;
% Function to be integrated
graphfun (a,b,'Integrand','x','f(x)','funf182')
% Estimate average value of f(x) with random numbers
randinit (seed);
x = randu(n,1,a,b);
for i = 1 : n
z = z + funf182(x(i));
y = (b-a)*z/i;
e(i) = y - ye;
end
% Display results
show ('Number of random points',n)
show ('Final estimate of integral',y)
graphmat (e,'Error in Estimate','number of random points',...
'error')
%-----------------------------------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -