GS 2003/Math - Collins
Lab 1
Introduction to MATLAB and Basic Polynomial Interpolation

Lab Format

The labs will usually consist of:

You can print out the labs if you want to read the
hardcopy or just want a record of the lab.

How to Read the Lab

In the labs, I'll try to use colors in a consistent way:


For this Lab, it would be good to try everything in blue or green

Starting Up and Getting Help in MATLAB 

To Start:. From Windows: Go under the Start menu and select
    MATLAB under the Programs/Matlab menu.
    You might want to resize the windows for you browser and MATLAB so you
    can easily switch between the two.

To Exit, type quit or exit or choose Exit from
    the file menu (if available)

To get Help:

    Use help command-name if you know the command name, try
          help magic

    Use helpwin or helpdesk to browse for commands

    Other commands: lookfor and intro

Saving Your Work

Use the diary command to keep a transcript of your commands and output.  Type:
	help diary   to learn about it.

Alternatively, open a text file (use MATLAB or a word processor) and then copy the
    good stuff into the text file.

Variables in MATLAB

Any string of characters can be used as a variable name (case matters).

The basic variable type in MATLAB is an NxM array of double precision
    complex numbers.  <<< Remember this!
    All the usual arithmetic and mathematic operations work on this
    type of object.  For example try this:

  a = [1 2 3], b = [4 5 6]
  a+b
  a-b
  a.*b
  a./b
  a.^b

We use .* instead of * to separate term by term multiplication
from regular matrix multiplication.  (Try a*b)

Entering data

  For values use numbers or expressions
  For lists (arrays) use [ to start, commas between, ] to end.

  For example you can enter the list of numbers
     1, 2, 3, 4, 5, 6, 7, 8, 9, 10
  as
     A = [1 2 3 4 5 6 7 8 9 10]
  or
     A(1) = 1
     A(2) = 2
     etc.
     

NOTE: MATLAB will automatically make your array big enough to
hold the elements you specify. It will set unspecified entries to 0

If you end a command with a semi-colon, then the results will not
be displayed.  Try typing:  B(4) = 1; (don't forget the semi-colon)

What do you think the result is?  Type B or disp(B) to
see the result.  Are you surprised?

If the elements are defined by a formula, you can write a little program,
for example:
    for i = 1:10
       C(i) = 2*i;
    end
    disp(C)

The colon (:) is a powerful tool in MATLAB.  Type
   1:10
   2:2:10
   1:0.1:2
   2:-0.1:1
We could use it to enter our first array as:
   A = 1:10;

My favorite array of numbers is N+1 numbers equally spaced
from 0 to 1.  In MATLAB you get this by:
  x = [0:N]/N;

Two Functions for Polynomial Interpolation

   The two commands are polyfit and polyval
   Here's an example of how to use them to construct a polynomial
      pattern generating function (PGF) for a sequence (the text following
      the % are comments)

   A = [1 3 7 15];      % four terms of the sequence
   I = [1:4];           % numbers from 1 to 4 (# terms in A)
   p = polyfit(I,A,3)  % construct polynomial of degree 3
   T = [1:8];           % numbers from 1 to 8 (for testing the polynomial)
   polyval(p,T)

   The output of the last command should give you the 4 terms from A plus 4
   additional terms.

   p contains the coefficients of the polynomial from the highest power term
   to the lowest in this case p contains the values
       0.3333   -1.0000    2.6667   -1.0000
   which corresponds to the function
       0.3333 x3 - 1.0000 x2 + 2.6667 x - 1.0000

Assignment

   1. Work through the above example.
   2. Use the same commands to work out a polynomial PGF for the following sequences:
       a.  2, 2, 4
       b.  7, 13, 19, 25
   3. Save your answers to a file, make them look good and give them to
      me or Laura before you leave today.
   4. Challenge: look at the plot command and figure out how to
      plot the original sequence and your computed sequence.

   If you have time left, look over the homework.  You might be able to
   use some of this stuff to answer the questions.

Mail: ccollins@math.utk.edu