Diferencia entre revisiones de «Series de Fourier LÁJ»
De MateWiki
(Página creada con «{{ TrabajoED | Series de Fourier. Grupo 6-A | EDP|2025-26 | Luis García Suárez, Álvaro Moreno Cisneros, Juan Pérez Guerra }...») |
|||
| Línea 3: | Línea 3: | ||
[[Archivo:Series de fourier LÁJ.jpeg||800px]]] | [[Archivo:Series de fourier LÁJ.jpeg||800px]]] | ||
| + | <source lang="python" line> | ||
| + | |||
| + | import numpy as np | ||
| + | import matplotlib.pyplot as plt | ||
| + | from scipy.integrate import trapezoid | ||
| + | |||
| + | def base_trigonometrica(n): | ||
| + | #Puntos donde se grafica | ||
| + | x = np.linspace(-1,1,1000) | ||
| + | plt.figure(figsize = (10,6)) | ||
| + | |||
| + | #Primer término de la serie | ||
| + | plt.plot(x, np.full_like(x, 0.5), label='1/2 (n=0)', linewidth=2, color='black') | ||
| + | |||
| + | #Términos trigonométricos | ||
| + | for n in range(1,n): | ||
| + | plt.plot(x, np.cos(n*np.pi*x), color = 'r', linestyle='-') | ||
| + | plt.plot(x, np.sin(n*np.pi*x), color = 'b', linestyle='-') | ||
| + | |||
| + | plt.title('Primeros términos de la base trigonométrica {1/2, cos(nπx), sin(nπx)}') | ||
| + | plt.xlabel('x') | ||
| + | plt.ylabel('f(x)') | ||
| + | plt.axhline(0, color='black', linewidth=0.5) | ||
| + | plt.axvline(0, color='black', linewidth=0.5) | ||
| + | plt.grid(True, linestyle=':', alpha=0.6) | ||
| + | plt.legend(['1/2','cos','sin'], loc = 'upper right', bbox_to_anchor = (1,1)) | ||
| + | plt.tight_layout() | ||
| + | |||
| + | # Mostrar resultado | ||
| + | plt.show() | ||
| + | |||
| + | def aproximar_funcion_cont(n_array): | ||
| + | def f(x): | ||
| + | return 1 - 2*np.abs(1/2 - x) | ||
| + | |||
| + | #Puntos donde se grafica y sus valores exactos | ||
| + | x = np.linspace(0,1,1000) | ||
| + | f_ev = f(x) | ||
| + | |||
| + | def serie_fourier(funcion,x_val,n_terms): | ||
| + | fn = np.zeros_like(x_val) | ||
| + | f_eval = funcion(x_val) | ||
| + | |||
| + | for k in range(1,n_terms+1): | ||
| + | #Integración númerica con método del trapecio | ||
| + | integrando = 2*f_eval*np.sin(k*np.pi*x_val) | ||
| + | ak = trapezoid(integrando,x_val) | ||
| + | |||
| + | #Sumar término | ||
| + | fn += ak * np.sin(k*np.pi*x_val) | ||
| + | |||
| + | return fn | ||
| + | |||
| + | #Gráfica | ||
| + | plt.figure(figsize = (10,6)) | ||
| + | plt.plot(x,f_ev,'k--', label='f(x) original', linewidth=2) | ||
| + | |||
| + | for i in n_array: | ||
| + | fn_ev = serie_fourier(f,x,i) | ||
| + | plt.plot(x,fn_ev, label=f'n = {i}') | ||
| + | |||
| + | plt.title('Aproximación de f(x) mediante Serie de Fourier (Senos)') | ||
| + | plt.legend() | ||
| + | plt.grid(True, alpha=0.3) | ||
| + | plt.show() | ||
| + | |||
| + | #Cálculo de errores | ||
| + | n_error = [] | ||
| + | for i in range(1,100): | ||
| + | n_error.append(i) | ||
| + | |||
| + | error_l2 = [] | ||
| + | error_inf = [] | ||
| + | |||
| + | for i in n_error: | ||
| + | f_n = serie_fourier(f,x,i) | ||
| + | dif = f_ev - f_n | ||
| + | |||
| + | error_l2.append(np.sqrt(trapezoid(dif**2,x))) | ||
| + | error_inf.append(round(float(np.max(np.abs(dif))),6)) | ||
| + | |||
| + | fig, ax = plt.subplots(1,2,figsize = (10,4)) | ||
| + | |||
| + | #Error L2 | ||
| + | ax[0].plot(n_error, error_l2, label='Error $L^2$') | ||
| + | ax[0].set_title('Error $L^2$') | ||
| + | |||
| + | #Error Linf | ||
| + | ax[1].plot(n_error, error_inf, label='Error Uniforme ($L^{infty}$)') | ||
| + | ax[1].set_title('Error $L^{inf}$') | ||
| + | |||
| + | |||
| + | plt.show() | ||
| + | |||
| + | def serie_fourier_definitiva(f,L,n): | ||
| + | |||
| + | #Puntos donde se grafica y sus valores exactos | ||
| + | x = np.linspace(-L/2,L/2,1000) | ||
| + | f_ev = f(x) | ||
| + | |||
| + | def serie_fourier(funcion,x_val,n_terms): | ||
| + | fn = np.zeros_like(x_val) | ||
| + | f_eval = funcion(x_val) | ||
| + | |||
| + | c0 = trapezoid(2/L * f_eval,x_val) | ||
| + | fn = c0/2 | ||
| + | for k in range(1,n_terms+1): | ||
| + | #Coeficientes | ||
| + | cn = trapezoid(2/L * f_eval * np.cos(2*k*np.pi*x_val/L),x_val) | ||
| + | dn = trapezoid(2/L * f_eval * np.sin(2*k*np.pi*x_val/L),x_val) | ||
| + | |||
| + | #Sumar términos | ||
| + | fn += cn* np.cos(2*k*np.pi*x_val/L) + dn * np.sin(2*k*np.pi*x_val/L) | ||
| + | |||
| + | return fn | ||
| + | |||
| + | #Gráfica | ||
| + | plt.figure(figsize = (10,6)) | ||
| + | plt.plot(x,f_ev,'k--', label='f(x) original', linewidth=2) | ||
| + | |||
| + | for i in n: | ||
| + | fn_ev = serie_fourier(f,x,i) | ||
| + | plt.plot(x,fn_ev, label=f'n = {i}') | ||
| + | |||
| + | plt.title('Aproximación de f(x) mediante serie de Fourier') | ||
| + | plt.legend() | ||
| + | plt.grid(True, alpha=0.3) | ||
| + | plt.show() | ||
| + | |||
| + | #Cálculo de errores | ||
| + | n_error = [] | ||
| + | for i in range(1,100): | ||
| + | n_error.append(i) | ||
| + | |||
| + | error_l2 = [] | ||
| + | error_inf = [] | ||
| + | |||
| + | for i in n_error: | ||
| + | f_n = serie_fourier(f,x,i) | ||
| + | dif = f_ev - f_n | ||
| + | |||
| + | error_l2.append(np.sqrt(trapezoid(dif**2,x))) | ||
| + | error_inf.append(round(float(np.max(np.abs(dif))),6)) | ||
| + | |||
| + | fig, ax = plt.subplots(1,2,figsize = (10,4)) | ||
| + | |||
| + | #Error L2 | ||
| + | ax[0].plot(n_error, error_l2, label='Error $L^2$') | ||
| + | ax[0].set_title('Error $L^2$') | ||
| + | |||
| + | #Error Linf | ||
| + | ax[1].plot(n_error, error_inf, label='Error Uniforme ($L^{infty}$)') | ||
| + | ax[1].set_title('Error $L^{inf}$') | ||
| + | |||
| + | |||
| + | plt.show() | ||
| + | |||
| + | |||
| + | #Lo que se ejecuta | ||
| + | # base_trigonometrica(6) | ||
| + | #aproximar_funcion_cont([1,5,10]) | ||
| + | |||
| + | def f(x): | ||
| + | lista = [] | ||
| + | for i in x: | ||
| + | if i < 0: | ||
| + | lista.append(0) | ||
| + | else: | ||
| + | lista.append(1) | ||
| + | return np.array(lista) | ||
| + | |||
| + | def MW(x): | ||
| + | n = 200 | ||
| + | a = 1/2 | ||
| + | b = 13 | ||
| + | |||
| + | suma = 0 | ||
| + | for i in range(n): | ||
| + | suma += a**i * np.cos(b**i * np.pi*x) | ||
| + | |||
| + | return suma | ||
| + | |||
| + | |||
| + | serie_fourier_definitiva(MW,20,[1,5,10,200]) | ||
| + | </source> | ||
[[Categoría:EDP]] | [[Categoría:EDP]] | ||
[[Categoría:EDP25/26]] | [[Categoría:EDP25/26]] | ||
Revisión del 18:21 16 feb 2026
| Trabajo realizado por estudiantes | |
|---|---|
| Título | Series de Fourier. Grupo 6-A |
| Asignatura | EDP |
| Curso | 2025-26 |
| Autores | Luis García Suárez, Álvaro Moreno Cisneros, Juan Pérez Guerra |
| Este artículo ha sido escrito por estudiantes como parte de su evaluación en la asignatura | |
import numpy as np
import matplotlib.pyplot as plt
from scipy.integrate import trapezoid
def base_trigonometrica(n):
#Puntos donde se grafica
x = np.linspace(-1,1,1000)
plt.figure(figsize = (10,6))
#Primer término de la serie
plt.plot(x, np.full_like(x, 0.5), label='1/2 (n=0)', linewidth=2, color='black')
#Términos trigonométricos
for n in range(1,n):
plt.plot(x, np.cos(n*np.pi*x), color = 'r', linestyle='-')
plt.plot(x, np.sin(n*np.pi*x), color = 'b', linestyle='-')
plt.title('Primeros términos de la base trigonométrica {1/2, cos(nπx), sin(nπx)}')
plt.xlabel('x')
plt.ylabel('f(x)')
plt.axhline(0, color='black', linewidth=0.5)
plt.axvline(0, color='black', linewidth=0.5)
plt.grid(True, linestyle=':', alpha=0.6)
plt.legend(['1/2','cos','sin'], loc = 'upper right', bbox_to_anchor = (1,1))
plt.tight_layout()
# Mostrar resultado
plt.show()
def aproximar_funcion_cont(n_array):
def f(x):
return 1 - 2*np.abs(1/2 - x)
#Puntos donde se grafica y sus valores exactos
x = np.linspace(0,1,1000)
f_ev = f(x)
def serie_fourier(funcion,x_val,n_terms):
fn = np.zeros_like(x_val)
f_eval = funcion(x_val)
for k in range(1,n_terms+1):
#Integración númerica con método del trapecio
integrando = 2*f_eval*np.sin(k*np.pi*x_val)
ak = trapezoid(integrando,x_val)
#Sumar término
fn += ak * np.sin(k*np.pi*x_val)
return fn
#Gráfica
plt.figure(figsize = (10,6))
plt.plot(x,f_ev,'k--', label='f(x) original', linewidth=2)
for i in n_array:
fn_ev = serie_fourier(f,x,i)
plt.plot(x,fn_ev, label=f'n = {i}')
plt.title('Aproximación de f(x) mediante Serie de Fourier (Senos)')
plt.legend()
plt.grid(True, alpha=0.3)
plt.show()
#Cálculo de errores
n_error = []
for i in range(1,100):
n_error.append(i)
error_l2 = []
error_inf = []
for i in n_error:
f_n = serie_fourier(f,x,i)
dif = f_ev - f_n
error_l2.append(np.sqrt(trapezoid(dif**2,x)))
error_inf.append(round(float(np.max(np.abs(dif))),6))
fig, ax = plt.subplots(1,2,figsize = (10,4))
#Error L2
ax[0].plot(n_error, error_l2, label='Error $L^2$')
ax[0].set_title('Error $L^2$')
#Error Linf
ax[1].plot(n_error, error_inf, label='Error Uniforme ($L^{infty}$)')
ax[1].set_title('Error $L^{inf}$')
plt.show()
def serie_fourier_definitiva(f,L,n):
#Puntos donde se grafica y sus valores exactos
x = np.linspace(-L/2,L/2,1000)
f_ev = f(x)
def serie_fourier(funcion,x_val,n_terms):
fn = np.zeros_like(x_val)
f_eval = funcion(x_val)
c0 = trapezoid(2/L * f_eval,x_val)
fn = c0/2
for k in range(1,n_terms+1):
#Coeficientes
cn = trapezoid(2/L * f_eval * np.cos(2*k*np.pi*x_val/L),x_val)
dn = trapezoid(2/L * f_eval * np.sin(2*k*np.pi*x_val/L),x_val)
#Sumar términos
fn += cn* np.cos(2*k*np.pi*x_val/L) + dn * np.sin(2*k*np.pi*x_val/L)
return fn
#Gráfica
plt.figure(figsize = (10,6))
plt.plot(x,f_ev,'k--', label='f(x) original', linewidth=2)
for i in n:
fn_ev = serie_fourier(f,x,i)
plt.plot(x,fn_ev, label=f'n = {i}')
plt.title('Aproximación de f(x) mediante serie de Fourier')
plt.legend()
plt.grid(True, alpha=0.3)
plt.show()
#Cálculo de errores
n_error = []
for i in range(1,100):
n_error.append(i)
error_l2 = []
error_inf = []
for i in n_error:
f_n = serie_fourier(f,x,i)
dif = f_ev - f_n
error_l2.append(np.sqrt(trapezoid(dif**2,x)))
error_inf.append(round(float(np.max(np.abs(dif))),6))
fig, ax = plt.subplots(1,2,figsize = (10,4))
#Error L2
ax[0].plot(n_error, error_l2, label='Error $L^2$')
ax[0].set_title('Error $L^2$')
#Error Linf
ax[1].plot(n_error, error_inf, label='Error Uniforme ($L^{infty}$)')
ax[1].set_title('Error $L^{inf}$')
plt.show()
#Lo que se ejecuta
# base_trigonometrica(6)
#aproximar_funcion_cont([1,5,10])
def f(x):
lista = []
for i in x:
if i < 0:
lista.append(0)
else:
lista.append(1)
return np.array(lista)
def MW(x):
n = 200
a = 1/2
b = 13
suma = 0
for i in range(n):
suma += a**i * np.cos(b**i * np.pi*x)
return suma
serie_fourier_definitiva(MW,20,[1,5,10,200])