% programita para la serie de senos y cosenos de f(x) = x % -L \le x \le L close all L = 1; letra=18; prefix='heaviside'; efe = inline('(x>0)'); a0 = 1/2; margen = 0.1; for N = [1 3 5 15] n=[1:N]; a = zeros(1,N); b = 1/pi./n .* (1-(-1).^n); x = [-L:0.01:L]; npix = pi*(n'*x)/L; fap = a0 + a * cos(npix) + b * sin(npix); f = efe(x); M = max([f fap]); m = min([f fap]); plot([-3*L 3*L],[0 0],'b',[0 0],[m M],'b') % ejes hold on plot(x,f,'r','LineWidth',2) plot(x,fap,'m','LineWidth',2) %title(sprintf('N = %d error = %.8f',N,max(abs(f-fap)))) axis([-L-margen L+margen m-margen M+margen]) set(gca,'FontSize', letra) filename=sprintf('fourier-%s-N=%02d',prefix,N); print('-dpng',filename) print('-depsc2',filename) hold off pause end pause ff = f; xx = x; xx1 = xx(1:100); xx2 = xx(102:201); ff1 = zeros(size(xx1)); ff2 = ones(size(xx2)); plot([-3*L 3*L],[0 0],'b',[0 0],[m M],'b') % ejes hold on plot(xx1,ff1,'m','LineWidth',2) plot(xx2,ff2,'m','LineWidth',2) plot([-L 0 L], [1/2 1/2 1/2], 'mo', 'MarkerEdgeColor','m','MarkerFaceColor','m', ... 'MarkerSize', 10) plot([-L L],[0 0],'b', [0 0],[m M],'b'); axis([-L-margen L+margen m-margen M+margen]) set(gca,'FontSize', letra) filename=sprintf('fourier-%s-N=inf',prefix); print('-dpng',filename) print('-depsc2',filename) hold off pause for N = [15] n=[1:N]; a = zeros(1,N); b = 1/pi./n .* (1-(-1).^n); x = [-3*L:0.01:3*L]; npix = pi*(n'*x)/L; fap = a0 + a * cos(npix) + b * sin(npix); f = efe(x); M = max([f fap]); m = min([f fap]); plot([-3*L 3*L],[0 0],'b',[0 0],[m M],'b') % ejes hold on plot(x,f,'r','LineWidth',2) plot(x,fap,'m','LineWidth',2) axis([-3*L-margen 3*L+margen m-margen M+margen]) set(gca,'FontSize', letra) filename=sprintf('fourier-%s-N=%02d-3L',prefix,N); print('-dpng',filename) print('-depsc2',filename) hold off pause end pause plot([-3*L 3*L],[0 0],'b',[0 0],[m M],'b') % ejes hold on plot(xx1-2*L,ff1,'m','LineWidth',2) plot(xx2-2*L,ff2,'m','LineWidth',2) plot(xx1,ff1,'m','LineWidth',2) plot(xx2,ff2,'m','LineWidth',2) plot(xx1+2*L,ff1,'m','LineWidth',2) plot(xx2+2*L,ff2,'m','LineWidth',2) plot([-3*L -2*L -L 0 L 2*L 3*L], ones(1,7)/2, 'mo', 'MarkerEdgeColor','m','MarkerFaceColor','m', ... 'MarkerSize', 10) axis([-3*L-margen 3*L+margen m-margen M+margen]) set(gca,'FontSize', letra) filename=sprintf('fourier-%s-N=inf-3L',prefix); print('-dpng',filename) print('-depsc2',filename)