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!