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!