# Créé par shemon, le 04/04/2024 en Python 3.7
from math import*
import numpy as np
def f(x):
    y=exp(x)-x**2/2+x-2
    return(y)
def g(x):
    y=x**7-3*x**4+x**3+5*x-2
    return(y)
def h(x):
    y=5*log(x+1)**3-x
    return(y)
#2 question array et annulation

L=np.zeros(10)
T=np.zeros(10)
for k in range(10):
    L[k]=k*0.05
    T[k]=f(L[k])
print(L)
print(T)
print("f(0.5)=",f(0.5))
print("g(0.3)=",g(0.3))
print("g(0.42)=",g(0.42))
Lh=np.zeros(10)
Th=np.zeros(10)
for k in range(10):
    Lh[k]=0.55+k*0.05
    Th[k]=h(Lh[k])
print(Lh)
print(Th)

#3 dichotomie itérative

def dichof(n):
    a=0.45
    b=0.50
    for k in range(n):
        c=(a+b)/2
        if f(a)*f(c)<0:
            b=c
        else:
            a=c
    return(c)

def dichog(n):
    a=0
    b=1
    for k in range(n):
        c=(a+b)/2
        if g(a)*g(c)<0:
            b=c
        else:
            a=c
    return(c)

def dichoh(n):
    a=0.65
    b=0.70
    for k in range(n):
        c=(a+b)/2
        if h(a)*h(c)<0:
            b=c
        else:
            a=c
    return(c)

#4 dichotomie until

def dicf(eps):
    a=0.45
    b=0.50
    while abs(b-a)>eps:
        c=(a+b)/2
        if f(a)*f(c)<0:
            b=c
        else:
            a=c
    return(c)
print("solution pour f à 10 chiffres:",dicf(10**(-10)))

def dicg(eps):
    a=0.3
    b=0.42
    while abs(b-a)>eps:
        c=(a+b)/2
        if g(a)*g(c)<0:
            b=c
        else:
            a=c
    return(c)
print("solution pour g à 10 chiffres:",dicg(10**(-10)))

def dich(eps):
    a=0.65
    b=0.70
    while abs(b-a)>eps:
        c=(a+b)/2
        if h(a)*h(c)<0:
            b=c
        else:
            a=c
    return(c)
print("solution non nulle pour h à 10 chiffres:",dich(10**(-10)))

#5 nombres célèbres
#racine carrée de 2
print("equation x^2-2=0 à 12 chiffres près")
def equa(x):
    y=x**2-2
    return(y)
a=1
b=2
while abs(b-a)>10**(-12):
    c=(a+b)/2
    if equa(a)*equa(c)<0:
        b=c
    else:
        a=c
print("racine 2 =",c)

print("equation exp(x)-2=0 à 12 chiffres près")
def equa(x):
    y=exp(x)-2
    return(y)
a=0
b=1
while abs(b-a)>10**(-12):
    c=(a+b)/2
    if equa(a)*equa(c)<0:
        b=c
    else:
        a=c
print("ln 2 =",c)


print("equation du nomre d'or à 12 chiffres près")
def equa(x):
    y=x-1-1/x
    return(y)
a=1
b=2
while abs(b-a)>10**(-12):
    c=(a+b)/2
    if equa(a)*equa(c)<0:
        b=c
    else:
        a=c
print("nombre or =",c)