% Script 2 for Week 6
Here is an example of my Fourier Coefficient function, fseries2.m, in action.
It is designed to work with a function on the interval [-pi,pi] and will create a periodic function that will approximate your function on that interval.
If your function is not on that interval you must use a linear change of variables to "shift-and-stretch" the domain to match this. You can then squish-and-shift to return to the original domain when you are done.
A Fourier series is sequence of orthogonal projections onto larger and larger subspaces of the continuous functions on an interval. Each subspace is spanned, for some n, by the 2*n-1 linearly independent functions (delete the zero function) sin(k*x) and cos(k*x) for k=0,...,n.
These functions are orthogonal to each other with integral inner product, just like i,j and k in R^3. There is the equivalent of the Pythagorean theorem for this function space. As n gets bigger and bigger the part of a function f left over after you take away the projection goes to 0 in the integral norm. At a certain point the function f "is" the finite series in the sense that the remainder is negligibly small. "Negligible," of course, depends on the situation. Small here means the integral of the square of the remainder is small.
If your function G is on an interval [r,s] create a new shifted-and-domain-scaled version of the function f given by f = @(t) G( t*(s-r)/(2*pi) + (s+r)/2 ) and approximate THAT on the interval [-pi,pi].
If you MUST you can switch back and replace t in each sin(k*t) or cos(k*t) term by the expression 2*pi*( t-(s+r)/2 )/(s-r). This new function will be a linear combination of sines and cosines whose periods match the interval [r,s].
%*******************************************
function z=q(u)
z=zeros(size(u));
region1 = u<-pi; % First interval
z(region1) = 0;
region2 = (-pi<=u) & (u<=0); % Second interval
z(region2) = 1;
region3 = (0