Saturday, August 11, 2018

Monte Carlo Crude Integration (User Defined Function)

Monte_clarlo_integration
% _________________________________________________________________________
% Monte Carlo crude integration
% By Mahesha MG, MIT
% Date: 11/08/2018
% _________________________________INPUT___________________________________
clc;
clear;
f=input('Enter a valid MATLAB expression in x : ','s');
a=input('Enter lower limit: ');
b=input('Enter upper limit: ');
n=input('Enter sampling number: ');
x=a+(b-a)*rand(1,n);
s=sum(eval(f));
result=(b-a)*s/n
% ______________________________SAMPLE OUTPUT______________________________
% Enter a valid MATLAB expression in x : x.^2+x
% Enter lower limit: 2
% Enter upper limit: 5
% Enter sampling number: 10000
%
% result =
%
%    49.3144
% _________________________________________________________________________

No comments:

Post a Comment