airecoeur.py

Created by schraf

Created on February 11, 2025

1.83 KB


import random
import math

def estimer_surface(nombre_points):
    # Compteur de succès
    succes = 0

    # Générer des points aléatoires et vérifier s'ils sont à l'intérieur du cœur
    for _ in range(nombre_points):
        x = -2 + 4 * random.random()
        y = -3 + 4 * random.random()

        # Partie supérieure du cœur
        if y >= 0 and ((x - 1)**2 + y**2 <= 1 or (x + 1)**2 + y**2 <= 1):
            succes += 1
        # Partie inférieure du cœur
        elif y <= 0 and ((x >= 0 and y >= 1.5 * x - 3) or (x <= 0 and y >= -1.5 * x - 3)):
            succes += 1

    # Calculer la surface estimée
    surface_estimee = (succes / nombre_points) * 8 * 8
    return surface_estimee, succes

def intervalle_confiance(surface_estimee, succes, nombre_points, z=1.96):
    # Calculer la proportion
    p = succes / nombre_points

    # Calculer l'erreur standard
    erreur_standard = math.sqrt(p * (1 - p) / nombre_points)

    # Calculer l'intervalle de confiance
    borne_inferieure = surface_estimee - z * erreur_standard * 8 * 8
    borne_superieure = surface_estimee + z * erreur_standard * 8 * 8

    return borne_inferieure, borne_superieure

# Nombre de points à générer
nombre_points = 100000

# Estimer la surface
surface_estimee, succes = estimer_surface(nombre_points)

# Calculer l'intervalle de confiance
borne_inferieure, borne_superieure = intervalle_confiance(surface_estimee, succes, nombre_points)

# Arrondir les valeurs à deux décimales
surface_estimee_arrondie = round(surface_estimee, 2)
borne_inferieure_arrondie = round(borne_inferieure, 2)
borne_superieure_arrondie = round(borne_superieure, 2)

# Afficher les résultats
print("Estimation surface : " + str(surface_estimee_arrondie))
print("Intervalle de confiance (95%)\n[" + str(borne_inferieure_arrondie) + ", " + str(borne_superieure_arrondie) + "]")

During your visit to our site, NumWorks needs to install "cookies" or use other technologies to collect data about you in order to:

With the exception of Cookies essential to the operation of the site, NumWorks leaves you the choice: you can accept Cookies for audience measurement by clicking on the "Accept and continue" button, or refuse these Cookies by clicking on the "Continue without accepting" button or by continuing your browsing. You can update your choice at any time by clicking on the link "Manage my cookies" at the bottom of the page. For more information, please consult our cookies policy.