% C. Talarico % file: fourierseries.m clear all; close all; clc; syms t n; A = 3; T = 4; w0 = 2*pi/T; n = 1:20; a0 = (1/T)*int(A,t,0,T/2); an = (2/T)*int(A*cos(n*w0*t),t,0,T/2); bn = (2/T)*int(A*sin(n*w0*t),t,0,T/2); a0 = double(vpa(a0)); an = double(vpa(an)); bn = double(vpa(bn)); t=-6:1e-6:6; x = [-6 -4 -4 -2 -2 0 0 2 2 4 4 6]; y = [0 0 A A 0 0 A A 0 0 A A]; ylim ([-1 4]); f = a0; figure(1) for n = 1:1; f = f+an(n)*cos(w0*n.*t)+bn(n)*sin(w0*n.*t); hold on; plot(x,y,'-',t,f,'-r') xlabel ('t', 'fontsize',16,'fontname', 'Helvetics'); ylabel ('f(t)','fontsize',16,'fontname', 'Helvetica'); ax = gca; set(ax,'fontsize',14,'fontname', 'Monospace'); title('f(t) approximation for n = 1', 'fontsize',16,'fontname', ... 'Monospace','fontweight','Bold'); end f = a0; figure(2) for n = 1:3; f = f+an(n)*cos(w0*n.*t)+bn(n)*sin(w0*n.*t); end plot(x,y,'-',t,f,'-r'); xlabel ('t', 'fontsize',16,'fontname', 'Monospace'); ylabel ('f(t)','fontsize',16,'fontname', 'Monospace'); ax = gca; set(ax,'fontsize',14,'fontname', 'Monospace'); title('f(t) approximation for n = 3', 'fontsize',18,'fontname', ... 'Monospace'); f = a0; figure(3) for n = 1:20; f = f+an(n)*cos(w0*n.*t)+bn(n)*sin(w0*n.*t); end plot(x,y,'-',t,f,'-r') xlabel ('t', 'fontsize',16,'fontname', 'Monospace'); ylabel ('f( t )','fontsize',16,'fontname', 'Monospace'); ax = gca; set(ax,'fontsize',14,'fontname', 'Monospace'); title('f(t) approximation for n = 20', 'fontsize',16,'fontname', ... 'Monospace','fontweight','Bold');