================
 Matlab project 1
 ================

Remark: Please only turn in the exercise part in the boxes. 

1. Matrices
-----------

A m-by-n matrix is an array of numbers which has m rows and n columns. For example

   1  3  5  7
  -1 -2 -3 -4
   0  1 -1  0

is a 3-by-4 matrix. In Matlab, we define such a matrix by the command

  >> A = [1  3  5  7; -1 -2 -3 -4; 0  1 -1  0] 

Here >> is the matlab prompt. Type in the command and press enter, then you can see the output is

  A =
 
       1     3     5     7
      -1    -2    -3    -4
       0     1    -1     0

Now we look at a special type of matrix:
  
  0.0  0.1  0.2  0.3  0.4  0.5  0.6  0.7  0.8  0.9  1.0

It is a 1-by-11 matrix. But usually we say it is a row vector. We can define this vector by typing
 
  >> A = [0.0  0.1  0.2  0.3  0.4  0.5  0.6  0.7  0.8  0.9  1.0]

Notice this vector contains evenly spaced numbers between 0 and 1. An easy way to define it is

  >> A = 0:0.1:1

Here the first number 0 is the starting point, the second number 0.1 is the step size 
and the last number 1 is the ending point.

A third way to define the above vector is

  >> A = linspace(0, 1, 11);

Here 0 is the starting point, 1 is the ending point and 11 is the total number of points. 
All these three different methods define the same row vector.

  -------------------------------------------------------------------------------------
  | Exercise: Define the following vectors in matlab                                  |
  |                                                                                   |
  |(1)  -1.0000    -0.6000   -0.2000      0.2000      0.6000      1.0000              |
  |(2)  2000        5000        8000       11000       14000       17000       20000  |
  -------------------------------------------------------------------------------------

2. Plot
-------

We can plot the graph of given functions in matlab. For example, we want to plot the graph of

  y = sin(x)

for x between -pi and pi. First, let's check the Matlab plotting library by typing

  >> lookfor plot

It lists all Matlab commands ralated to plotting 
(Warning: the list is long. Press CTRL+C to interrupt if you do not have enough patience). 
We are going to use 3 of these commands: plot, ezplot and fplot. 
The following commands tell you how to use them:

  >> help plot
  >> help ezplot
  >> help fplot

Now, to plot the function y=sin(x) between -pi and pi, we can use

  >> x = -pi:0.1:pi;
  >> y = sin(x);
  >> plot(x,y);

  Remark: If the function is y=x^2 or y=x*sin(x), in the secondline use
          >> y=x.*x;       and      >> y=x.*sin(x);

or
  
  >> ezplot('sin(x)',[-pi,pi]);

or

  Define a M-file with the filename g.m which contains the following 2 lines:

    function y=g(x)
    y=sin(x);

  Then type
  
    >> fplot('g(x)',[-pi,pi]);

  -----------------------------------------------------------------------------------------
  | Exercise: Plot the graph of y=x(x^2-1) for x between -1 and 1 using all three methods.|
  -----------------------------------------------------------------------------------------


3. Figure, title and multiple plots
-----------------------------------

Sometimes we would like to plot two functions at the same time. 
For example, we would like to plot

  y1 = sin(x)  for x between -pi and pi
  y2 = cos(x)  for x between 0 and 2*pi

There are two different ways to do this:

(a). plot two functions in one graph:

  >> x = -pi:0.1:pi;
  >> y1 = sin(x);
  >> plot(x,y1);
  >> hold on;
  >> x = 0:0.1:2*pi;
  >> y2 = cos(x);
  >> plot(x,y2);
  >> hold off;

Here "hold on" tells Matlab that we will add graph to this window and "hold off" cancels it. 
Try the above commands without "hold on" and "hold off" to see what happens.

(b). plot two functions in two different windows:

  >> x = -pi:0.1:pi;
  >> y1 = sin(x);
  >> figure(1);
  >> plot(x,y1);
  >> x = 0:0.1:2*pi;
  >> y2 = cos(x);
  >> figure(2);
  >> plot(x,y2);

The results are two windows each contains one plot. 

Finally, we can add titles to each figure. Try

  >> figure(1);
  >> title('This is the first figure');
  >> figure(2);
  >> title('This is the second figure');

  -----------------------------------------------------------
  | Exercise: Plot the following functions in one graph     |
  |             y1 = sin(x)*x                               |
  |             y2 = 2*sin(x/2)*x                           |
  |             y3 = 4*sin(x/4)*x                           |
  |             y4 = 8*sin(x/8)*x                           |
  |           for x between 0 and 16*pi.                    |
  -----------------------------------------------------------