% % EIGENF2.M Demo of Fourier Series Representation for f(x) = x(pi-x) % % The goal here is to evaluate the infinite series expansion for % f(x) = x(pi-x) in terms of sinusoids (i.e. a Fourier series expansion) % % For normalized eigenfunctions, the coefficients and basis functions % are: % an = 4*sqrt(2/pi)/m^3 gn(x) = sqrt(2/pi)*sin(mx) % with m = (2n-1) and n = 1, 2, ... % % For unnormalized eigenfunctions, the coefficients and basis functions % are : % an = (8/pi)/m^3 gn(x) = sin(mx) % with m = (2n-1) and n = 1, 2, ... % % File prepared by J. R. White, UMass-Lowell (original Nov. 1997) % --> minor modifications (Aug. 1998) % % % getting started clear all, close all % % set x domain Nx = 201; x = linspace(0,pi,Nx); c = 8/pi; % % calc exact function fe = x.*(pi-x); % % calc Fourier Series approx fa = zeros(size(x)); Max=100; tol = 0.001; mrerr = 1.0; n = 0; while mrerr > tol & n < Max n = n+1; m = 2*n-1; ff = (c/m^3)*sin(m*x); fa = fa + ff; rerr = ff(2:Nx-1)./fa(2:Nx-1); mrerr = max(abs(rerr)); end % % plot curves plot(x,fe,'g-',x,fa,'r--'),grid r = axis; r(2) = pi; axis(r) title('Accuracy of Truncated Fourier Series for f(x) = x(pi-x)') ylabel('f(x)'), xlabel('x value') legend('Exact f(x)','Approx f(x)') gtext([num2str(n),' terms for convergence']) % % end of demo %