% Since some folk are having problems getting these files, this is a plain text
% compilation of all three. You should save each one as a separate file.
% ------------------------------------------------------------------
% FILE: a.m
function an=a(n,funcname)
% this function computes the Fourier coefficient 'an' for cos(nx) in the
% Fourier approximation to function 'funcname':
%
% S(n,x) = 1/2(a0) + (a1)cos(x) + (b1)sin(x) + (a2)cos(2x) + (a3)sin(2x)+..
%
% 'an' depends on n and the function 'funcname' to be approximated.
%
% Its value is:
% an = Integral from -pi to pi of funcname(x)*cos(nx)
%
% funcname must be given in single quotes. e.g., if you have a function
% called myfunc.m you want to approximate (which is stored in file myfunc.m),
% you can set a variable c equal to the Fourier coefficient for cos(3x) by
% typing:
% c = a(3,'myfunc');
% declare global variables
global M;
global FUNC_NAME;
global TRIG_NAME;
% set global variables that proj.m needs to be able to use.
% NOTE: since proj.m is being integrated by quad8, the function can
% only have one input parameter. That's why we have to use global
% variables for everything else.
M=n;
FUNC_NAME=funcname;
TRIG_NAME='cos';
% integrate proj(x) from -pi to pi, then divide by pi.
an=(quad8('proj',-pi,pi)./pi);
% ------------------------------------------------------------------
% FILE: b.m
function bn=b(n,funcname)
% this function computes the Fourier coefficient 'bn' for sin(nx) in the
% Fourier approximation to function 'funcname':
%
% S(n,x) = 1/2(a0) + (a1)cos(x) + (b1)sin(x) + (a2)cos(2x) + (a3)sin(2x)+..
%
% 'bn' depends on n and the function 'funcname' to be approximated.
%
% Its value is:
% bn = Integral from -pi to pi of funcname(x)*sin(nx)
%
% funcname must be given in single quotes. e.g., if you have a function
% called myfunc.m you want to approximate (which is stored in file myfunc.m),
% you can set a variable c equal to the Fourier coefficient for sin(3x) by
% typing:
% c = b(3,'myfunc');
% declare global variables
global M;
global FUNC_NAME;
global TRIG_NAME;
% set global variables that proj.m needs to be able to use.
% NOTE: since proj.m is being integrated by quad8, the function can
% only have one input parameter. That's why we have to use global
% variables for everything else.
M=n;
FUNC_NAME=funcname;
TRIG_NAME='sin';
% integrate proj(x) from -pi to pi, then divide by pi.
bn=(quad8('proj',-pi,pi)./pi);
% ------------------------------------------------------------------
% FILE: proj.m
function an=proj(x)
% this function only makes sense when called from a.m or b.m
% It computes the integrand that is then integrated to compute a Fourier
% coefficient.
%
% It returns FUNC_NAME(x) .* TRIG_NAME(M*x)
% where FUNC_NAME, TRIG_NAME, and M are global variables set in a.m or b.m
global M;
global FUNC_NAME;
global TRIG_NAME;
an=eval([FUNC_NAME,'(x) .* ',TRIG_NAME,'(M*x)']);
% ------------------------------------------------------------------