Lecture 2 vectors and matrices ROW VECTORS Enter the following in SciLab:
[1,2,3] scilab notation for row vectors [8]==8
a=[2 3 4] separate entries with spaces or commas b=[10,10,10]
a+b, b-a add, subtract the respective coordinates 2*a, a+1 scalar product and addition
a**2, 2**a
a*b wrong dimensions for matrix multiplication a.*b pointwise multiplication
not matrix muliplication [a,b] a followed on the right by b.
’,’ means ’continues on right’.
[a;b] first row a, second row b.; starts new row below. ’;’ means ’starts new row below’.
a(1), a(2), a(3)... a(i)= ith entry of vector a.
For the first time this semester, we are going to use a for loop. A for loop is a statement which allows the computer to execute repeatedly, for a given number of iterations, a sequence of instructions. for i=[3,1,5], disp(2*i), end
In this example, the values of the entries of vector [3,1,5]will be con-secutively assigned to the loop variable i. For each of these values, the machines displays (using the built-in function disp) the value 2*i. Once all the values have been assigned, the loop stops. As a result, we obtain the output :
6 =2*i for i=3 2 =2*i for i=1 10 =2*i for i=5
COLUMN VECTORS a=[1;2;3]
b
b=b’ transpose
a+b, b-a, a.*b operations are performed component wise [a,b] column a followed on the left by column b
[a;b] append b below a.
OPERATIONS ON VECTORS
sum(a) sum of entries ,sum( [4,3,2] ) = 9
prod(a) product of entries, prod( [4,3,2] ) = 24 max(a) largest entry max( [4,3,2] ) = 4
min(a) smallest entry min( [4,3,2] ) = 2
length(a) number of entries in the vector, length( [4,3,2] )=3 not the geometric length or magnitude of the vector! a(1) = first.entry of a
a(length(a))= last entry of a PROGRESSIONS
[0,1,5] row vector [0;1;5] column vector
[0:1:5] start with 0, step size 1, end with 5 0:2:8 step size 2, [ ] may be omitted. [1:.1:2] step size .1
0:5 abreviation for [0:1:5] [5:-1:1]
Open SciNotes. Reminder: All classwork and homework Scilab code lines including testing lines are to be entered in SciNotes (not in Scilab). c2.1 SelectFile/New. File/Save as c2.1 Copy/paste the template lines to SciNotes. Fill in the 3blanks.
Write as a progression ([n:s:m]) the vector:[2.0,1.9,1.8,..,0.0]] (should be 9 symbols with n + s + m = 1.9).
DOT (INNER) PRODUCT
[a,b,c].[a’,b’,c’]= a*a’+b*b’+c*c’= the dot product.
The above formula only works for vectors of length 3. The dot-product function (built into Matlab but not Scilab) must work for vectors of any length. We have to define it.
function P = dot product(a,b) //Type in SciNotes P = sum(a.*b) //3 function lines endfunction
dot product([1,2],[3,4]) //2 testing lines dot product([1,2,3],[4,5,6])
//Answers: 11, 32.
Scilab (Matlab) has lots of built-in functions. When asked to write a function, you are to write your own. Do not use a built-in function which does the same thing. This week, use only the functions provided in this lecture.
e2.1 Write a function H(a) for the sum of the cubes of the components of a vector a, i.e., a3
1+a32 +· · ·+a3n. Test it on input provided by the user (see lecture 1).
function x=H(a) x=sum(a**3) endfunction
a=input( Enter a vector ) disp(H(a))
MAGNITUDE (GEOMETRIC LENGTH) √a2+b2+c2
This formula only for vectors of length 3. Let’s write a function which works for vectors of any length.
c2.2 File/Save as c2.2. Copy/paste the commented template lines to SciNotes. Define a function mag(a) which calculates the geometric
function, write lines which gets a vector a from the user and displays mag(a).
//c2.2 mag(a)=geometric length of a.
//Test on user input: a = input(’Enter a vector. > ’)
NOTATION. In Matlab we often writea1ora 1fora1. Variable names
may include characters, numbers or underscores ’ ’. MATRICES AND MATRIX MULTIPLICATION Enter in SciLab:
a=[1 2; 3 4] b=[1,1;1,1]
2*b,a+b, a-b, a.*b, a*b
[n,m]=size(a) n= number of rows m= number columns Matrix multiplication: ( a11 a12 a21 a22 ) ∗ ( b11 b12 b21 b22 ) = ( a11b11+a12b21 a11b12+a12b22 a21b11+a22b21 a21b21+a22b22 )
The entry cij in the ith row and jth column of the product is thedot product of the ith row of the first matrix and the jth column of the second matrix.
a=[1 2;3 5]
a(1,2),a(2,1) a(i,j) = entry in ith row, jth column a(2,2)=4 changes a(2,2) to 4
a,a(:,:)
a,a(:,1),a(:,2) a(:,j)=all rows of jth column=jth column a,a(1,:),a(2,:) a(i,:)=all columns of ith row=ith row a
a(1,2)=10 a(:,2)=[9;9] a(1,:)=[0,0] b
[b,[0;0]] appends new column on left [b;[0,0]] appends new row below
z=b saves b to z
b(1,:)= b(1,:)+2
b=z recovers b from z
b(:,1)= b(:,1)-2 What does this do?
IDENTITY MATRIX, MATRIX INVERSES b=ones(1,3)
b=0*b
b=ones(3,1) b=8*b
I=eye(2,2) I is the 2x2identity matrix
1 is the identity for multiplication of numbers: 1x = x1 = x. I=eyes(n) is the identity for matrix multiplication: if A is n × n then Ia=aI=a.
a, I*a, a*Ix−1 is the inverse operation of multiplication of numbers: xx−1 =x−1x= 1.
The notation is the same for matrices.
inv(a) is the inverse of the matricea. a=[1,2;3,4] inv(a)
a*inv(a),inv(a)*a
e2.3 Write a function J(a) which adds a row of 8’s above the matrix a and then adds a column of 9’s on the right. Test it on [1 2; 3 4] and on[1 2 3; 4 5 6; 7 8 9].
function a=J(a)
[n,m]=size(a) // n= number of rows, m= number columns a=[8*ones(1,m);a]
a=[a,9*ones(n+1,1)] endfunction
b=[1,2; 3,4],J(b)
J([1,2,3; 4,5,6; 7,8,9])
c2.3 Select File/New in SciNotes. File/Save as c2.3. Paste template. Together, one step-and-test at a time.
Supposeais a matrix with 2 or more rows. Write a functionK(a)which adds 1 to a’s bottom right entry.
Then negates (that is to say multiply by -1) a’s next-to-the-last row. Then appends a column of 0’s to the right.
Test it on[1,2; 3,4],[1,2,3; 4,5,6; 7,8,9].
//c2.2 Function K(a) adds one to bottom right entry.
// Negates next-to-last row. Appends a column of 0s on the // right.
// Test on [1,2; 3,4;5,6] and on [1,1,1,1;2,2,2,2;3,3,3,3] // Answers: [1,2,0;-3,-4,0;5,7,0],
SOLVING SIMULTANEOUS EQUATIONS
In mathematics, solving a linear system of equations can be per-formed by computing the so-calledreduced row echelon form of the augmented matrix corresponding to the system.
e2.4
Solve Corresponding augmented matrix 2x+ 4y= 2 x+ 3y= 3 a = ( 2 4 2 1 3 3 )
We perform operations on the rows of the matrix a to compute its re-duced row echelon form:
• all zero rows are at the bottom of the matrix,
• the the leading coefficient of a non-zero row appears to the right of the leading coefficient of the row above it,
• all the entries in a column below a leading entry are zeroes. The solution of the system is deduced from the last column of the reduded row echelon form of the a.
Solution Correspondingaugmented matrix x=−3 y= 3 a= ( 1 0 −3 0 1 2 ) In scilab enter [2,4,2;1,3,3] rrefa e2.5 Solve 2x+ 4y= 6 x+ 2y= 3 . In scilab enter a=[2,4,6; 1,2,3] rref(a) We obtain rref(a)= [ 1 2 3 0 0 0 ] which means x+ 2y= 3 0 = 0
Hence x= 3−2y where y is arbitrary (it can be any number). c2.4 Select File/New, File/Save as c2.4 Solve
x−16y+ 4z = 50 x+ 12y+ 2z = 30 2x−4y+ 6z = 80
Enter the augmented matrix line and the rref line in SciNotes. After //, hand-write the answer to two decimals places.
//c2.4 Two SciNote lines, plus comment. Dont copy answers from //SciLab.
// x=0, y=...
c2.5 Select File/New, File/Save as c2.5 Solve
x−16y+ 4z = 50 x+ 12y+ 2z = 30 −x+ 68y−z = 0
Enter the augmented matrix line and the rref line in SciNotes. //c2.5 Two SciNote lines, plus comment. Dont copy answers from //SciLab.
HOMEWORK 2 DUE BEFORE NEXT TUESDAYS CLASS h2.1(3), h2.2(3), h2.3(3)
email to: gautier@math.hawaii.edu subject line: 190 h2(9)
h2.1(3). The distance between 2 vectors [a1, . . . , an] and [b1, . . . , bn] is given by √(a1−b1)2+· · ·+ (an−bn)2.
Write a Scilab function for the distance D=distance(a,b) between vec-tors a and b. Use it to find the distance between[1,2] and [3,3] and the distance between [1,2,3] and [3,3,3]. Your function must work for vectors of any length, it may not use +...+. See examplee2.1above. Don’t send answers. Try entering the formula one step-and-test at a time.
// h2.1(3)function D = distance(a,b)
// Distance between [1,2],[3,3] and between
// [1,2,3],[3,3,3], 3 function lines, 2 test lines. // Answers: 2.23..., 2.23...
h2.2(3) In SciNotes, write a Scilab function F(a) which, on a matrix a, appends a row of 0’s to the bottom of a and then appends a column of 3’s on the right of the resulting matrix.
Hint: 8*ones(r,1) is a column of r 8’s (see example e2.3). Test it on [1,2,3; 4,5,6; 7,8,9] and on[1,2; 3,4; 5,6 ]. Your function
must work for any matrix of any size. / h2.2(3) Function F(a) // appends a row of 0s at the bottom of a,
// then appends a column of 3’s on the right. // Test on [1,2,3; 4,5,6].
//Answer: [1,2,3,3; 4,5,6,3; 0,0,0,3] // Test on [1,2; 3,4; 5,6].
//Answer: [1,2,3; 3,4,3; 5,6,3; 0,0,3]
h2.3(3) In SciNotes, write a Scilab functionG(a) which, on a matrix a, multiplies the first column by 10, then subtracts 100 from the last row. Hint: n=length(a(:,1))= index of the last row. (see example e2.3). Test it on [1,2,3; 4,5,6; 7,8,9] and on [1,2; 3,4; 5,6]. Your function must work forany matrix of any size. // h2.2(3) Function G(a)
// multiplies first column by 10 then // then subtracts 100 from the last row 2 // then appends a column of 3’s on the right. // Test on [1,2,3; 4,5,6].
//Answer: [10,2,3; -60,-95,-94] // Test on [1,2; 3,4; 5,6]. //Answer: [10,2; 30,4; -50,-94]