Labs (please see syllabus for due dates):
I like google custom search so I made one with a bunch of Matlab related sites referenced. Try it out if you like.
This lab is primarily going to be to introduce students to Matlab. I'm going to explain my background in Matlab, how I've been using it, and basic 'gotchas'. While I will give some instructions on using Matlab, and answer all questions as best I can, for the bulk of the lab time I will ask students to go though several tutorials on Matlab basics, this is because these short tutorials demonstrate the features I want to introduce better than would presenting the code on the board. I list several tutorials below.
A particular issue: Elementwise Operations
If you have a system Ax = b of n equations in n unknowns, there are three possible solutions scenarios.
mldivide: Backslash or left matrix divide, A\B is the matrix division of A into B, which is roughly the same as INV(A)*B , except it is computed in a different way. If A is an N-by-N matrix and B is a column vector with N components, or a matrix with several such columns, then X = A\B is the solution to the equation A*X = B. A warning message is printed if A is badly scaled or nearly singular. A \ EYE(SIZE(A)) produces the inverse of A.
Try this:
A = [2 1 3;1 -2 5;3 4 -2];
b = [-2;-13;15];
x = A\b;
A*x
abs((A*x) - b)
Now try this:
A = [1 -1 0;-1 1 -1;5 -5 3];
b = ones(3,1);
x = A\b;
A*x
abs((A*x) - b)
Due to round-off error, Matlab cannot determine exactly if the matrix is singular, but it suspects that this is the case. A check reveals that AB ~= I, evidence that matrix B is not the inverse of matrix A.
Please do these exercises , the answers are included.
Another reference
Important note:A student informed me that when going over problem 15 in the excercises he did not get the same results as the solutions listed. This is because the working precision of the matrix was lower than the precision set in the authors variable. This can be changed using the 'vpa' command linked to above. To see the working precision of a variable you can use the 'eps' command also referenced above. A final note, if a system you are working with is singular and has infinite solutions you have no guarantee that Matlab will find the same solution for you as it did for the author of the excercises, you can check whether the solution is valid by performing the calculation A*x, and testing whether the result is equal to b with some degree of tolerance e.g some variable = 0.9999999 rather than 1. If you perform the calculation in problem 15, with the addition of the commands 'A = vpa(A,20); b = vpa(b,20);' you should get one of an infinite number of solutions for the same one.
Another note:
You can set the displayed precision higher by entering the command: format long e
For more information enter help format
Work on lab 1, posted at the top of this page. Ask me questions if you have any, I'll try to help without giving away the answers...
2 notes. First of all a student asked me to post some example behavior from the function in lab 1, I give some below. Secondly, upon later reflection I decided that the precision portion of homework 1, problem 1 may be a bit hard for you. I've decided to do this: I will be very lenient on the grading of that portion of the lab, just try to get your answers in the ballpark of the number of digits of the answer that you can trust, I won't grade down if you are off by a few digits. Also, I am posting several links that should explain the situation with precision in Matlab, and I give some hints on what you can try to explore this situation. Also, you are only required to look at cases where the variables are of class 'double', 'single' or 'symbolic' where a symbolic variable is only passed if it was created by calling 'digits(P)' and then 'vpa(A)' here A is the variable passed to solveSLS, P is and integer value. Again, you don't have to get the exact values for p as I get for p in the examples below. But the answers should be somewhere near there. Where my numbers come from, and why I will alow some ambiguitiy is explained in detail in the links 'Double' and 'Single' below. Rememmber I'm asking you to give me the sig figs in decimal not binary (this introduces some complications which are discussed in those links).
Try These Commands:
Hint 1:
>> D = 1000000000000000
D =
1.000000000000000e+015
>> D == 1000000000000001
ans =
0
>> D = 10000000000000000
D =
1.000000000000000e+016
>> D == 10000000000000001
ans =
1
Hint 2:
>> Z = single(10000000)
Z =
10000000
>> Z == single(10000001)
ans =
0
>> Z = single(100000000)
Z =
100000000
>> Z == single(100000001)
ans =
1
>> Z = single(99999990)
Z =
99999992
>> class(D)
ans =
double
Examples:
%Example 1
>> digits(64)
>> A = vpa([1,2,3;4,5,6;7,8,9])
A =
[ 1.0, 2.0, 3.0]
[ 4.0, 5.0, 6.0]
[ 7.0, 8.0, 9.0]
>> b = vpa(ones(3,1))
b =
1.0
1.0
1.0
>> [x,s,p] = solveSLS(A,b)
Warning: System is rank deficient. Solution is not unique.
x =
-1.0
1.0
0
s =
2
p =
64
>> A*x
ans =
1.0
1.0
1.0
>> b
b =
1.0
1.0
1.0
%Example 2
>> A=[1.5 2.3 7.9; 6.1 3.2 13; 13 21 76]
A =
1.5000 2.3000 7.9000
6.1000 3.2000 13.0000
13.0000 21.0000 76.0000
>> b = [1;3;5]
b =
1
3
5
>> [x,s,p] = solveSLS(A,b)
x =
0.8580
3.0118
-0.9132
s =
1
p =
15
>> A*x
ans =
1.0000
3.0000
5.0000
>> det(A)
ans =
-38.9300
%Example 3
>> A = [1 1 1; 2 2 2; 4 1 9]
A =
1 1 1
2 2 2
4 1 9
>> b = ones (3,1)
b =
1
1
1
>> [x, s, p] = solveSLS(A, b)
Warning: Matrix is singular to working precision.
> In solveSLS at 7
x =
-Inf
Inf
Inf
s =
0
p =
0
%Example 4
>> A=single([23 56 1; 1 5 7; 1 66 2])
A =
23 56 1
1 5 7
1 66 2
>> b = 7*ones(3,1)
b =
7
7
7
>> [x, s, p] = solveSLS(A, b)
x =
0.0773
0.0766
0.9343
s =
1
p =
6
Work on lab 1, posted at the top of this page. Ask me questions if you have any, I'll try to help without giving away the answers...
Monday: Final section before the lab is due. Tuesday: Ask questions you didn't get a chance to resolve. I will be handing back homework 1 (what you turned in on the 22'nd) in the lab sections.
Your labs have been graded and are being returned. You can get comments about how your score was arrived at from the 'Assignment Return' dropbox on EEE. Below are my explanations of the grading for the lab.
Warning: If you have issues with the grade, you MUST read and understand all I have written below for the problem you have issue with. Do not contact me asking me a grading question I have already answered in the descriptions below.
Problem 1:
There are 6 cases. The _t variables are the answers I got.
For the returned x variables you had to achieve equality modulo a small tolerance.
For s you had to get my answer exactly to get the point.
If you got within +-1 or +-2 of the # of digits of precision for single, and
double you got credit of that portion of the test cases.
If you get between digits, and 2*digits you got credit for VPA calculations of
precision.
Each test is worth 1 point, (1/3 for getting within the specifed bounds for
each of x,s,p. I take the average of your scores for the 4 test cases for the
grade for the problem. Except for test case 4, there 100% of the score is assigned
for getting 's' right. In test 3 I don't have an x_t, that is because there were
many possible x values that could be returned, as long as your x satisfied
A*x - b <= t, for a small t.
Sometimes I manually add or remove +- 0.05 to the score if the code shows that
you knew more/less than the raw test case results indicate.
If you got a 0 due to the code not running I give something between 0 and 0.2
based on what was submitted.
Your score for each problem is given as :
a b c
where a is the score for your x, b is the score for your p, c is the score
for your s. Each such line gives the score for each of the test cases
in the same order as listed below.
If you have less than 6 such lines, that means one of the tests was not able
to run on your code, ie you get 0 for all parts of it.
The last line is the full score for the problem.
Test 1:
A = [1 0 1; 0 -3 1; 2 1 2];
b = [6;7;11];
[x,s,p] = solveSLS(A,b);
x_t = [2;-1;4];
p_t = 15;
s_t = 1;
Test 2:
A = single([2 1 3; 2 6 8; 6 8 18]);
b = single([1;3;5]);
[x,s,p] = solveSLS(A,b);
x_t = [(3/10);(2/5);0];
p_t = 5;
s_t = 1;
Test 3:
digits(20)
A=vpa([3 1 -6; 2 1 -5; 6 -3 3]);
b=vpa([-10;-8;0]);
[x,s,p] = solveSLS(A,b);
p_t = digits();
s_t = 2;
Test 4:
A = [1 0 1; 1 1 1; 1 -1 1];
b = [1; 2; 1];
[~,s,~] = solveSLS(A,b);
s_t = 0;
Test 5:
digits(20);
A = vpa([2 1 3;1 -2 5;3 4 -2]);
b = vpa([-2;-13;15]);
[x,s,p] = solveSLS(A,b);
p_t = digits();
s_t = 1;
Test 6:
A = [1 -2 2 0 -2;-1 -1 -1 -2 0;1 -2 0 0 0;-1 -1 1 0 -2;-1 1 0 1 0];
b = ones(5,1);
[x,s,p] = solveSLS(A,b);
p_t = 15;
s_t = 1;
Problem 2 :
Given below are the matrices I pass to your code (A), and the answers I expect
back (B). IMPORTANT: If instead of infs you have 0's I counted that as correct.
I ignore your answers along the main diagonal, (i.e B(1,1), B(2,2) ) there
was ambiguity as to what was expected there so I ignored those when grading.
To be absolutely clear if you said B(1,1) = -1000 I counted that as correct.
Finally to get the score for each case I took the average of the differences between
your matrix and mine (modulo the diagonal and cases of inf <=> 0 ).
The grade for the problem is the mean of the score for all test cases.
Test 1:
A = [0 1 1 0 0 0 0; 0 0 0 1 1 0 0; 0 0 0 0 0 1 1; 0 0 0 0 1 0 0; 0 0 0 0 0 1 0; 0 0 0 0 0 0 1; 0 0 0 1 0 0 0];
B = [0 1 1 2 2 2 2; Inf 0 Inf 1 1 2 3; Inf Inf 0 2 3 1 1; Inf Inf Inf 0 1 2 3; Inf Inf Inf 3 0 1 2; Inf Inf Inf 2 3 0 1; Inf Inf Inf 1 2 3 0];
Test 2:
A = [0 1 0 0; 0 0 1 0; 0 0 0 1; 1 0 0 0];
B = [ 0 1 2 3; 3 0 1 2; 2 3 0 1 ; 1 2 3 0];
Test 3:
A=[0 1 1 0 0 1 0; 1 0 1 0 0 0 0; 0 0 0 1 0 1 0; 0 0 1 0 1 0 0; 0 0 0 0 0 0 1; 1 0 1 0 0 0 0; 0 0 0 1 0 0 0];
B=[0 1 1 2 3 1 4; 1 0 1 2 3 2 4; 2 3 0 1 2 1 3; 3 4 1 0 1 2 2; 5 6 3 2 0 4 1; 1 2 1 2 3 0 4; 4 5 2 1 2 3 0];
Test 4:
A=[0 1 0 0 0 0; 0 0 1 0 0 0 ; 1 0 0 0 0 0; 0 0 0 0 1 1; 0 0 0 0 0 0; 0 0 0 0 0 0];
B=[0 1 2 Inf Inf Inf; 2 0 1 Inf Inf Inf; 1 2 0 Inf Inf Inf; Inf Inf Inf 0 1 1; Inf Inf Inf Inf 0 Inf; Inf Inf Inf Inf Inf 0] ;
Problem 3 :
Nobody got the extra credit so I don't mention it.
Full credit if you plot what is asked for, for the series you were asked for,
explanation is something like: "Error increases as n increases, up until [this]
point" where [this] depends on your graph.
Only 1 lab this week, Monday this week is veteran's day. Work on lab 2, posted at the top of this page. Ask me questions if you have any, I'll try to help without giving away the answers... You can also ask questions from the midterm.
Here to answer your questions.
Lab 2 and homework 2 returned. Lab 2 Grading:
Each question is worth one point.
Each multi-part (a,b,c,d...) question is still worth one point. Each part of such a question is worth an equal portion out of that one point. For example if a question has parts: a,b,c,d
each of those is worth one point. If there are '1' marks next to parts of a question that means you got that fraction right. For example if there is a '1' next to 'a' a '1' next to 'b'
a '1' next to 'c' and a '1' next to 'd' that does not mean you have 4 points, it means you have 1 point (1/4 a point per part).
In part 5.1 there are two "try this operation and then describe it in words" type questions. Each is worth half a point so together they are worth 1 point. If you
see a '1' mark next to both but after addition determine you are missing a point, you are probably not, even though I mark the definitions with a '1' I only count both of them
together as 1 point.
Each section of the lab (5.1,5.2,etc) was assigned a score (the mean of your scores on the questions in that part) then the average of those means was taken. Call this grade_1.
Then another grade (grade_2) was calculated by taking the mean score for all questions in labs 5 and 6 (as though all the questions were in one 'section').
Your grade is the maximum of grade_1 and grade_2.
I will not be doing selective regrading for this assignment. If you want a regrade, write a description of the problems in the original grade and submit it to me along with the
assignment, I will regrade the WHOLE assignment. I may find mistakes I made where I allocated you more points than you were due, I may find the opposite, you will receive
a replacement grade for the assignment based on my regrade. The new grade may be higher or lower than the original it depends whether the balance of my mistakes is
in your favor or not.
Homework 2 Solutions:
For this homework, each of the parts in a multi-part question
counted as 1 point, e.g questions 17 and 18 are worth 3 points.
The multi-part true false questions are each worth 1 point only,
each of their subparts is worth an equal portion of that point.
So the homework is out of 11 points.
Edit: I noticed that for the last T-F problem, I had thought that there were
19 sub questions, instead there are 16. I decided to label this bug a feature and just keep the
current scheme, basically this means that you all get 3/19'ths of 1 point for free ! So again
if on the last problem you see something like 17/19 even though there are only 16 sub-questions
that means you missed 2 of the subquestions.
Ch 4 Supplementary exercise 1 parts a-s:
T T F F T T F F T F F F T F T T F T T
If you need clarification as to some part of the question ask me or Dr. Hayes.
Section 2.5 #17
U^(-1) : [ 1/2 -3/4 -2 ; 0 -1/4 -1; 0 0 -1/2]
L^(-1) : [ 1 0 0; 2 1 0; -2 -1 1]
A^(-1) : [ 3 5/4 -2; 3/2 5/4 -1; 1 1/2 -1/2]
Section 6.4 numbers 3, 7, 17, 18.
3. [2 -5 1], [3 3/2 3/2]
7. ( 1 / SQRT(30) )*[2 -5 1] , ( 1 / SQRT(6) )*[2 1 1]
17.a) False, if c=0 then {v1,v2,0} is not a basis for W.
17.b) True, Gram-Schmidt theorem.
17.c) True, columns of Q are orthonormal then Q^(T)*Q = I {Thm 6}
So Q^(T)*A = Q^(T)*(QR) = IR = R
18.a) False, 3 orthogonal non-zero vectors are needed to be a basis for a 3-D subspace.
18.b) True, If x is not in subspace W, then x cannot equal proj_W(x) because proj_W(x) is in W.
18.c) True, by theorem 12.
Chapter 3 Supplementary Exercise #1.
T T F F F F T T F F T F F T F T
If you need clarification as to some part of the question ask me or Dr. Hayes.