from matplotlib import pyplot as plt
from math import *

DeltarH=-92400
P=200
a=1/3
def dicho(xmin,xmax,x,p,f,a):
    m=0
    while xmax-xmin>p:
        m=(xmin+xmax)/2
        if x==f(m,a):
            return(m)
        elif (f(m,a)-x)*(f(xmin,a) - x) > 0 :
            xmin=m
        else:
            xmax=m

    return(m)


def f(x,T):
    k=log(6.53130)-(DeltarH/8.314)*((1/T)-(1/423))
    K=exp(k)
    b=P*P*K-((2*x)**2)*((1+a-2*x)**2)/((a-x)*(1-3*x)**3)
    return (b)



def R(T):
    if a<(1/3):
        R=dicho(0,a,0,0.000001,f,T)/a

    else:
        R=3*dicho(0,1/3,0,0.000001,f,T)
    return(R)



A=[300+i*0.001 for i in range(500000)]
B=[R(T) for T in A]
plt.plot(A,B)
plt.title("rendement à différente température pour un mélange en proportions stochiometriques.")
plt.xlabel("T")
plt.ylabel("Rendement")
plt.show()