e182.m

来自「matlab算法集 matlab算法集」· M 代码 · 共 37 行

M
37
字号
%-----------------------------------------------------------------------
% 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 + =
减小字号Ctrl + -
显示快捷键?