Skip to content
Snippets Groups Projects
Commit 6774ee6f authored by iliya's avatar iliya
Browse files

feat: prepa pr l'exam

parent 03087e32
No related branches found
No related tags found
No related merge requests found
from rf_methods import rec_bisection, rec_regula_falsi, iter_bisection, \ from rf_methods import rec_bisection, rec_regula_falsi, iter_bisection, \
iter_regula_falsi iter_regula_falsi
from Dual_Numbers import Dual_Number from Dual_Numbers import Dual_Number
import numpy as np
def f(x: Dual_Number) -> float: # def f(x: Dual_Number) -> float:
return ((x * x - 1) * Dual_Number.exp(x - 1)).d # 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: def g(x: Dual_Number) -> float:
return (Dual_Number.sin(x * x) + Dual_Number.exp(x + 3) * return (Dual_Number.exp((-1) * (x * x) / 7 + x + Dual_Number.sin(1.5 * x)) * Dual_Number.log(x * x + 2) - 7).d
Dual_Number.cos(5 - x)).d
def h(x: Dual_Number) -> float:
return g(2 * x - 1)
def dfdx(x: float) -> float: # def dgdx(x: float) -> float:
return f(Dual_Number(x, 1)) # return g(Dual_Number(x, 1))
def dgdx(x: float) -> float: def dhdx(x: float) -> float:
return g(Dual_Number(x, 1)) return h(Dual_Number(x, 1))
if __name__ == "__main__": if __name__ == "__main__":
# Example for f(x) # Example for f(x)
start = -6 # start = -6
stop = -1 # stop = -1
# Example for g(x) # Example for g(x)
# start = -2 # start = -2
# stop = 0.1 # 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("----------------------------------------------")
print(f"Rec bisection = {rec_bisection(start, stop, dfdx, False)}") print(f"Iter bisection = {iter_bisection(start, stop, dhdx, True)}")
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)}")
import math # import math
import numpy as np # import numpy as np
import matplotlib.pyplot as plt # import matplotlib.pyplot as plt
def exp(x: float) -> float: # def exp(x: float, deg: int) -> float:
return pow(x, i) / math.factorial(i) # return pow(x, deg) / math.factorial(deg)
def sin(x: int) -> float: # def sin(x: float, deg: int) -> float:
return (pow(-1, i) / math.factorial(2 * i + 1)) * pow(x, 2 * i + 1) # return (pow(-1, deg) / math.factorial(2 * deg + 1)) * pow(x, 2 * deg + 1)
if __name__ == "__main__": # if __name__ == "__main__":
iter = 10 # # iter = 10
x_axis = np.arange(0, iter, 1) # deg = 5
y_axis = [] # x_axis = np.arange(-5, 10, 1)
# print(x_axis)
# y_axis = []
for i in range(iter): # for i in range(deg):
y_axis.append(exp(i)) # # 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.plot(x_axis, y_axis)
plt.show() # plt.show()
# print(f"For a = {point} and n_iter = {iter} exp({point}) = {exp_taylor(point, iter)}") # # print(f"For a = {point} and n_iter = {iter} exp({point}) = {exp_taylor(point, iter)}")
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()
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)}")
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()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment