-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGraphing_Fourier.py
163 lines (131 loc) · 5.47 KB
/
Graphing_Fourier.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
from sympy import *
from sympy.abc import x,n,T
import numpy as np
import matplotlib.pyplot as mpl
def introducir_a_zero(coeficiente_zero):
while (true):
try:
funcion = coeficiente_zero
funcion=sympify(funcion)
return funcion
except:
funcion = input("Ingrese una funcion valida para A0: ")
funcion = sympify(funcion)
return funcion
def introducir_an(coeficiente_an):
while (true):
try:
funcion = coeficiente_an*cos(2*pi*n*x/T)
funcion=sympify(funcion)
return funcion
except:
funcion = input("Ingrese una funcion valida An: ")+"*cos(2*pi*n*x/T)"
funcion = sympify(funcion)
return funcion
def introducir_bn(coeficiente_bn):
while (true):
try:
funcion = coeficiente_bn*sin(2*pi*n*x/T)
funcion=sympify(funcion)
return funcion
except:
funcion = input("Ingrese una funcion valida para Bn: ")+"*sin(2*pi*n*x/T)"
funcion = sympify(funcion)
return funcion
def coeficientes_normales(coeficientes,periodo,it):
periodo=float(periodo)
a_zero = introducir_a_zero(coeficientes[0])
print("A0: " + str(a_zero))
a_n = introducir_an(coeficientes[1])
todo_an = 0
for i in range(1, it):
todo_an = todo_an + a_n.subs({n: i, x: x,T:periodo})
print(todo_an)
b_n = introducir_bn(coeficientes[2])
todo_bn = 0
for i in range(1, it):
todo_bn = todo_bn + b_n.subs({n: i, x: x,T:periodo})
print(todo_bn)
todo = a_zero + todo_an + todo_bn
print(todo)
todo = lambdify(x, todo)
x_v = np.arange(-4, 4 * np.pi, 0.1)
y_v = todo(x_v)
mpl.plot(x_v, y_v)
mpl.show()
'''
((7)*(((-1)**n)-1)/((n**2)*(pi**2)))
-(7*(-1)**(n))/(n*pi)
'''
def menu():
print("Elija una opcion:\n 1) Serie de Fourier Normal\n 2) Serie de Medio Rango de Fourier\n 3) Serie de Fourier Compleja\n 4) Salir")
def menu_2():
print("Elija una opcion:\n 1) Introducir funciones\n 2) Introducir coeficientes\n 3) Salir")
def integrar_an(func,limits,periodo):
func_integrated=lambdify(x,integrate(func*cos(2*np.pi*n*x/float(periodo)),x),["sympy","numpy"])
func_integrated=func_integrated(limits[1])-func_integrated(limits[0])
return func_integrated
def integrar_bn(func,limits,periodo):
func_integrated=lambdify(x,integrate(func*sin(2*np.pi*n*x/float(periodo)),x),["sympy","numpy"])
func_integrated=func_integrated(limits[1])-func_integrated(limits[0])
return func_integrated
def integrar_a0(func,limits):
func_integrated=lambdify(x,integrate(func,x),["sympy","numpy"])
func_integrated=func_integrated(limits[1])-func_integrated(limits[0])
return func_integrated
def conseguir_coeficientes(dict,periodo):
a_zero=0
an=0
bn=0
for func in dict:
print("heeelloooooo")
a_zero=a_zero+integrar_a0(sympify(func),dict[func])
an=an+integrar_an(sympify(func),dict[func],periodo)
bn=bn+integrar_bn(sympify(func),dict[func],periodo)
a_zero = a_zero*(1/(periodo))
an = an*(2/periodo)
bn = bn * (2 / periodo)
return [sympify(a_zero),sympify(an),sympify(bn)]
def menu_3():
print("\n 1) Continuar con otro indice de iteracion \n 2) Salir\n")
if __name__ == "__main__":
menu()
opcion=input("Escoja una opcion: ")
while(opcion!="4"):
if(opcion == "1"):
menu_2()
opcion_2=input("Elija una opcion: ")
while(opcion_2!="3"):
if(opcion_2=="1"):
num_funciones=int(input("Cuantas funciones componen la serie: "))
periodo=sympify(input("Ingresa el perido: "))
left_start=0-float(periodo)/2
funciones={}
for i in range(num_funciones):
if(i!=num_funciones-1):
funcion=input("Ingrese la funcione que empieza en " + str(left_start)+" --> ")
right_end=float(sympify(input("Ingrese el fin de la funcion " + funcion+ " --> ")))
funciones[sympify(funcion)]=[left_start,right_end]
left_start=right_end
else:
funcion = input("Ingrese la funcione que empieza en " + str(left_start) + " y termina en: "+ str(float(periodo)/2)+" --> ")
right_end=float(periodo)/2
funciones[sympify(funcion)] = [left_start, right_end]
coeficientes=conseguir_coeficientes(funciones,periodo)
it=int(input("Ingrese el indice n de iteracion: "))
coeficientes_normales(coeficientes,periodo,it)
menu_3()
opcion_3=input("Elija una opcion: ")
while(opcion_3!=2):
it = int(input("Ingrese el indice n de iteracion: "))
coeficientes_normales(coeficientes, periodo, it)
menu_3()
opcion_3 = input("Elija una opcion: ")
if (opcion_2 == "2"):
pass
'''
if (opcion == "2"):
if (opcion == "3"):
'''
menu()
opcion = input("Escoja una opcion: ")