Anda di halaman 1dari 5

Lab Activity 4 - Answers

Name: __________________________ NetID: _______________ Section#: ______

INTRODUCTION SECTION: MATLAB MATRIX OPERATIONS


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; -7 9] and B = [2 -2 ; 3 -3 ] compute the following values:
a) >>A .* B
6 -4
-21 -27
_____________________________________
b) >>A .^ B
9.0000 0.2500
-343.0000 0.0014
_____________________________________
c) >>A * B
12 -12
13 -13
_____________________________________
d) >>B * A
20 -14
30 -21
_____________________________________
e) >>A
3 -7
29
_____________________________________
2. Given the matrix B =[1 2 3; 4 5 6 ; 7 8 9; -1 -2 -3] compute the following values in the
following sequence:
a) >> B( 2:3, 1:2:3 )
46

7 9________________________________________________________
b) complete the Matlab command to replace the third column of the matrix 'B' with the values in
the vector [1; 0 ; -1; 0] .
>> B( _____:___, __3_________) =
121
450
7 8 -1
-1 -2 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=
45
12
66
3. 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. Write the values of v, w and x after entering the following sequence of Matlab commands.
>> v = [ 4 , 4 , 4 , 4];
>> w = cumsum(v)
____4 8 12 16________________________________
>> x = diff(w)
_______4 4 4_____________________________
5. Fill in the blanks to use the repmat function to create a row vector x of values [2 4 6 8 2 4 6
8 ...] (length(x) is 12, 4 sets of [2 4 6 8]).
>> x = repmat( ____2:2:8____________ , ___1______ , _____4______ ) '

Part B: Matrix Creation


Let's say you want to create the following 5x5 matrix:
K=
1 2000
2 2300
0 3340
0 0445

0 0055
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,__1______);
>>E = diag(b,____-1____);
>>K = C _+___ D _+___ E;
Now solve the equation K * x = h
>> h = (1:5)' ;
>> x = ____K\h_____________________ ; (don't write the solution write the Matlab
expression that will give you the solution)
Check your solution,
>> K * x
ans =
___1.1277
-0.0638
-0.0426
0.8298
0.1702_________________________________________
>> 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)
1 ;(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.
4y+9z+x = 1
2x + 4z = 2
3z+1x+8y = 3
>>A = _____[1 4 9;2 0 4;1 8 3]______________________________________
>>c = ____[1;2;3]_______________________________________
>>A\c = ____

1.3077
0.2692
-0.1538_____________________________________

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?
_________Warning: Matrix is singular to working
precision._______________________________________________________________________
________________________________________________________

MAIN LABORATORY ACTIVITY: PROGRAMMING MP1'S find_xy


FUNCTION
Part 1: Programming the find_xy function
1. Write down the five equations.
K2*(h2-h1)+K1*(HL-h1)=W1
K3*(h3-h2)+K2(h1-h2)=W2
K4*(h4-h3)+K3(h2-h3)=W3
K5*(h5-h4)+K4(h3-h4)=W4
K6*(h6-h5)+K5(h4-h5)=W5
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 (7) in the 3. Math section.)
-K2-K1
K2
0
0
0
* H1 =
W1-K1*HL
K2

-K3-K2 K3

K3

0
0

H2 =

W2

-K4-K3 K4

H3 =

W3

K4

-K5-k4

K5

H4 =

W4

K5

-K5-K6 *

H5 =

W5-K6*HR

3. K = _____HF./d____________________________________________

4. A_main = diag( - (K(_____1:N______________) + K(___2:N+1_______________) ) ,


_____0_______ ) ;
5. A_upper = diag( K( ___2:N___________________ ) , _____1_________________ );
6. A_lower = diag( K(______2:N_________________) , _______-1________________ ) ;
7. W(1) = W( ____1_____________ ) - _K(1)*HL____________________________ ;
W(N) = W( _____N___________ ) - __K(N+1)*HR____ ;
8. h = ________A\W___________________ ;
9. h = _______h'______________ ;
10. h = ______[HL h HR]_____ ;
11. x = ______ x=[0 cumsum(d)]_______ ;

Anda mungkin juga menyukai