diff --git a/derive_and_min-max.py b/derive_and_min-max.py index b9a58a00d93b994583d1cebf1e7b594e828b4870..42cb8803d33112448fbdec31c89e0b10b54c16dd 100644 --- a/derive_and_min-max.py +++ b/derive_and_min-max.py @@ -1,39 +1,82 @@ from rf_methods import rec_bisection, rec_regula_falsi, iter_bisection, \ iter_regula_falsi from Dual_Numbers import Dual_Number +import numpy as np -def f(x: Dual_Number) -> float: - return ((x * x - 1) * Dual_Number.exp(x - 1)).d +# def f(x: Dual_Number) -> float: +# return ((x * x - 1) * Dual_Number.exp(x - 1)).d +# +# +# def g(x: Dual_Number) -> float: +# return (Dual_Number.sin(x * x) + Dual_Number.exp(x + 3) * +# Dual_Number.cos(5 - x)).d +# +# +# def h(x: Dual_Number) -> float: +# return Dual_Number.cos(x) + +# def g(x: Dual_Number) -> float: +# return (Dual_Number.sin(x * x) + Dual_Number.exp(x * 3) * +# Dual_Number.cos(x - 1)).d + + +# def dhdx(x: float) -> float: +# return h(Dual_Number(x, 5)) + + +# def dgdx(x: float) -> float: +# return g(Dual_Number(x, 1)) def g(x: Dual_Number) -> float: - return (Dual_Number.sin(x * x) + Dual_Number.exp(x + 3) * - Dual_Number.cos(5 - x)).d + return (Dual_Number.exp((-1) * (x * x) / 7 + x + Dual_Number.sin(1.5 * x)) * Dual_Number.log(x * x + 2) - 7).d + + +def h(x: Dual_Number) -> float: + return g(2 * x - 1) -def dfdx(x: float) -> float: - return f(Dual_Number(x, 1)) +# def dgdx(x: float) -> float: +# return g(Dual_Number(x, 1)) -def dgdx(x: float) -> float: - return g(Dual_Number(x, 1)) +def dhdx(x: float) -> float: + return h(Dual_Number(x, 1)) if __name__ == "__main__": # Example for f(x) - start = -6 - stop = -1 + # start = -6 + # stop = -1 # Example for g(x) # start = -2 # stop = 0.1 + # Example for g(x) + # start = -5 + # stop = 9 + + start = 1.3 + stop = 2.2 + # start = 3.5 + # stop = 5.5 + + # print(f"dhdx = {dhdx(np.pi)}") + + # print("----------------------------------------------") + # print(f"Rec bisection = {rec_bisection(start, stop, dfdx, False)}") + # print("----------------------------------------------") + # print(f"Rec regula falsi = {rec_regula_falsi(start, stop, dfdx, False)}") + # print("----------------------------------------------") + # print(f"Iter bisection = {iter_bisection(start, stop, dfdx, False)}") + # print("----------------------------------------------") + # print(f"Iter regula falsi = {iter_regula_falsi(start, stop, dfdx, False)}") + # print(f"Iter regula falsi = {iter_regula_falsi(start, stop, dgdx, False)}") + # print("----------------------------------------------") + # print(f"Iter bisection = {iter_bisection(start, stop, dgdx, False)}") + # + # print(f"Iter regula falsi = {iter_regula_falsi(start, stop, dhdx, False)}") print("----------------------------------------------") - print(f"Rec bisection = {rec_bisection(start, stop, dfdx, False)}") - print("----------------------------------------------") - print(f"Rec regula falsi = {rec_regula_falsi(start, stop, dfdx, False)}") - print("----------------------------------------------") - print(f"Iter bisection = {iter_bisection(start, stop, dfdx, False)}") - print("----------------------------------------------") - print(f"Iter regula falsi = {iter_regula_falsi(start, stop, dfdx, False)}") + print(f"Iter bisection = {iter_bisection(start, stop, dhdx, True)}") diff --git a/serie_taylor/exp.py b/serie_taylor/exp.py index a98319626b654759d626b2bfc862e465b25e4532..8b30b5f1854efbd31433e36c3145589e65d389cc 100644 --- a/serie_taylor/exp.py +++ b/serie_taylor/exp.py @@ -1,24 +1,28 @@ -import math -import numpy as np -import matplotlib.pyplot as plt +# import math +# import numpy as np +# import matplotlib.pyplot as plt -def exp(x: float) -> float: - return pow(x, i) / math.factorial(i) +# def exp(x: float, deg: int) -> float: +# return pow(x, deg) / math.factorial(deg) -def sin(x: int) -> float: - return (pow(-1, i) / math.factorial(2 * i + 1)) * pow(x, 2 * i + 1) +# def sin(x: float, deg: int) -> float: +# return (pow(-1, deg) / math.factorial(2 * deg + 1)) * pow(x, 2 * deg + 1) -if __name__ == "__main__": - iter = 10 - x_axis = np.arange(0, iter, 1) - y_axis = [] +# if __name__ == "__main__": +# # iter = 10 +# deg = 5 +# x_axis = np.arange(-5, 10, 1) +# print(x_axis) +# y_axis = [] - for i in range(iter): - y_axis.append(exp(i)) +# for i in range(deg): +# # y_axis.append(exp(-2, i)) +# y_axis.append(sin(2, i) + exp(-2, i)) +# print(y_axis) - plt.plot(x_axis, y_axis) - plt.show() - # print(f"For a = {point} and n_iter = {iter} exp({point}) = {exp_taylor(point, iter)}") +# plt.plot(x_axis, y_axis) +# plt.show() +# # print(f"For a = {point} and n_iter = {iter} exp({point}) = {exp_taylor(point, iter)}") diff --git a/serie_taylor/revision_part_b.py b/serie_taylor/revision_part_b.py new file mode 100644 index 0000000000000000000000000000000000000000..8e494d42ef7fe692bed0f75a5966e4cdad2290bb --- /dev/null +++ b/serie_taylor/revision_part_b.py @@ -0,0 +1,50 @@ +import numpy as np +import math +import matplotlib.pyplot as plt +from rf_methods import iter_bisection, iter_regula_falsi + + +def f(x: float) -> float: + return np.sin(x) + np.exp(-x) / 10 + + +def f1(x: float) -> float: + return np.cos(x) - np.exp(-x) / 10 + + +def f2(x: float) -> float: + return -np.sin(x) + np.exp(-x) / 10 + + +def f3(x: float) -> float: + return -np.cos(x) - np.exp(-x) / 10 + + +def f4(x: float) -> float: + return np.sin(x) + np.exp(-x) / 10 + + +def f5(x: float) -> float: + return np.cos(x) + np.exp(-x) / 10 + + +def T(x: float, a: float) -> float: + return (f(a) / math.factorial(0) * pow(x - a, 0)) + (f1(a) / math.factorial(1) * pow(x - a, 1)) + (f2(a) / math.factorial(2) * pow(x - a, 2)) + (f3(a) / math.factorial(3) * pow(x - a, 3)) + (f4(a) / math.factorial(4) * pow(x - a, 4)) + (f5(a) / math.factorial(5) * pow(x - a, 5)) + + +if __name__ == "__main__": + eval_point = 2 + x_axis = np.arange(-5, 9, 0.1) + + print(f"Zéros de la fonction f = {iter_bisection(4, 8, f, True)}") + + # plt.figure(figsize=(8, 6)) + # plt.plot(x_axis, f(x_axis), label='f(x) = sin(x) + exp(-x) / 10') + # plt.plot(x_axis, T(x_axis, eval_point), + # label='Polynôme de Taylor de degré 5') + # plt.legend() + # plt.xlabel('x') + # plt.ylabel('y') + # plt.title('Polynôme de Taylor de degré 5 pour f(x)') + # plt.grid(True) + # plt.show() diff --git a/serie_taylor/rf_methods.py b/serie_taylor/rf_methods.py new file mode 100644 index 0000000000000000000000000000000000000000..e2409e4131a48e98c181b4620bd20feafa6a0fa0 --- /dev/null +++ b/serie_taylor/rf_methods.py @@ -0,0 +1,118 @@ +import numpy as np +from typing import Callable + + +def f(x: float) -> float: + return x ** 4 + x ** 3 + x ** 2 - 1 + + +def g(x: float) -> float: + return x ** 2 - x - 1 + + +def h(x: float) -> float: + return x ** 2 - 25 + + +def z(x: float) -> float: + return x ** 3 - x ** 2 - 1 + + +def rec_regula_falsi(start: float, stop: float, func: Callable[[float], float], + debug: bool) -> float: + cN = (start * func(stop) - stop * func(start)) / (func(stop) - func(start)) + + if np.sign(func(cN)) != np.sign(func(start)): + stop = cN + elif np.sign(func(cN)) != np.sign(func(stop)): + start = cN + + if np.abs(func(stop) - func(start)) > 1e-9: + if debug: + print(f"x = {cN}\tf(x) = {func(cN)}") + # Testing the y-axis + if np.abs(func(cN)) < 1e-9: + return cN + + return rec_regula_falsi(start, stop, func, debug) + else: + return cN + + +def iter_regula_falsi(init_start: float, init_stop: float, func: + Callable[[float], float], debug: bool) -> float: + cN = (init_start * func(init_stop) - init_stop * + func(init_start)) / (func(init_stop) - func(init_start)) + start = init_start + stop = init_stop + + iter = 0 + + while np.abs(func(stop) - func(start)) > 1e-9 or np.abs(func(cN)) > 1e-9: + if debug: + print(f"Iter = {iter}\tx = {cN}\tf(x) = {func(cN)}") + + cN = (start * func(stop) - stop * func(start)) / \ + (func(stop) - func(start)) + + if np.sign(func(cN)) != np.sign(func(start)): + stop = cN + elif np.sign(func(cN)) != np.sign(func(stop)): + start = cN + + iter += 1 + + return cN + + +def rec_bisection(start: float, stop: float, func: Callable[[float], float], + debug: bool) -> float: + cN = (stop + start) / 2 + + if np.sign(func(cN)) != np.sign(func(start)): + stop = cN + elif np.sign(func(cN)) != np.sign(func(stop)): + start = cN + + if np.abs(stop - start) > 1e-9: + if debug: + print(f"x = {cN}\tf(x) = {func(cN)}") + + return rec_bisection(start, stop, func, debug) + else: + return cN + + +def iter_bisection(init_start: float, init_stop: float, func: + Callable[[float], float], debug: bool) -> float: + cN = (init_stop + init_start) / 2 + start = init_start + stop = init_stop + + iter = 0 + + while np.abs(stop - start) > 1e-5: + if debug: + print(f"Iter = {iter}\tx = {cN}\tf(x) = {func(cN)}") + + cN = (stop + start) / 2 + + if np.sign(func(cN)) != np.sign(func(start)): + stop = cN + elif np.sign(func(cN)) != np.sign(func(stop)): + start = cN + + iter += 1 + + return cN + + +if __name__ == "__main__": + # print(f"Bissection = {bissect_method(0.5, 2, g)}") + # print(f"Regula falsi = {regula_falsi(-3, 7, h)}") + # print(f"Bissection = {bissect_method(-3, 7, h)}") + # print(f"Regula falsi = {rec_regula_falsi(0, 3, z)}") + print(f"Iter bisection= {iter_bisection(0.5, 2, g, False)}") + print(f"Rec bisection= {rec_bisection(0.5, 2, g, False)}") + print(f"Iter regula falsi= {iter_regula_falsi(0.5, 2, g, False)}") + print(f"Rec regula falsi = {rec_regula_falsi(0.5, 2, g, False)}") diff --git a/serie_taylor/sin_plus_exp.py b/serie_taylor/sin_plus_exp.py new file mode 100644 index 0000000000000000000000000000000000000000..f14decb15f918eee7a1637d11cd2d549ef58de56 --- /dev/null +++ b/serie_taylor/sin_plus_exp.py @@ -0,0 +1,61 @@ +import numpy as np +import matplotlib.pyplot as plt + +# Définir la fonction f(x) + + +def f(x): + return np.sin(x) + np.exp(-x) + + +# Définir le point de développement a +a = 2 + +# Calcul des dérivées de f(x) jusqu'au cinquième degré + + +def f_prime(x): return np.cos(x) - np.exp(-x) +def f_double_prime(x): return -np.sin(x) + np.exp(-x) +def f_triple_prime(x): return -np.cos(x) - np.exp(-x) +def f_4th_prime(x): return np.sin(x) - np.exp(-x) +def f_5th_prime(x): return np.cos(x) + np.exp(-x) + + +# Évaluez ces dérivées en a +f_a = f(a) +f_prime_a = f_prime(a) +f_double_prime_a = f_double_prime(a) +f_triple_prime_a = f_triple_prime(a) +f_4th_prime_a = f_4th_prime(a) +f_5th_prime_a = f_5th_prime(a) + +# Calculez le polynôme de Taylor de degré 5 + + +def taylor_poly(x): + return ( + f_a + + f_prime_a * (x - a) + + (f_double_prime_a / 2) * (x - a)**2 + + (f_triple_prime_a / 6) * (x - a)**3 + + (f_4th_prime_a / 24) * (x - a)**4 + + (f_5th_prime_a / 120) * (x - a)**5 + ) + + +# Créez un tableau de valeurs x entre -5 et 9 +x = np.linspace(-5, 9, 400) + +# Calculez les valeurs correspondantes du polynôme de Taylor +taylor_values = taylor_poly(x) + +# Tracez le graphique de f(x) et du polynôme de Taylor +plt.figure(figsize=(8, 6)) +plt.plot(x, f(x), label='f(x) = sin(x) + exp(-x)') +plt.plot(x, taylor_values, label='Polynôme de Taylor de degré 5') +plt.legend() +plt.xlabel('x') +plt.ylabel('y') +plt.title('Polynôme de Taylor de degré 5 pour f(x)') +plt.grid(True) +plt.show()