#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Dec  5 18:11:52 2022

@author: valerielaurent
"""
from math import *
import matplotlib.pyplot as plt
import numpy as np
import random as rd

# Déclaration de la liste des variables#

L = [11.6 , 11.8 , 11.6 , 11.8 , 11.5 , 11.5 , 11.5 , 11.5 , 11.5 , 11.7 , 11.6 , 11.6 , 11.6 , 11.8 , 11.6 , 11.8 , 12.2 , 12.0 , 12.3 , 12.0 , 11.8 , 11.6 , 11.8 , 11.8 , 11.6 , 11.6 , 11.6 , 11.8 , 11.2 , 11.5 , 11.6 , 11.5 , 11.7 , 11.6 , 11.5 , 11.3 , 11.4 , 11.6 , 11.7 , 11.9 , 12.6 , 12.4 , 12.7 , 12.5 , 11.2 , 11.4 , 11.1 , 11.6]

# Fonction qui calcule la moyenne#
def moyenne(L):
    somme = 0
    for i in L:
        somme = somme +i
    moy = somme /len(L)
    return(moy)

# Fonction qui calcule l'écart-type#
def ecarttype (L):
    somme = 0
    m = moyenne(L)
    for i in L:
        somme = somme + (i-m)*(i-m)
    s = sqrt(somme/len(L))
    return(s)

# Déclaration des valeurs expérimentales utiles pour le dosage#

Ca = 0.1
UCa = 0.2/100*Ca
Vb = 10
uVb1 = 0.02/sqrt(3)
uVb2 = 0.1
t = 1.676 #Nombre de Student pour 50 valeurs et 95%#

Ve = moyenne(L)
s=ecarttype(L)
UVe = s*t/sqrt(len(L))

N = 10000 #Nombre de points de calcul#


def norm(X,UX):
    p = np.random.normal(0,1)
    sgn = rd.choice([-1,1])
    y = X + p * sgn * UX
    return(y)

def rectangle(Ux):
    p = rd.random()
    sgn = rd.choice([-1,1])
    z = p*sgn*Ux
    return(z)

Conc = []
for i in range (N) :
    Cah = norm(Ca,UCa)
    Veh = norm(Ve,UVe)
    Vbh =norm(Vb,uVb2)+rectangle(uVb1)
    Cb = Cah * Veh/Vbh
    Conc.append(Cb)

C = moyenne(Conc)
IC = ecarttype(Conc)

print(C,IC)

plt.hist(Conc,100)
plt.show()


