Diferencia entre revisiones de «Series de Fourier MMA»

De MateWiki
Saltar a: navegación, buscar
 
(No se muestran 4 ediciones intermedias del mismo usuario)
Línea 414: Línea 414:
  
  
 +
=== Apéndice ===
 +
 +
[[Archivo:SumasFourierMMA.gif||]]
  
  

Revisión actual del 22:49 18 feb 2026

Trabajo realizado por estudiantes
Título Series de Fourier. Grupo MMA
Asignatura EDP
Curso 2025-26
Autores Marta Tejedor

María Romojaro

Andrea Sánchez

Este artículo ha sido escrito por estudiantes como parte de su evaluación en la asignatura


Series de Fourier MMA.jpeg


1 Serie de Fourier Función Exponencial

clear; clc; close all;

% Dominio
x = linspace(-pi, pi, 2000);

% Función original
f = exp(x);

% Número máximo de modos
Nmax = 100;

% ---- Coeficientes trigonométricos ----
a0 = (1/pi) * trapz(x, f);

an = zeros(1, Nmax);
bn = zeros(1, Nmax);

for n = 1:Nmax
    an(n) = (1/pi) * trapz(x, f .* cos(n*x));
    bn(n) = (1/pi) * trapz(x, f .* sin(n*x));
end

% Valores de N que queremos visualizar
N_values = [5 10 15 50];

figure; hold on;

% Dibujar función original
plot(x, f, 'k', 'LineWidth', 1);

% Colores automáticos
colors = lines(length(N_values));

for j = 1:length(N_values)
    N = N_values(j);

    % Suma parcial trigonométrica
    SN = (a0/2) * ones(size(x));
    for n = 1:N
        SN = SN + an(n)*cos(n*x) + bn(n)*sin(n*x);
    end

    plot(x, SN, 'Color', colors(j,:), 'LineWidth', 0.8);
end

legend('e^x', 'S_5', 'S_{10}', 'S_{15}', 'S_{50}', 'Location', 'best');
title('Serie de Fourier de e^x');
xlabel('x');
ylabel('Valor');
grid on;




clear; clc; close all;

% Intervalo
a = -pi;
b = pi;

% Mallado fino para integración
x = linspace(a,b,5000);
dx = x(2)-x(1);

% Función
f = sign(t);

% Número máximo de términos
Nmax = 100;

error_L2 = zeros(1,Nmax);

for N = 1:Nmax
    
    % Coeficientes
    a0 = (1/pi)*trapz(x,f);
    
    an = zeros(1,N);
    bn = zeros(1,N);
    
    for n = 1:N
        an(n) = (1/pi)*trapz(x, f.*cos(n*x));
        bn(n) = (1/pi)*trapz(x, f.*sin(n*x));
    end
    
    % Suma parcial
    SN = a0/2*ones(size(x));
    
    for n = 1:N
        SN = SN + an(n)*cos(n*x) + bn(n)*sin(n*x);
    end
    
    % Error L2
    error_L2(N) = sqrt(trapz(x, (f-SN).^2));
    
end

% Gráfica del error
figure;
plot(1:Nmax, error_L2,'LineWidth',2)
ylim([0 max(error_L2)])
xlabel('N'); ylabel('Error L^2');
title('Error L^2 (e^x)'); grid on

fprintf('Error L2 en N=40: %.16e\n', error_L2(100));




2 Serie de Fourier Función Signo

clear; clc; close all;

% Dominio
x = linspace(-pi, pi, 4000);

% Función discontinua: signo (escalón)
f = ones(size(x));
f(x < 0) = -1;

% Fourier: máximo modo que necesitamos
Nmax = 100;
nvals = -Nmax:Nmax;
c = zeros(size(nvals));

% Coeficientes c_n = (1/2pi) int f(x) e^{-inx} dx
for k = 1:length(nvals)
    n = nvals(k);
    integrando = f .* exp(-1i*n*x);
    c(k) = trapz(x, integrando) / (2*pi);
end

% N a visualizar
N_values = [5 10 15 50 100];

figure; hold on;

% Original
plot(x, f, 'k', 'LineWidth', 1);

% Colores automáticos
colors = lines(length(N_values));

for j = 1:length(N_values)
    N = N_values(j);
    SN = zeros(size(x));
    
    for k = 1:length(nvals)
        if abs(nvals(k)) <= N
            SN = SN + c(k)*exp(1i*nvals(k)*x);
        end
    end
    
    plot(x, real(SN), 'Color', colors(j,:), 'LineWidth', 0.8);
end

legend('sign(x)', 'S_5', 'S_{10}', 'S_{15}', 'S_{50}','S_{100}', 'Location', 'best');
title('Serie de Fourier de sign(x)');
xlabel('x'); ylabel('Valor');
grid on;




clear; clc; close all;

% Intervalo
a = -pi;
b = pi;

% Mallado fino para integración
x = linspace(a,b,5000);
dx = x(2)-x(1);

% Función signo
f = sign(x);

% Número máximo de términos
Nmax = 100;

error_L2 = zeros(1,Nmax);

for N = 1:Nmax
    
    % Coeficiente a0
    a0 = (1/pi)*trapz(x,f);
    
    an = zeros(1,N);
    bn = zeros(1,N);
    
    for n = 1:N
        an(n) = (1/pi)*trapz(x, f.*cos(n*x));
        bn(n) = (1/pi)*trapz(x, f.*sin(n*x));
    end
    
    % Suma parcial
    SN = a0/2*ones(size(x));
    
    for n = 1:N
        SN = SN + an(n)*cos(n*x) + bn(n)*sin(n*x);
    end
    
    % Error L2
    error_L2(N) = sqrt(trapz(x, (f-SN).^2));
    
end

% Gráfica del error
figure;
plot(1:Nmax, error_L2,'LineWidth',2)
xlabel('N'); ylabel('Error L^2');
title('Error L^2');
grid on

fprintf('Error L2 en N=100: %.16e\n', error_L2(100));




clear; clc; close all;

x = linspace(-pi, pi, 5000);

% Onda cuadrada
f = ones(size(x));
f(x < 0) = -1;

Nmax = 100;   % más grande para que Gibbs sea muy claro
nvals = -Nmax:Nmax;
c = zeros(size(nvals));

% Coeficientes de Fourier
for k = 1:length(nvals)
    n = nvals(k);
    integrando = f .* exp(-1i*n*x);
    c(k) = trapz(x, integrando)/(2*pi);
end

N_values = [5 10 15 50 100];

figure; hold on;

plot(x, f, 'k', 'LineWidth', 2);

colors = lines(length(N_values));

for j = 1:length(N_values)
    N = N_values(j);
    SN = zeros(size(x));
    
    for k = 1:length(nvals)
        if abs(nvals(k)) <= N
            SN = SN + c(k)*exp(1i*nvals(k)*x);
        end
    end
    
    plot(x, real(SN), 'Color', colors(j,:), 'LineWidth', 1.5);
end

legend('sign(x)', 'S_5', 'S_{10}', 'S_{15}', 'S_{50}', 'S_{100}', 'Location', 'best');
title('Fenómeno de Gibbs');
xlabel('x'); ylabel('Valor');
grid on;

xlim([-1 1])
ylim([-1.5 1.5])



3 Sumas de Cesàro y de Abel

clear; clc; close all;

% Intervalo base [-pi, pi]
a = -pi; b = pi;
x = linspace(a,b,2000);

% Parámetros de la animación
N_list = 1:2:80;            % N creciente
r_list = linspace(0.60,0.98, length(N_list));  % r creciente (mismo nº de frames)

% Funciones a animar
cases = {
    @(t) sign(t), 'sign';
    @(t) exp(t),  'exp'
};

for c = 1:size(cases,1)
    f = cases{c,1};
    name = cases{c,2};

    outdir = fullfile(pwd, ['frames_' name]);
    if ~exist(outdir, 'dir'); mkdir(outdir); end

    % Precomputo coeficientes hasta Nmax para eficiencia
    Nmax = max(N_list);
    [a0, an, bn] = fourier_coef(f, a, b, Nmax);

    for k = 1:length(N_list)
        N = N_list(k);
        r = r_list(k);

        % Aproximaciones
        SN    = fourier_partial(a0, an, bn, x, N);
        CesN  = fourier_cesaro(a0, an, bn, x, N); % Fejér
        AbelN = fourier_abel(a0, an, bn, x, N, r);

        % Plot
        figure(1); clf;
        plot(x, f(x), 'LineWidth', 2); hold on;
        plot(x, SN,    'LineWidth', 1.2);
        plot(x, CesN,  'LineWidth', 1.2);
        plot(x, AbelN, 'LineWidth', 1.2);
        grid on;

        xlabel('x'); ylabel('y');
        title(sprintf('%s: N=%d, r=%.2f', name, N, r));
        legend('f(x)', 'S_N', 'Cesàro (Fejér)', 'Abel', 'Location','best');

        % Guardar frame
        fname = fullfile(outdir, sprintf('frame_%04d.png', k));
        exportgraphics(gcf, fname, 'Resolution', 200);
    end
end

disp('Frames generados.');

%% ================== FUNCIONES ==================

function [a0, an, bn] = fourier_coef(f, a, b, N)
% Coeficientes trigonométricos en [a,b], periodo T=b-a
    T = b - a;
    xx = linspace(a,b,12000);    % mallado fino para integrar
    fx = f(xx);

    a0 = (2/T) * trapz(xx, fx);

    an = zeros(1,N);
    bn = zeros(1,N);

    for n = 1:N
        an(n) = (2/T) * trapz(xx, fx .* cos(2*pi*n*xx/T));
        bn(n) = (2/T) * trapz(xx, fx .* sin(2*pi*n*xx/T));
    end
end

function SN = fourier_partial(a0, an, bn, x, N)
% S_N(x) usando primeros N coeficientes en [-pi,pi] => cos(nx), sin(nx)
    SN = (a0/2) * ones(size(x));
    for n = 1:N
        SN = SN + an(n)*cos(n*x) + bn(n)*sin(n*x);
    end
end

function sigmaN = fourier_cesaro(a0, an, bn, x, N)
% Cesàro/Fejér: sigma_N = (1/(N+1)) sum_{k=0}^N S_k
    sigmaN = zeros(size(x));
    Sk = (a0/2) * ones(size(x));  % S_0
    sigmaN = sigmaN + Sk;

    for k = 1:N
        Sk = Sk + an(k)*cos(k*x) + bn(k)*sin(k*x);
        sigmaN = sigmaN + Sk;
    end

    sigmaN = sigmaN/(N+1);
end

function Ar = fourier_abel(a0, an, bn, x, N, r)
% Abel truncado: A_r(x) = a0/2 + sum_{n=1}^N r^n(...)
    Ar = (a0/2) * ones(size(x));
    for n = 1:N
        Ar = Ar + (r^n) * ( an(n)*cos(n*x) + bn(n)*sin(n*x) );
    end
end


4 Apéndice

SumasFourierMMA.gif