Approximate e using random points
5 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
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 Commenti
James Tursa
il 9 Set 2011
Can you post the code you have written so far? That way we have something to comment on.
Risposte (3)
Rick Rosson
il 9 Set 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
0 Commenti
Rick Rosson
il 10 Set 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.
0 Commenti
Vedere anche
Categorie
Scopri di più su Scatter Plots in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!