CS101 Prelab 4

Name: _________________________       Netid: ______________         Section: ____


Print out this page and fill in the blanks to answer the following questions.  Hand in this assignment at the beginning of lab for the period. You should use the material found in Lecture 4 of the course notes and/or Matlab by Pratap Chapters 3.1, 3.2, 5.1.  

This week's lab will be concerned with matrices. The following exercises should help prepare you for the in-lab activities and MP1. Complete the following before coming to lab.
 

Part A: Matlab matrix operations and functions.

1. Given B = [4  2 ; 0  2 ] and A = [2  -1; 1  3] compute the following values:

    a) >>A .* B




    _____________________________________
    b) >>A .^ B
 



    _____________________________________
    c) >>A * B

 

 

    _____________________________________


  d) >>B * A




   _____________________________________

 





  1. Given the matrix A =[9 8 7; 6 5 4 ; 3 2 1; 0 -1 -2] compute the following values in the following sequence:
    a) >> A( 1:2:3, :)
 

    ____________________________________________________________

    b) complete the Matlab command to replace the second column of the matrix 'A' with the values in the vector [-1 0 1 0] .
 

    >> A( ________, ___________) = _______________________________

   c) compute the following,

    >>  A = [ 1  2   3;  4   5   6;   7   8  9];
    >>  B = [ 6 ; 6];
    >>  C = [ A(1:2,2:3) , B]
           C =
        

       _____________________________________


  1.  Compute the following,

     >>  A= eye(2,3);

    >>   B = ones(2,3)

   >>    C = ones(1,6)

    >>   D = [ A , B ; C  ]

    D =



    _____________________________________
 

 

  1.  Write the values of v, w and x after entering the following sequence of Matlab commands.

     >> v = [ 2 ;  2 ; 2 ; 2];

     >> w = cumsum(v)
 
 
 

    ____________________________________
    >> x = diff(w)
 
 

    ____________________________________

  1.  Let P be a 3 x 4 matrix. Which of the following are valid operations? Circle the correct answers.
     
          P*P         P*P'        P'*P         P'*P'         P.*P         P.*P'         P'.*P         P'.*P'

Part B: Matrix Creation

Let's say you want to create the following 5x5 matrix:
K  =
 1  2  0  0  0
 2  2  3  0  0
 0  3  3  4  0
 0  0  4  4  5
 0  0  0  5  5
  1.  Below is a sequence of Matlab commands for creating such a matrix. Fill in the blanks with the appropriate statements and operators to create the above matrix K:
>>a = _______________________________  % a should be a vector with numbers 1 through 5

>>b = _______________________________  % b should be a vector with numbers 2 through 5

Proceed to define the matrices:

>>C = diag(a,0);

>>D = diag(b,________);

>>E = diag(b,________);

>>K = C ____ D ____ E;

The matrix K you have just created is called a tri-diagonal matrix. Knowing how to use the 'diag' function will  be useful in your next lab and machine problem assignment.

Part C: Linear Systems of Equations

  1.   Transform the following set of linear equations into a Matlab matrix and vector. Matrix "A" should contain the coefficients of the equations and column vector "c" should contain the right hand side of the equations. 
      2x - y + 2z  = 6
        x + 3y + z   = 10
    -4z + 4y + 3x = -1
>>A = ___________________________________________

>>c = ___________________________________________
 
 

>>A\c = _________________________________________ 

  1. Not all systems of linear equations have a solution. Try solving the system below using the backslash operator:
                                                                                            - 2y + 3z = 6
                                                                                          x - 2y + 3z =19
                                                                                       -3x + 6y - 9z = 6

What is the error message that Matlab displays?

____________________________________________________________________