mercredi 24 avril 2024

calcul Pi approbability quartor disk

Calcul Pi suivre technique: take a random P at coordinate (x,y) such that 0<=x<=1 and 0<= y<=1, then the point is inside the quarter disk of radius 1, otherwise the point is outside. we know tht the probability that the point is inside the quarter disk is equal pi/4. write the methos double Approx(Point[] pts) who will use the points pts to return an approximation of the number pi.



=========================


Pour approximer la valeur de Pi en utilisant la méthode décrite, vous pouvez suivre l'algorithme suivant : 1. Générer un certain nombre de points aléatoires dans le carré unitaire (0 <= x <= 1 et 0 <= y <= 1). 2. Compter le nombre de points qui tombent à l'intérieur du quart de disque de rayon 1 (c'est-à-dire ceux dont la distance par rapport à l'origine est inférieure ou égale à 1). 3. Utiliser la relation de probabilité fournie (probabilité que le point soit à l'intérieur du quart de disque est π/4) pour estimer Pi. une implémentation en C# : ```csharp using System; class Program { static Random random = new Random(); static double DistanceFromOrigin(double x, double y) { return Math.Sqrt(x * x + y * y); } static double Approx(Point[] pts) { int insideCount = 0; foreach (Point p in pts) { double distance = DistanceFromOrigin(p.X, p.Y); if (distance <= 1) { insideCount++; } } double probabilityInside = (double)insideCount / pts.Length; return probabilityInside * 4; } static void Main() { // Generate random points const int numPoints = 1000000; Point[] points = new Point[numPoints]; for (int i = 0; i < numPoints; i++) { points[i] = new Point(random.NextDouble(), random.NextDouble()); } double approximation = Approx(points); Console.WriteLine("Approximation of Pi: " + approximation); } } class Point { public double X { get; } public double Y { get; } public Point(double x, double y) { X = x; Y = y; } } ``` Dans cet exemple, la méthode `Approx` prend un tableau de points et retourne une approximation de Pi en utilisant la méthode décrite dans la question. La classe `Point` représente les coordonnées (x, y) d'un point.

Aucun commentaire:

Enregistrer un commentaire