-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAnimation.py
123 lines (91 loc) · 3.16 KB
/
Animation.py
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import math
import numpy as np
from decimal import *
import matplotlib.pyplot as plt
from matplotlib.widgets import Slider, TextBox
getcontext().prec = 25
def f(x):
return 2 * np.log(x, dtype="float64") - x / 4
def Df(x):
return 2 / x + 1 / 4
def p(coefficente):
def f_polinomio(x):
p_coef = 0
for a in range(len(coefficente)):
p_coef = p_coef + coefficente[a] * (x ** a)
return p_coef
return f_polinomio
def Dp(coefficente):
def Df_polinomio(x):
Dp_coef = 0
for a in range(len(coefficente) - 1):
Dp_coef = Dp_coef + coefficente[a + 1] * (a + 1) * (x ** a)
return Dp_coef
return Df_polinomio
def metodo_bisezione(a, b, f, errore):
n = 0
while n < math.log((b - a) / (2 * errore), 2):
c = (a + b) / 2
if f(c) == 0:
return c
break
if np.sign(f(c)) == np.sign(f(a)):
a = c
else:
b = c
return c
def metodo_tangenti(a, f, Df, errore):
n = 0
differenza = errore + 1
while differenza >= errore:
b = a
if Df(b) == 0:
a = b - (f(b) / (Df(b) + errore))
else:
a = b - (f(b) / Df(b))
differenza = abs(b - a)
n += 1
return a, n
coef = [-1, 0, 0, 1]
a_min = 1 # the minimial value of the paramater a
a_max = 200 # the maximal value of the paramater a
res = 200
centrox, centroy = 0, 0
ampiezza_intervallox, ampiezza_intervalloy = 8, 8
m, n, l, k = centrox - ampiezza_intervallox/2, centrox + ampiezza_intervallox/2, centroy - ampiezza_intervalloy/2, centroy + ampiezza_intervalloy/2
init_zoom = 1
x = np.linspace(m, n, res)
y = np.linspace(l, k, res)
z = [[None for i in range(len(y))] for j in range(len(x))]
for i in range(len(y)):
for j in range(len(x)):
z[i][j], a = metodo_tangenti(complex(x[j], y[i]), p(coef), Dp(coef), 0.000001)
z[i][j] = z[i][j].imag * z[i][j].real
fig = plt.figure()
conv_ax = plt.axes([0.1, 0.2, 0.8, 0.65])
slider_ax = plt.axes([0.1, 0.05, 0.8, 0.05])
plt.axes(conv_ax)
plt.title('y = sin(ax)')
conv_plot = plt.pcolormesh(x, y, z)
plt.xlim(m, n)
plt.ylim(l, k)
a_slider = Slider(slider_ax, 'a', a_min, a_max, valinit=init_zoom)
def update(a):
m, n, l, k = centrox - ampiezza_intervallox / (2*(1.05)**a), centrox + ampiezza_intervallox / (2*(1.05)**a), centroy - ampiezza_intervalloy / (2*(1.05)**a), centroy + ampiezza_intervalloy / (2*(1.05)**a)
plt.title((1.05)**a)
x = np.linspace(m, n, res)
y = np.linspace(l, k, res)
z = [[None for i in range(len(y))] for j in range(len(x))]
for i in range(len(y)):
for j in range(len(x)):
z[i][j], a = metodo_tangenti(complex(x[j], y[i]), p(coef), Dp(coef), 0.000001)
z[i][j] = z[i][j].imag * z[i][j].real
plt.xlim(m, n)
plt.ylim(l, k)
conv_plot = plt.pcolormesh(x, y, z)
fig.canvas.draw_idle()
a_slider.on_changed(update)
def buildmebarchart(a=int):
import matplotlib.animation as ani
animator = ani.FuncAnimation(fig, buildmebarchart, interval = 100)
plt.show()