Clear Filters
Clear Filters

Approximate e using random points

5 views (last 30 days)
Austin
Austin on 9 Sep 2011
I need to use some sort of loop to approximate e using random points. I have plotted a line y=1/x, and generated 1000 random points on the plot. I am trying to make a loop that determines whether each point is above or below the line. I want to use the results from the loop to calculate the area under the curve by figuring out the percentage of points below the line (I already know how to do this). I also want to make a plot of the approximated value of e vs. the number of iterations. Any help would be much appreciated. Thanks (Initial question: http://www.mathworks.com/matlabcentral/answers/15459-approximation-of-e-using-random-points)
  2 Comments
James Tursa
James Tursa on 9 Sep 2011
Can you post the code you have written so far? That way we have something to comment on.
Austin
Austin on 9 Sep 2011
Sure! So far my code is:
% Initialization
clear all, close all, clc
%% Step 1
% Start with a unit square whose lower left is (1,0) and upper right is
% (2,1)
hold on;
axis([1 2 0 1]); % axis([xmin xmax ymin ymax])
%% Step 2
% Generate many random points in this range
% r = a + (b-a).*rand(1000,1);
x = 1 + (2-1).*rand(1000,1);
y = 0 + (1-0).*rand(1000,1);
% scatter
x0 = (y < 1./x); % Set up an equation that determines whether the point is above the line or not
scatter(x(x0),y(x0),5,'blue','filled'); % If the point is below, blue
scatter(x(~x0),y(~x0),5,[1,.5,0],'filled'); % If the point is ablove, orange
%% Step 3
% Cut the box with the curve y=1/x
x1 = 1:.001:2;
y1 = 1./x1;
plot(x1,y1,'LineWidth',5,'Color','g')
%% Step 4
% Determine the percentage of these points that lie below the y=1/x curve
points_below = sum(x0); % Number of points below the line y = 1/x
% total number of points = 1000
percent_below = points_below/1000;
%% Step 5
% Since the box is a unit area, the percentage is an estimate of the area
% below the curve.
area = percent_below;
%% Step 6
% The area (A) would be (ideally) equal to ln(2).
% Using A=ln(2), then e^A = 2 gives e = 2^(1/A)
e_approx = 2.^(1./area)
This code gives me an answer of e. But I also am trying to make another plot that requires a loop to solve the section labelled %scatter.

Sign in to comment.

Answers (3)

Rick Rosson
Rick Rosson on 9 Sep 2011
Please try something like the following:
NumIter = 1000*[1;5;10;20;30;50];
NumTrials = size(NumIter,1);
e_approx = zeros(NumTrials,1);
for k = 1:NumTrials
x = 1 + (2-1).*rand(NumIter(k),1);
y = 0 + (1-0).*rand(NumIter(k),1);
...
...
...
e_approx(k) = 2.^(1./area);
end
figure;
plot(NumIter,e_approx);
HTH.
Rick

Rick Rosson
Rick Rosson on 10 Sep 2011
You may be able to eliminate the for loop by vectorizing the code I posted in my previous answer. The approach would be to make x and y into matrices instead of column vectors. The size of these matrices would be NumIter x NumTrials. The outcome variable e_approx would still be a column vector of size NumTrials x 1.

Austin
Austin on 12 Sep 2011
Could you please explain the code? And then could you please explain how I would turn x and y into matrices? Because the problems I've been running into while trying to use a loop are saying that matrix dimensions must agree.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!