Lab2 - Matlab PDF

Title Lab2 - Matlab
Author HRISHIKESH GALANDE
Course Introduction To Computation In Engineering Design
Institution The University of British Columbia
Pages 9
File Size 147.5 KB
File Type PDF
Total Downloads 89
Total Views 127

Summary

Matlab...


Description

MATH 152 Computer Lab 2 Instructions 1. Login to matlab.mathworks.com (or another MATLAB development environment). 2. Follow the examples and enter the commands listed below. The examples give you the tools you need to complete the exercises. 3. The answers for each exercise are MATLAB variables which could be a number, vector, matrix or text. The goal of the lab is to create variables for each exercise with the correct data. Make sure to save the variable for each exercise with the correct variable name. 4. When you are done, save all your work in a MATLAB .mat file and submit the .mat file to Canvas (see the video on Canvas in the “Getting Started with MATLAB” module about how to submit a .mat file). 5. Visit the MATLAB Help Center for extra help.

Learning Goals After completing this lab, you should be able to: ◦ Use MATLAB functions to compute the dot product, cross product, projection, length, angle between vectors, and area of a parallelepiped. ◦ Select and modify entries and sub-matrices of a matrix. ◦ Use MATLAB functions to create large matrices. ◦ Use MATLAB functions to compute the transpose, determinant, and reduced row echelon form of a matrix. ◦ Use MATLAB functions to compute the solution of a linear system Ax = b.

MATLAB Functions In the last assignment, we introduced a few MATLAB functions for computing with vectors: Function

Description

dot(v,w)

compute the dot product of vectors v · w √ compute the square root of a number x √ compute the norm of a vector kvk = v · v

sqrt(x) norm(v) cross(v,w)

compute the cross product of vectors v × w 1

We also have all the usual mathematical functions: Function

Description

abs(x)

|x|

sin(x)

sin x

cos(x)

cos x

tan(x)

tan x

asin(x)

arcsin x

acos(x)

arccos x

atan(x)

arctan x

exp(x)

ex

log(x)

ln x (the natural logarithm)

For example, the angle θ between vectors v and w is related to the dot product by the formula v · w = kvkkwk cos θ Therefore we can compute the angle between vectors v = (1, 2, 3, 4) and w = (1, −1, 1, 0) by entering the commands: >> v = [1 2 3 4]; w = [1 -1 1 0]; >> theta = acos( dot(v,w) / (norm(v) * norm(w)) ) theta = 1.3584

Use the help command to display the documentation for any function. For example, to learn about the function cross, enter the command: >> help cross Check out the complete MATLAB documentation online: www.mathworks.com/help/matlab.

Exercise 1 (a) Create a row vector with 4 entries using the first 4 digits of your student number and save it to the variable OneAvec1. Create another vector using the last 4 digits of your student number and save it to the variable OneAvec2. Use the MATLAB functions outlined above to compute the projection of OneAvec1 onto OneAvec2 and save the result as OneAvec. Recall, the projection of a vector v onto a vector w is given by

2

v·w w w·w For example, if your student number is 12345678 then compute the projection of (1, 2, 3, 4) onto (5, 6, 7, 8). (b) Create a row vector using the first 3 digits of your student number and save it to the variable OneBvec1. Create another vector using the next 3 digits of your student number and save it to the variable OneBvec2. Finally, create another vector using the last 2 digits of your student number along with the number 0 and save it to the variable OneBvec3. Use the MATLAB functions outlined above to compute the volume of the parallelepiped spanned by these 3 vectors and save the result as OneBnum. Recall, the volume of the parallelepiped spanned by vectors u, v and w is the triple product u · (v × w) For example, if your student number is 12345678 then compute the volume spanned by the vectors (1, 2, 3), (4, 5, 6) and (7, 8, 0). (c) Compute the distance from the point P = (1, −2, 3) to the plane defined by the parametric equation       1 5 3 1  + t 1  + s  2  2 2 −1 Save the result as OneCnum.

Working with Matrices We say a matrix is n by m if it has n rows and m columns. For example, consider the following 3 by 4 matrix: >> A = [1 2 1 2; 3 4 3 4; 5 6 5 6] A = 1 3 5

2 4 6

1 3 5

2 4 6

How can we extract pieces of A? Individual entries can be accessed by specifying the row n, counting down from the top, and column m, counting from the left, using the notation A(n,m). For example, the entry in row 2 and column 3 is: >> A(2,3) ans = 3 3

We can change this entry to 7 by entering the command: >>

A(2,3) = 7

A = 1 3 5

2 4 6

1 7 5

2 4 6

To extract a sub-matrix, we must specify a range of rows and columns. For example, (assuming you have changed the A(2,3) entry to 7 as above) we can access the sub-matrix in rows 1 and 2 and columns 2 and 3: >> A(1:2,2:3) ans = 2 4

1 7

We can also use this syntax to modify the entries. Let’s set all these entries to zero: >> A(1:2,2:3) = [0 0;0 0] A = 1 3 5

0 0 6

0 0 5

2 4 6

When specifying ranges of rows and columns, use end to denote the last row or column. This is handy if we are not sure how large the matrix is. For example, enter the command A(end,end) to check the element in the last row and last column: >> A(end,end) ans = 6 If we want the second to last entry in the first row, enter the command: >> A(1,end-1) ans = 0 The range 1:end can specified by : therefore A(1,:) is a way to reference the entire first row: 4

>> A(1,:) ans = 1

0

0

2

This is handy notation if you want to perform operations on a single row or column of a matrix. For example, if you were performing row operations, you may want to modify a matrix so that the third row is the result of subtracting the second row from the third: >> A(3,:) = A(3,:) - A(2,:) A = 1 3 2

0 0 6

0 0 5

2 4 2

You may have already learnt in the lectures that performing operations on the individual rows of a matrix is a common technique to find the solution of a linear system. So far we have created matrices using the bracket syntax. For example, to create the matrix   2 0 1 9 −1 9 9 9

we enter the command;

>> A = [2 0 1 9; -1 9 9 9] A = 2 -1

0 9

1 9

9 9

However, manually creating a matrix this way is not a good idea when the matrix is large. Thankfully, there are MATLAB functions for easily creating simple matrices of any size: Function

Description

ones(n)

create a square matrix of size n with all entries set to 1

ones(n,m)

create a n by m matrix with all entries set to 1

zeros(n)

create a square matrix of size n with all entries set to 0

zeros(n,m) eye(n) eye(n,m)

create a n by m matrix with all entries set to 0 create the identity matrix of size n create a n by m matrix with diagonal entries set to 1 and other entries set to 0

For example, create a square matrix of size 5 with zeros along the diagonal and ones everywhere else: 5

>> M = ones(5) - eye(5) M = 0 1 1 1 1

1 0 1 1 1

1 1 0 1 1

1 1 1 0 1

1 1 1 1 0

Finally, there are several MATLAB functions for computing with matrices:

Function

Description compute the transpose X T of a matrix X

X' det(M) rref(A)

compute the determinant det(M ) of a square matrix M compute the reduced row echelon form of a matrix A

linsolve(A,b)

compute the solution of the linear system Ax = b where A is a square matrix

A\b

compute the solution of the linear system Ax = b where A is a square matrix

For example, create the matrix  −3 8 −2 1  6 −4 1 5   A=  2 5 −8 8  1 5 −8 −7 

and the column vector  18  12   b=  44  −2 

and form the augmented matrix >> A = [-3 8 -2 1;6 -4 1 5;2 5 -8 8;1 5 -8 -7]; >> b = [18; 12; 44; -2]; >> M = [A b] M = -3 6 2 1

8 -4 5 5

-2 1 -8 -8

1 5 8 -7

18 12 44 -2

6

We can use the function rref to compute the reduced row echelon form of the augmented matrix to compute the solution of the system Ax = b >> rref(M) ans = 1 0 0 0

0 1 0 0

0 0 1 0

0 0 0 1

1 2 -1 3

Or use the function solve to compute the solution >> linsolve(A,b) ans = 1.0000 2.0000 -1.0000 3.0000 Equivalently: >> A\b ans = 1.0000 2.0000 -1.0000 3.0000

Exercise 2 (a) Use the MATLAB functions and examples outlined above to create the 4 by 8 matrix   1 1 1 1 1 1 1 1 1 0 0 0 0 0 0 1   1 0 0 0 0 0 0 1 1 1 1 1 1 1 1 1

Save the result as TwoAmat. Try to create the matrix with the shortest command(s) possible. In particular, don’t type each entry individually. (b) Suppose we have a large matrix saved to a variable B. What line of code computes the projection of the first row of B onto the third row of B? Save the code as text and assign the text to a variable TwoBcode. Execute the following commands to see if your code is correct: 7

>> B = [1 3 1 0; 7 8 1 3; 6 5 4 1]; eval(TwoBcode) ans = 1.9231

1.6026

1.2821

0.3205

>> B = [1 1 1; 2 2 2; 3 3 -3; 4 4 4]; eval(TwoBcode) ans = 0.3333

0.3333

-0.3333

(c) Create the matrix X by entering the command: >> X = [7 3 3 1; 5 4 9 1; 0 2 4 5]; Compute the reduced row echelon form of X T and save the result as TwoCmat. Are the rows of X linearly independent? Enter your response as text 'yes' or 'no' and save the text as TwoCword. (d) Create the augmented matrix [A b]  0 1 A= 0 1

where 0 0 0 0

1 1 1 0

 1 1   0  1



 1  1   b=  1  1

Compute the reduced row echelon form of the matrix and save the result as TwoDmat. How many solutions does the system have? Enter your response as either 'infinite', 'unique' or 'none' and save the text as TwoDword.

Hand-in Submit your work in a single .mat file containing all your variables. (Do not save your work as a .m file or a text file. Your work will not be graded.) To create your .mat file: 1. Check that you have a value stored for each of the questions you were asked this week. Your Workspace should contain: OneAvec1, OneAvec2, OneAvec, OneBvec1, OneBvec2, OneBvec3, OneBnum, OneCnum, TwoAmat, TwoBcode, TwoCmat, TwoCword, TwoDmat and TwoDword. 2. Save your variables to a .mat file by entering the command >> save('assignment2.mat') Or right click the Workspace window and select Save. 8

3. Download your .mat file: click on the file assignment2.mat in the Current Folder window and then click Download. 4. Upload your .mat file to Canvas in the assignment called MATLAB Computer Lab 2. 5. Visit the MATLAB Help Center if you need more help.

9...


Similar Free PDFs