-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2-ejercicio.py
More file actions
34 lines (26 loc) · 862 Bytes
/
2-ejercicio.py
File metadata and controls
34 lines (26 loc) · 862 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import math
def newton_method(k, epsilon=1e-3, max_iter=1000):
# Definimos la función y su derivada
def f(x):
return math.atan((k+1)*x/2) - 1
def df(x):
return (k+1)/(2*(1 + ((k+1)*x/2)**2))
# Inicializamos x en 0
x = 0
# Iteramos hasta alcanzar la máxima cantidad de iteraciones
for _ in range(max_iter):
fx = f(x)
if abs(fx) < epsilon:
# Si la función es menor que epsilon, hemos encontrado una raíz
return x
# Actualizamos x usando el método de Newton
x = x - fx/df(x)
# Si llegamos a este punto, el método de Newton no convergió
return None
# Probamos el método de Newton con k = 1
k = 26781816
root = newton_method(k)
if root is None:
print("El método de Newton no convergió")
else:
print(f"La raíz encontrada es {root}")