Approximate e using random points
5 次查看(过去 30 天)
显示 更早的评论
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 个评论
James Tursa
2011-9-9
Can you post the code you have written so far? That way we have something to comment on.
回答(3 个)
Rick Rosson
2011-9-9
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
0 个评论
Rick Rosson
2011-9-10
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.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Scatter Plots 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!