Warning: Undefined variable $highlight in
/home/jeanmichel.richer/public_html/rendu_code.php on line
43
import math
import numpy as np
import matplotlib.pyplot as plt
# définition de la fonction pour NUMPY , attention parfois vous avez écrit
# (sin(x) * x + 1)/(x+4) !!
def f(x):
return np.sin(x) * x + 1/(x+4)
# définition de la fonction pour MATH
def f_math(x):
return math.sin(x) * x + 1/(x+4)
# recherche d'un zéro par dichotomie, méthode simple mais plus longue
def dichotomie(a,b):
x0 = (a+b)/2
while abs(f_math(x0)) > 1e-8:
if f_math(a) * f_math(x0) < 0:
b = x0
else:
a = x0
x0 = (a+b)/2
return x0, f_math(x0)
def main():
# construire 200 valeurs entre -5 et 5 avec numpy
x_values = np.linspace(-5, 5, 200)
# appliquer f sur x mais il faut avoir utilisé np.sin
y_values = f(x_values)
#for i in range(len(x_values)):
# print(x_values[i], y_values[i])
x0, fx0 = dichotomie(2, 4)
print(f"la fonction f s'annule en f({x0})={fx0}")
plt.plot(x_values, y_values, color='red')
plt.grid()
plt.show()
if __name__ == "__main__":
main()