import matplotlib.pyplot as plt
import numpy as np

print("TP 2 - Jeudi 30 Septembre 2021")

print(3*" ")
print("Module matplotlib")

print(3*" ")
print(" - Exercice")

print(3*" ")
print(" - Question 1:")

debut = -10
fin = 10
pas = 3
x = np.arange(debut,fin,pas)
f = 1 + np.exp(x)
print(x)
print(f)

plt.plot(x,f,"k") # "k" trace une courbe noire
plt.xlabel("x") # Légende sur l'axe des abscisses
plt.ylabel("f(x) = 1 + exp(x)") # Légende sur l'axe des ordonnées
plt.title("Question 1: Fonction exponentielle translatée de 1")
plt.show()

print(3*" ")
print(" - Exercice")

print(3*" ")
print(" - Question 2:")

print("Solution: Réduire le pas de discrétisation")
debut = -10
fin = 10
pas = 0.5
x = np.arange(debut,fin,pas)
f = 1 + np.exp(x)
print(x)
print(f)

plt.plot(x,f,"k") # "k" trace une courbe noire
plt.xlabel("x") # Légende sur l'axe des abscisses
plt.ylabel("f(x) = 1 + exp(x)") # Légende sur l'axe des ordonnées
plt.title("Question 2: Fonction exponentielle translatée de 1 avec pas réduit")
plt.show()


print(3*" ")
print(" - Exercices")

print(3*" ")
print(" - Question 3:")

x = np.arange(0.1,10,0.1)
f = 3*np.log(x)-1
plt.plot(x,f,"green")
plt.xlabel("x")
plt.ylabel("f(x) = 3log(x)-1")
plt.title("Question 3: Fonction logarithme translatée de -1")
plt.show()

print(3*" ")
print(" - Question 4:")

x = np.arange(0.1,10,0.1)
f = 3*np.sqrt(x)-1
plt.plot(x,f,"red")
plt.xlabel("x")
plt.ylabel("f(x) = 3sqrt(x)-1")
plt.title("Question 4: Fonction racine carrée translatée de -1")
plt.show()

print(3*" ")
print(" - Question 5:")

print("En théorie, la fonction racine carrée croit le plus vite en + infini")
print("En théorie, la fonction logarithme décroit le plus vite en 0")

x = np.arange(0.1,10,0.1)
f = 3*np.log(x)-1
g = 3*np.sqrt(x)-1
plt.plot(x,f,"green",label="Fonction avec logarithme") # On tape les deux lignes plt.plot à suivre pour 
plt.plot(x,g,"red",label="fonction avec racine carée")   # superposer deux courbes
plt.xlabel("x")
plt.ylabel("f(x) = 3log(x)-1 et f(x) = 3sqrt(x)-1")
plt.legend()
plt.title("Question 5: Fonctions logarithme et racine carrée translatées de -1 superposées")
plt.show()

print(3*" ")
print(" - Question 6:")

x = np.arange(0.1,10,0.1)
plt.plot(x,np.exp(np.log(x)))
plt.title("Question 6: Fonctions réciproques")
plt.show()
print("exp et log sont deux fonctions réciproques, donc: log(exp(x)) = exp(log(x)) = x pour tout x > 0")



print(3*" ")
print(" - Exercice")

print(3*" ")
print(" - Question 7:")

t = np.arange(0.01,20,0.01)
f = np.exp(-t/5.0)

plt.plot(t,f)
plt.xlabel("t")
plt.ylabel("exp(-t/5.0)")
plt.title("Question 7: Fonction f(t) = exp(-t/5.0) en échelle standard")
plt.show()

plt.semilogy(t,f)
plt.xlabel("t")
plt.ylabel("exp(-t/5.0)")
plt.title("Question 7: Fonction f(t) = exp(-t/5.0) en échelle logarithmique selon l'axe y")
plt.show()

print(3*" ")
print(" - Exercice")

print(3*" ")
print(" - Questions 8 et 9:")

temps = [0, 3, 6, 9, 12]
abondance = [10, 102, 1221, 2435, 5184]

plt.subplot(1,2,1)
plt.plot(temps,abondance,"g+")
plt.xlabel("Temps (jours)")
plt.ylabel("Abondance (ind/mL)")
plt.grid()
plt.title("Q 8: Echelle standard")

plt.subplot(1,2,2)
plt.semilogy(temps,abondance,"g+")
plt.xlabel("Temps (jours)")
plt.ylabel("Abondance (ind/mL)")
plt.grid()
plt.title("Q 9: Echelle semi-logarithmique")
plt.show()

print(3*" ")
print(" - Question 10:")

print("Croissance exponentielle entre t = 0 et t = 6 puis croissance exponentielle moins importante entre t = 6 et t = 12")


print(3*" ")
print("Pour les plus rapides (en option)")

print(3*" ")
print(" - Exercice")

print(3*" ")
print(" - Question 11-a:")

print("f'(x) = x/sqrt(x**2 + 1)")

h = 0.0001
x = np.arange(-10,10,0.5)
df = x/np.sqrt(x**2+1)
dftilde = (np.sqrt((x+h)**2+1)-np.sqrt(x**2+1))/h
plt.plot(x,df,"green") 
plt.plot(x,dftilde,"red")
plt.xlabel("x")
plt.ylabel("f'(x) et (f(x+h)-f(x))/h")
plt.title("Question 11-a: Dérivation de f(x) = sqrt(x**2+1)")
plt.show()


print(3*" ")
print(" - Question 11-b:")

print("f'(x) = (1-x)*exp(-x)")

h = 0.0001
x = np.arange(-10,10,0.5)
df = (1-x)*np.exp(-x)
dftilde = ((x+h)*np.exp(-(x+h))-x*np.exp(-x))/h
plt.plot(x,df,"green") 
plt.plot(x,dftilde,"red")
plt.xlabel("x")
plt.ylabel("f'(x) et (f(x+h)-f(x))/h")
plt.title("Question 11-b: Dérivation de f(x) = x*exp(-x)")
plt.show()


print(3*" ")
print(" - Question 12-a:")

print("Equation de la tangente: y = 7*x-5")
x = np.arange(-3,3,0.1)
f = x**4 + 2*x**2 - x
tangentef = 7*x - 5
plt.plot(x,f,"green") # Tracé de la courbe
plt.plot(x,tangentef,"red") # Tracé de la tangente
plt.xlabel("x")
plt.ylabel("f(x) et tangente en 1")
plt.title("Question 12-a: f(x) = x^4 + 2x^2 - x et tangente en 1")
plt.show()


print(3*" ")
print(" - Question 12-b:")

print("Equation de la tangente: y = 0*x+1")
x = np.arange(-3,2,0.1)
f = np.exp(x) - np.sin(x)
tangentef = 0*x + 1 # Attention à bien mettre le 0*x sans quoi tangentef ne serait que le nombre 1 et on ne pourrait pas la tracer !
plt.plot(x,f,"green") # Tracé de la courbe
plt.plot(x,tangentef,"red") # Tracé de la tangente
plt.xlabel("x")
plt.ylabel("f(x) et tangente en 1")
plt.title("Question 12-b: f(x) = exp(x)-sin(x) et tangente en 0")
plt.show()