



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
The matlab program and an explanation for calculating the area under the curve y = x using monte carlo integration method. It includes the program code, the theory behind the method, and the results.
Typology: Exercises
1 / 6
This page cannot be seen from the preview
Don't miss anything!




% Program name: hw2_1.m % find area under curve by MC method % Home Work Number 2, Question No. 1. n(1) = 0; area(1) = 0.45; exact =0. hit = 0; mis = 0; xs = 1; ys = 0; rand('state', 0) % this sets an initial state for i = 1: 50 hit = 0; ys =0; n(i+1) = n(i) + 500; for k = 1: n(i+1) x = rand; y = rand; ys = x; if(ys >= y ) hit = hit + 1; end end area(i+1) = hit/n(i+1); end plot(n, area, 'r:.',n, exact, 'b:.' )
This is the MATLAB program for the Q. 1 of Home work assignment number 2 for the area under the curve y = x in range of [0, 1].
Here is the result for the area under the curve for y = x in the range of x = [0, 1].
The actual value of the area is 0.
xi 0 1
Generate xi from the following and it will create g(x) = 2x. Repeat it for N times.
Then find the following sum and it will be close to the integral value:
N
i
4 1
0
5
where xi are from g(x) = 2x using uniform random Variables in [0, 1].
We can also change the N and repeat the area under the curve calculations and see when it close it the actual Value.
% Program name; fg-type_area.m % Sampling from a Function g(x) = 2x and % find area under f(x)g(x), where, % f(x) = x^4 in interval [0, 1] N(1) = 0; exact(1)=1/6; area(1)=1/6.; Max_bins = 60; pi = 3.14156; rand('state', 0) % initialize the generator to state zero axis([0 20000 0.16 0.175]) hold on for i=1: i sum = 0.0; N(i+1) = N(i) + 500; for k = 1:N(i+1)
x(k) = 0.; f = rand; x(k) = sqrt(f); sum = sum + x(k)^4; end area(i+1) = sum/(2.*N(i+1)); exact(i+1) = 1./6;
end plot(N,area,'ro',N,exact,'b-*' ) % n = hist(x,Max_bins); % bar(n, 'r')