GS 2000/Math - Collins
Lab 9
Building Functions from Sines and Cosines - Fourier Series

In this lab you will create some functions by combining Sine
and Cosine functions with different frequencies.  At the end
there is a function you have to try and decompose into its
Sine and Cosine components.

Basic Building Blocks

  We will do all our work in the interval [0,2p]

  Start by creating a vector of x-values in this interval:

      x = 2*pi*[0:399]/400;

  Our building block functions are: 1, cos(x), cos(2x), cos(3x),
                                       sin(x), sin(2x), sin(3x)

      c0 = cos(0*x);
      c1 = cos(1*x);
      c2 = cos(2*x);
      c3 = cos(3*x);
      s1 = sin(1*x);
      s2 = sin(2*x);
      s3 = sin(3*x);

  Plot these functions (as in the handout)

      plot(x,c1,x,c2,'--',x,c3,':')
      axis([0, 2*pi, -1 1])
      title('cos(x), cos(2x), cos(3x)')

      plot(x,s1,x,s2,'--',x,s3,':')
      axis([0, 2*pi, -1 1])
      title('sin(x), sin(2x), sin(3x)')

  To make further computations easier, let's combine all these
  functions into one block:

      all7 = [c0;c1;c2;c3;s1;s2;s3];

Building Functions

  Any function which can be built from these seven functions is
  defined by the seven coefficients you use.  For example the
  function 1 + 2cos(3x) - sin(2x) is defined by the coefficient 
  list: (1, 0, 0, 2, 0, -1, 0).  These are the coefficients of 
  1, cos(x), cos(2x), cos(3x), sin(x), sin(2x), sin(3x)
  in order.

  Using the block all7 we can easily evaluate and plot this function:

       c = [1, 0, 0, 2, 0, -1, 0];
       y = c*all7;
       plot(x,y)

  Plot the following functions:

      a. -1 + cos(x) - sin(2x)
      b. cos(2x) + 1/2*cos(3x) - sin(2x) + 1/4*sin(3x)
      c. 1 + 3/5*cos(x) - 1/5*cos(2x) + 1/7*cos(3x)
      d. -sin(x) + 2*sin(2x) - 1/10*sin(3x)

  Try some random functions:

      for i = 1:10
         c = randn(1,7); 
         y = c*all7;
         s = sprintf(' %4.2f',c);
         plot(x,y), title(['Coeffs:',s]), pause
      end

      Hit return to see the next plot.

Assignment

   Copy the following MATLAB code into a m-file and save it as
   bwave.m in the folder work.


   % script: BWAVE.M - builds a random function

   name = input('Enter your name: ','s');

   name = double(name);
   rand('state',round(sum(name)*pi))

   sh = 64 + floor(60*rand(1));
  
   name = [name,64+floor(60*rand(1,7))];

   cz = (name(1:7)-sh);
   cz = cz/norm(cz);
   cz = round(cz*10)/10;

   xz = 2*pi*[0:399]/400; 
   b7 = [ones(size(xz))
         sin(xz)
         cos(2*xz)
         sin(3*xz)
         cos(xz)
         sin(2*xz)
         cos(3*xz)];
   Y = cz*b7;
   plot(x,Y), axis([0 2*pi, min(Y)-0.1, max(Y)+0.1])

   disp('Your mystery function is stored in Y')

   clear cz b7 xz sh name

   % end of script
 

   Run this program and type in your name when prompted.
   Your assignment is to determine what set of coefficients
   were used to generate the function with values in Y

   Hints:  
      1. Once you think you know one part, subtract it from Y
         and work with the rest. For example, if you think the function
         contains 0.2 cos(x), set nY = Y - 0.2*c1; and
         plot and work with nY.
      2. Compute the average value of all the functions. (Use the 
         MATLAB function mean).
      3. Count the peaks to find the highest frequency.
      4. Look at the values at the endpoints and at p,
         and other interesting values.
      5. Once you have an idea, you can use trial and error to figure out
         the possible coefficients.  Try plotting your guess and the
         actual function on the same graph:

         c = [        ];    % your guess
         y = c*all7;
         plot(x,y,x,Y)

      6. All the coefficients are rounded to 1 decimal place
         and should be between -2 and 2.
      7. Compute the values c0*Y', c1*Y', ..., and see if they
         tell you anything.

   Mail me ccollins@math.utk.edu the name you used and your guess for the 
coefficients BEFORE YOU LEAVE TODAY.

ccollins@math.utk.edu
Last Modified: July 7, 2000