Lab Activity 4 - Answers

Name: __________________________   NetID: _______________   Section#: ______

Part 1 Prelab

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 A = [3 2 1;4 7 6;1 2 3] and B = [2 4 6;1 3 5;1 4 7] compute the following values:

a) >>A .* B

 


_____________________________________
b) >>A .^ B

 

_____________________________________
c) >>A * B


_____________________________________


d) >>B * A




_____________________________________


e) >>A’




_____________________________________



2.      Given the matrix B = [2 4 6;1 3 5;1 4 7;1 1 2] compute the following values in the following sequence:

a) >> B( 2:3, 1:2:3 )

____________________________________________________________

b) complete the Matlab command to replace the third column of the matrix 'B' with the values in the vector [10; 0 ; -10; 0] .

>> B( ________, ___________) = _______________________________

c) compute the following,

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

_____________________________________

 

3.      Compute the following,

>> A= eye(4,4);

>> B = zeros(4,4);

>> C = [ A , B ];

>> C( end , : )

ans =


_____________________________________

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

>> v = [ 3 , 3 , 3 , 3];

>> w = cumsum(v)

____________________________________
>> x = diff(w)

____________________________________

5.      Fill in the blanks to use the repmat function to create a row vector x of values [1 3 5 7 1 3 5 7 1 3 5 7 1 3 5 7] (length(x) is 16 , 4 sets of [1 3 5 7]).

>> x = repmat( ____1:2:7____________ , _________ , ___________ )

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 the matrix K and then solving the equation K * x = h where h = (1:5)'. In addition you will find the minimum value of the solution (x).


Fill in the blanks with the appropriate statements and operators to create the above matrix K:

>>a = 1:5;

>>b = 2:5;

Proceed to define the matrices:

>>C = diag(a,0);

>>D = diag(b,________);

>>E = diag(b,________);

>>K = C ____ D ____ E;

Now solve the equation K * x = h

>> h = (1:5)' ;

>> x = _________________________ ; (don't write the solution write the Matlab expression that will give you the solution)

Check your solution,

>> K * x
ans =

______________________

 

            >> find the minimum value of x and assign that value to a new variable minX using the min() function;

minX=                         ;( The Matlab expression that will give you the solution)

                                                  ;(the value of minX )

 

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.

2y+4z+x = 1
1x+8z = 2
3z+2x+8y = 3

>>A = ___________________________________________

>>c = ___________________________________________

>>A\c = _________________________________________

8.      Not all systems of linear equations have a solution. Try solving the system below using the backslash operator:

- 2x + 3z = 6
- 2x + y + 3z =19
-3y + 6x - 9z = 6

What is the error message that Matlab displays?

____________________________________________________________________

=============================================================

Part 2  Programming the function 'find_xy' .

The first two steps involve writing out the math behind find_xy.
1. Write down the five equations.

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

2. Write down the equation that you are going to solve, in the matrix form D*y = wp.
 
 
  matrix equation in the MP 1 write-up immediately above equation (7) in the 3. Math section.) 

          *   =  
          *   =  
          *   =  
          *   =  
          *   =  



 
  

The following answers should also be typed, in sequence, in your find_xy.m file.

3. Fill in the blanks to create the matrix  D_main. Assume that the variable n has already been created.
 

D_main  = diag( - (1./ d(______________)  + 1./d(_____________) ) ,  ________    ) ;
 
 

4. Fill in the blanks to create the matrix D_upper. Assume that the variable n has already been created.

 

D_upper  = diag(  1./d(_____________) ,  ________    ) ;
 
 

5. Fill in the blanks to create the matrix D_lower. Assume that the variable n has already been created.

 

D_lower  = diag(  1./d(_____________) ,  ________    ) ;
 

Now type (in your Matlab function find_xy.m)

    D = D_main + D_upper + D_lower ;

   

6. Fill in the blank to create the vector wp from the weight vector W and HF.
  (Note: This isn't the finished version of wp, rather we are assuming wp = Wi / HF  i = 1,...,n )

wp =  _____________________________________  ;

7. Fill in the blanks to modify the first and last elements of the vector wp.

wp(1) = wp( ______ ) - ______________ ;

wp(n) = wp( ______ ) - ______________ ;
 
 

8. Fill in the blank to solve the system of equations using the backslash (\) operator, and store this result in a vector y.
 

 y = ___________________________ ;

 

9.  Fill in the blank to change y from a column vector into a row vector.

 

y = _____________________ ;

 

10. Fill in the blank to prepend y with yL and append y with yR.  (Hint: if y = [1 2 3 4 5]  and yL = 0 , yR = 6 then 
      we need to create [ 0 1 2 3 4 5 6])

 

y =  __________________________________________ ;

 

11. Fill in the blank to create a row vector x of distances from the left tower. You should have n+2 (7 for our example) elements in this vector. Assume the variable d (with n+1 elements) has already been created.
 

x = ___________________________________________ ;

You're finished programming  find_xy  !