-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path3-ejercicio.py
More file actions
92 lines (71 loc) · 2.65 KB
/
3-ejercicio.py
File metadata and controls
92 lines (71 loc) · 2.65 KB
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import math
diferencia = 0.00000045399 # EPSILON
valorMinimo = 0.0000004 # DELTA
PI = 3.1416 # VALOR DE PI
M = 3000 # MAXIMO DE ITERACIONES
def function(x): # FUNCION
c = 2 * x
if (c - ((math.pi / 2) * 1.0)) != math.pi:
b = math.cos(c)
else:
b = 0 # coseno vale cero en este punto
if (x - (math.pi)) != math.pi:
a = math.sin(x)
else:
a = 0 # seno vale cero en este punto
return ((0.25 * pow(x, 2)) - (x * a) - ((0.5) * b) + 0.5)
def function_der(x): # FUNCION DERIVADA
# ejercio 2
aux = x * 2
if (aux - math.pi) != math.pi:
a = math.sin(aux)
else:
a = 0 # coseno vale cero en este ppunto
if (x - ((math.pi / 2) * 1.0)) != math.pi:
b = math.cos(x)
else:
b = 0 # coseno vale cero en este ppunto
if (x - ((math.pi / 2) * 1.0)) != math.pi:
b = math.cos(x)
else:
b = 0 # coseno vale cero en este ppunto
if (x - math.pi) != math.pi:
c = math.sin(x)
else:
c = 0
return (((0.5) * x) + a - (x * b) - c)
def methodNewton():
converge = False
a = ((math.pi) / 2) * 1.0
b = 5 * (math.pi)
c = 10 * (math.pi)
t0 = [a, b, c]
for l in range(1):
v = function(t0[l])
print("METODO DE NEWTON-\n\n")
print("VALORES INICIALES:\n")
print("\t t0= ", t0[l], " v=", v, "\n")
if abs(v) >= valorMinimo: # t0 en la funcion es mayor al valor minimo
i = 0
auxt0 = t0[l]
while (i < M) and (not converge): # MIENTRAS NO SE AGOTEN EL MAXIMO DE ITERACION Y LA FUNCION NO CONVERGE
# ECUACIONES PARA CONSEGUIR EL SIGUIENTE T1
fx = function_der(auxt0)
t1 = auxt0 - ((v / fx) * 1.0)
v = function(t1)
print(" i=", i + 1, " t1=", t1, " fx=", v, " f'x=", fx, "\n")
if (abs(t1 - auxt0) <= diferencia) or (abs(v) <= valorMinimo): # CONDICION DE PARADA, CUANDO HAYA UNA RAIZ
converge = True # DETIENE EL CICLO
raiz = t1 # VARIBALE QUE REPRESENTA LA RAIZ
else:
if math.isnan(t1) or math.isnan(v): # VALIDA SI NO ES UN NUMERO, PARA QUE CICLO PARE
i = M # DETIENE EL CICLO
i += 1 # INCREMENTO DE ITERACION
auxt0 = t1 # ASIGNACION DEL NUEVO AUXT0
if converge: # SI CONVERGE MUESTRA LA RAIZ SINO MUESTRA (NO EXISTE RAIZ)
print("EXISTE RAIZ: ", raiz)
else:
print("NO EXISTE RAIZ")
converge = False # REINICIA LA VARIABLE CONVERGE
print("\n\n")
methodNewton()