Lab Activity 4 - Solutions


INTRODUCTION SECTION: MATLAB MATRIX OPERATIONS

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

1. (5/9 points) 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

    6    8     6
    4    21   30

    1    8     21

_____________________________________
b) >>A .^ B

    9    16     1
    4    343   7776

    1    16     2187


but no deduction for the same values displayed in another format

_____________________________________
c) >>A * B

    9    22     35
    21  61     101

    7    22     37 _____________________________________


d) >>B * A

   28    44     44
   20    33     34

   26    44     46
_____________________________________

e) >>A¡¯



   3      4      1
   2      7      2

   1      6      3
_____________________________________

 




2. ( 5/9 points) Given the matrix B = [2 4 6;1 3 5;1 4 7;1 1 2] compute the following values:
a) >> B( 2:3, 1:2:3 )

1      5

1      7


_____________________________________

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( ___:_____, ____3_______) = _____[10; 0 ;-10;0 ] or [10 0 -10 0] ________

OR
>> B( ___1:4_ OR __[1 2 3 4]_ OR __[1; 2; 3; 4]__, ____3___) = ___ [10; 0 ;-10;0 ] or [10 0 -10 0] ________

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 =
     4     5

     1     2

     6     6

_____________________________________

3.      ( 1/9 point) Compute the following,

>> A= eye(4,4);

>> B = zeros(4,4);

>> C = [ A , B ];

>> C( end , : )

ans =


0     0     0   1     0     0     0    0

_____________________________________

4.      (2/9 points) Write the values of v, w and x after entering the following sequence of Matlab commands.

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

>> w = cumsum(v)

3     6     9    12
____________________________________
>> x = diff(w)
3     3     3
____________________________________

5. (2/9 points)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____________ , ____1_____ , _____4______ )

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. ( 8/9 points) 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,___1_____);

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

>>K = C __+__ D __+__ E;

Now solve the equation K * x = h

>> h = (1:5)' ;

>> x = ______K \ h _____( not / and not h / K )______________ ; (don't write the solution write the Matlab expression that will give you the solution)

Check your solution,

>> K * x
ans =
1
2
3
4
5

______________________

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

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

                        -0.0638              ;(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. ( 3/9 points) 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 = ____[1 2 4; 1 0 8; 2 8 3]_________

>>c = ____[1; 2; 3]___

>>A\c = ___ [-6.0000; 1.5000 ; 1.0000]_ but no deduction for the same values displayed in row format rather than column format__

8. ( 1/9 point) Not all systems of linear equations have a unique 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?

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



MAIN LABORATORY ACTIVITY: PROGRAMMING MP1'S find_xy FUNCTION

Each question worth 2/5 points except #1 and #2 which are worth 6/5 points.



Part 1: Programming the function 'find_xy' .

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

  img1   or   img2 

2. Write down the equation that you are going to solve, in the matrix form [matrix]*[column vector] = [column vector]. (Hint: see the matrix equation in the MP 1 write-up
immediately above equation_1.)
 

img3

                                                       

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

3. 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:n____________)  + 1./d(__2:n+1___________) ) ,  _0_______    ) ;

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(_2:n____________) ,  __1______    ) ;
 
  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(__2:n___________) ,  _-1_______    ) ;
 
 

6. Fill in the blank to create the vector wp from the weight vector W and HF.

wp = W./HF;
 

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

wp(1) = wp( __1____ ) - 1./d(1)*yL___ ;

wp(n) = wp( __n____ ) - __1./d(n+1)*yR_ ;  
 

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

 y = D\wp;

 

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

  y = 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 =  [yL y' yR] or [yL, y, yR] ;

  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 = [0 cumsum(d)] ;