CS101 Prelab 4

Part A: Matlab matrix operations and functions. Each blank worth 1/10 point unless marked otherwise.

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

    a) >>A .* B

                      8      -2

                      0       6  
     _____________________________________
    b) >>A .^ B   

                   16      1

                    1        9

    _____________________________________
    c) >>A * B

                 8        2
                 4        8

    _____________________________________


  d) >>B * A

              10        2

                2        6
   _____________________________________




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

    9  8  7 
    3  2  1


    _____________________________________

    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( ___:_____, ____2_______) = _____[-1  0  1  0]_____or___[-1  0  1  0] '____

     

   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 =
                     2       3       6
                     5       6       6                       

       _____________________________________               

  1.  Compute the following,

     >>  A= eye(2,3)

    >>   B = ones(2,3)

    >>   C = ones(1,6)

    >>   D = [ A , B ; C]


    D =

            1         0         0        1       1       1
            0         1         0        1       1       1
            1         1         1        1       1       1

         
    _____________________________________
  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)
 
                 2
                 4
                 6
                 8

    ____________________________________
    >> x = diff(w)
                 2
                 2
                 2

    ____________________________________

5. (8/10 point)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

6. 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 = ___[1 2 3 4 5]___ or [1:5] or linspace(1,5,5)  % a should be a vector with numbers 1 through 5

>>b = ___[2 3 4 5] ___ or [2:5] or linspace(2,5,4) % b should be a vector with numbers 2 through 5

Proceed to define the matrices:

>>C = diag(a,0);

>>D = diag(b,___1___);

>>E = diag(b,___-1___); OR -1 for D and 1 for E

>>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

7. 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 = ____[2  -1  2; 1  3  1; 3  4 -4]_________

>>c = ____[6; 10; -1]___
 
 

>>A\c =   ___  1.0000 ; 2.0000 ; 3.0000]_ but no deduction for the same values dispalyed in row format rather than column format__
        

8. Not all systems of linear equations have a unique 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?

Warning: Matrix is singular to working precision. (This is sufficient for a "correct answer".)
NaN
Inf
Inf
-----------------------------------------------------------------------------------------------------------