Effacer les filtres
Effacer les filtres

How do I get the area of this problem?

3 vues (au cours des 30 derniers jours)
Md Jilani
Md Jilani le 15 Juil 2019
Modifié(e) : KSSV le 15 Juil 2019
I do my code for another problem using monte carlo simulation for the value of pi. This is not my homework. This is the previous trimester question of School. I didn't get it properly. Here is my code of value of PI.
prompt='Eneter number of trials:';
n=input(prompt);
hit = 0;
x1 = 0;
y1 = 0;
x0 = 0;
y0 = 0;
r=1;
for i = 1:n
x1 = rand();
y1 = rand();
if ((x1-x0)^2 + (y1-y0)^2) < r^2
hit = hit +1;
plot(x1,y1,'g.');
else
hold on;
plot(x1,y1,'r.');
end%if end
end%end for loop
disp(hit);
pi= (4*hit)/n;
disp('PiValue');
disp(pi);
cx = linspace(0,1,100);
cy = sqrt(1 - cx.^2);
hold on;
plot(cx,cy);

Réponses (1)

KSSV
KSSV le 15 Juil 2019
  2 commentaires
Md Jilani
Md Jilani le 15 Juil 2019
It measure the value of PI.... which I have done already. But I can't get this problem. By the way, Thanks a lot brother.
KSSV
KSSV le 15 Juil 2019
Modifié(e) : KSSV le 15 Juil 2019
For the given code...your area will be:
Area = hit/n ;
% Original area through formula
A = pi/4*r^2 ;

Connectez-vous pour commenter.

Catégories

En savoir plus sur Programming dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by