math mathematics math 123, Study notes of Mathematics

math math mathematics math matnematics 123

Typology: Study notes

2022/2023

Uploaded on 10/06/2023

Cucumber123Cucumber
Cucumber123Cucumber 🇺🇸

7 documents

1 / 6

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
MAST10007 Linear Algebra 2021 Semester 2 MATLAB Computer Lab 6
6 Computer Lab 6: Span and Linear Independence
6.1 Introduction
Today we continue to explore linear combinations, linear (in)dependence and span. As part of this, we will
write a simple program that uses a for-loop and then save it as an m-file.
You will need to use the m-files that you downloaded into MATLAB during Computer Lab 3.
If you do not have the m-files, then follow the instructions on the Computer Lab Class Files page of
the MAST10007 website to load the Linear Algebra m-files into MATLAB.
6.2 Exercises
Exercise 1: Drawing Vectors
We are going to use a program called drawvec1 to continue exploring spans and linear combinations graph-
ically. This program draws vectors on a graph. It requires 3 input variables:
(1) a row or column matrix representing a vector in R2
(2) a colour, and
(3) a number to determine the size of the axes on which the vector is drawn.
Enter the following commands in MATLAB:
»x=[3 -4]
»abs(x)
»m=2+max(abs(x))
What do these last two commands do?
Notice that max returns the maximum value of all the entries in a matrix and abs(x) gives the absolute
value of each entry in x.
Now enter
»drawvec1(x,0b0,m)
You should see a new window called Figure 1 appear. If necessary you can bring this to the front by clicking
on it. You should see a blue vector with its base at the origin and its end at (3,4). The second input
variable for drawvec1 was 0b0which determines the colour of the vector. The options are
0c0,0m0,0y0,0r0,0g0,0b0,0w0and 0k0
which correspond to cyan, magenta, yellow, red, green, blue, white and black. The third input, m, gives the
limits of the axes, mxmand mym.
Try drawing the vector (4,5) in green and the vector (3,7) in cyan.
We will now draw more than one vector on the same graph.
Enter
»x=[2 3];y=[-3 5];z=[4 -5]
Notice that we have entered three commands in one line. The ;is used to separate such commands. The
output only gives the result of the final command.
The University of Melbourne 1School of Mathematics and Statistics
pf3
pf4
pf5

Partial preview of the text

Download math mathematics math 123 and more Study notes Mathematics in PDF only on Docsity!

6 Computer Lab 6: Span and Linear Independence

6.1 Introduction

Today we continue to explore linear combinations, linear (in)dependence and span. As part of this, we will write a simple program that uses a for-loop and then save it as an m-file.

You will need to use the m-files that you downloaded into MATLAB during Computer Lab 3. If you do not have the m-files, then follow the instructions on the Computer Lab Class Files page of the MAST10007 website to load the Linear Algebra m-files into MATLAB.

6.2 Exercises

Exercise 1: Drawing Vectors

We are going to use a program called drawvec1 to continue exploring spans and linear combinations graph- ically. This program draws vectors on a graph. It requires 3 input variables:

(1) a row or column matrix representing a vector in R^2

(2) a colour, and

(3) a number to determine the size of the axes on which the vector is drawn.

Enter the following commands in MATLAB: »x=[3 -4] »abs(x) »m=2+max(abs(x)) What do these last two commands do?

Notice that max returns the maximum value of all the entries in a matrix and abs(x) gives the absolute value of each entry in x.

Now enter »drawvec1(x,′b′,m)

You should see a new window called Figure 1 appear. If necessary you can bring this to the front by clicking on it. You should see a blue vector with its base at the origin and its end at (3 , −4). The second input variable for drawvec1 was ′b′^ which determines the colour of the vector. The options are ′c′, ′m′, ′y′, ′r′, ′g′, ′b′, ′w′ (^) and ′k′

which correspond to cyan, magenta, yellow, red, green, blue, white and black. The third input, m , gives the limits of the axes, − mxm and − mym.

Try drawing the vector (− 4 , 5) in green and the vector (3 , −7) in cyan.

We will now draw more than one vector on the same graph.

Enter »x=[2 3];y=[-3 5];z=[4 -5]

Notice that we have entered three commands in one line. The ; is used to separate such commands. The output only gives the result of the final command.

Enter »u=[x y z] »m=2+max(abs(u)) »drawvec1(x,′r′,m) »hold on »drawvec1(y,′b′,m);hold on;drawvec1(z,′g′,m) »hold off You should have a graph showing all three vectors.

Look at the output from u=[x y z]. This is the matrix with x in the first two columns, y in the 3rd and 4th columns and z in the final two columns. As you have seen in previous labs, this is a very handy way to produce a matrix from two smaller matrices. In contrast, the command [x; y; z] produces a matrix with x in the top row, y in the second row and z in the third row.

Exercise 2: Exploration — The Span of a Vector

Now try entering the following commands which will generate 10 random multiples of the vector (1 , −1). Notice that some of the commands do not have » at the start. This is because when one enters for i=1: MATLAB expects a further sequence of commands ending in an end statement. The semi-colons at the end of the command lines are helpful. These suppress output from that command. Otherwise we would have ten lots of output in the command window.

»x=[1 -1]; »m=10; »for i=1: y=(rand-0.5)20x; drawvec1(y,′g′,m+2); hold on end »hold off

Look at the output in the Figure 1 window.

Note: If you make a mistake with your input and MATLAB is not showing the » prompt, try pressing Ctrl+C to cancel and then start over.

So what did we do?

∑ After entering x and m=10 for the axes range, we introduced, in the command for i=1:10, a counter, i , which takes the values 1 , 2 ,... , 10. Initially i is set at 1. We then work through the commands:

y=(rand-0.5)20x; drawvec1(y,′g′,m+2); hold on

When MATLAB first reaches the end command, i is set to the value 2 and the commands are then repeated. Each time MATLAB finishes the commands it increases the value of i until i = 10, when it moves on past end to hold off.

∑ The command rand generates random numbers between 0 and 1. We subtracted 0.5 from these to give numbers between − 0_._ 5 and 0.5 and then multiplied by 20, thus effectively generating random numbers between − 10 and 10.

∑ We multiplied x by the random number to produce y and graphed the result.

So we have drawn 10 random vectors in the span of x = (1 , −1).

Exercise 4: Exploration — The Span of Two Vectors

Enter »help span to find out what the program span2 does, and then »span to run it. Look at the results in the figure window. What does it look like?

We randomly generated scalars between − 10 and 10 to find our linear combinations of u and v. Would we get a similar result for scalars between − 100 and 100?

What kind of object do you think the span of u and v is?

Do u and v span R^3?

To see the code used in this program, enter »edit span

First you will notice a number of lines starting with %. These are not read as commands, but are comments which are printed in response to help span2.

The command A=[0 0 0;u] produces a matrix with the top row being the zero vector and the second row being the vector u = (1 , − 1 , 2).

Enter »A(:,1) This prints the first column of A , which corresponds to the x coordinates of the two vectors (0 , 0 , 0) and u. Try the commands B(:,2) and A(:,3). What do you obtain?

A (: , 1) =

  

   B (: ,^ 2) =

  

   A (: ,^ 3) =

  

  

The command plot3(A(:,1),A(:,2),A(:,3),′k-′) draws a line from (0 , 0 , 0) to (1 , − 1 , 2) giving a graphical representation of u. There are 4 input variables for plot3. The first three give the x , y and z coordinates respectively of (0 , 0 , 0) and (1 , − 1 , 2). The final input determines the colour, the type of line and the type of marker at the end of the line. In our case we specified the colour (black) and a solid line (-).

The command for drawing a line in 2 dimensions is plot. If you want to find out more about plot and plot3, type in help plot and help plot3 in the Command Window.

Change span2 to randomly generate u and v. Write down your code for u.

Save your new program using Save As from the toolbar, and call it myspan2.m Run the program a few times to test it.

Exercise 5: Exploration — Column, Row and Solution Space

Let A be a m × n matrix. ∑ The row space is equal to the span of the rows of A. It is a subspace of R n.

∑ The column space is equal to the span of the columns of A. It is a subspace of R m.

∑ It is always the case that:

dim(column space) = dim(row space) = rank( A )

∑ The set of solutions of the equation A x = 0 is called the solution space (or nullspace ). It is a subspace of R n.

∑ The nullity of A is defined as the dimension of the solution space.

∑ The nullity and the rank are related by the equation:

rank( A ) + nullity( A ) = n

Using the things you have learned in lectures, and the MATLAB commands you have learned in the MAT- LAB classes, find bases for the column space, row space and solution space of the following matrix. To save you typing-in the values you can run the program lab6a.

A =

   

   

Use the command format rat as in Lab Class 2 to find rational approximations in your calculations instead of decimal approximations.

Alternatively, you can use the command rats(rref(A)) to find rational approximations in your row-reduced echelon form.

Row-reduced form of A :