SlideShare a Scribd company logo
CSCI 2033: Elementary Computational Linear Algebra
(Spring 2020)
Assignment 1 (100 points)
Due date: February 21st, 2019 11:59pm
In this assignment, you will implement Matlab functions to
perform row
operations, compute the RREF of a matrix, and use it to solve a
real-world
problem that involves linear algebra, namely GPS localization.
For each function that you are asked to implement, you will
need to complete
the corresponding .m file with the same name that is already
provided to you in
the zip file. In the end, you will zip up all your complete .m
files and upload the
zip file to the assignment submission page on Gradescope.
In this and future assignments, you may not use any of Matlab’s
built-in
linear algebra functionality like rref, inv, or the linear solve
function Ab,
except where explicitly permitted. However, you may use the
high-level array
manipulation syntax like A(i,:) and [A,B]. See “Accessing
Multiple Elements”
and “Concatenating Matrices” in the Matlab documentation for
more informa-
tion. However, you are allowed to call a function you have
implemented in this
assignment to use in the implementation of other functions for
this assignment.
Note on plagiarism A submission with any indication of
plagiarism will be
directly reported to University. Copying others’ solutions or
letting another
person copy your solutions will be penalized equally. Protect
your code!
1 Submission Guidelines
You will submit a zip file that contains the following .m files to
Gradescope.
Your filename must be in this format: Firstname Lastname ID
hw1 sol.zip
(please replace the name and ID accordingly). Failing to do so
may result in
points lost.
• interchange.m
• scaling.m
• replacement.m
• my_rref.m
• gps2d.m
• gps3d.m
• solve.m
1
Ricardo
Ricardo
Ricardo
Ricardo
�
The code should be stand-alone. No credit will be given if the
function does not
comply with the expected input and output.
Late submission policy: 25% o↵ up to 24 hours late; 50% o↵ up
to 48 hours late;
No point for more than 48 hours late.
2 Elementary row operations (30 points)
As this may be your first experience with serious programming
in Matlab,
we will ease into it by first writing some simple functions that
perform the
elementary row operations on a matrix: interchange, scaling,
and replacement.
In this exercise, complete the following files:
function B = interchange(A, i, j)
Input: a rectangular matrix A and two integers i and j.
Output: the matrix resulting from swapping rows i and j, i.e.
performing the
row operation Ri $ Rj .
function B = scaling(A, i, s)
Input: a rectangular matrix A, an integer i, and a scalar s.
Output: the matrix resulting from multiplying all entries in row
i by s, i.e. per-
forming the row operation Ri sRi.
function B = replacement(A, i, j, s)
Input: a rectangular matrix A, two integers i and j, and a scalar
s.
Output: the matrix resulting from adding s times row j to row i,
i.e. performing
the row operation Ri Ri + sRj .
Implementation tips:
In each of these functions, you should check that the input
indices i and j are
of rows in the
matrix (which may not be the same as the number of columns!).
If any index
is out of range, print an error using the built-in function disp,
and return the
original matrix. This could help you diagnose problems when
you write your
RREF function in the next part.
Testing:
Test your code on rectangular m⇥n matrices of various sizes,
both when m < n
and when m > n. You can generate some simple matrices for
testing on using
the functions eye(m,n), ones(m,n), and randi(10,m,n).
2
3 RREF (40 points)
Next, you will use these row operations to write a function that
performs Gauss-
Jordan elimination and compute the reduced row echelon form
of any matrix.
We will call the function my_rref, because the rref function
already exists in
Matlab.
Specification:
function R = my_rref(A)
Input: a rectangular matrix A.
Output: the reduced row echelon form of A.
For full credit, your function should handle the following:
• Partial pivoting: At each step, you should swap the current
row with the
one whose entry in the pivot column has the largest absolute
value.
• Free variables: Due to numerical error, the entries in a column
corre-
sponding to a free variable may be extremely small but not
precisely zero.
Therefore, you should consider an entry to be zero if its
absolute value is
smaller than 10�12.
We suggest first implementing the algorithm without
considering these two issues,
then adding code to deal with them one at a time.
Implementation tips:
There are two di↵ erent ways one can implement Gauss-Jordan
elimination.
• In Section 1.2 under “The Row Reduction Algorithm”, the
book describes
it in two phases: first do Gaussian elimination (Steps 1-4), then
perform
row operations equivalent to back-substitution (Step 5).
• Gauss-Jordan elimination can also be done in a single phase:
every time
you find a pivot, perform scaling so the pivot entry becomes 1,
then perform
elimination on all the other rows, both above and below the
pivot row.
You may use either approach in your implementation. Below,
we provide
pseudocode for the latter approach.
In either case, since we want to be
able to handle free variables, the pivot
entry won’t necessarily be on the di-
agonal. Instead, you’ll need to keep
track of both the pivot row, say k, and
the pivot column, l, as you go along;
see the illustration on the right.
3
Algorithm 1 RREF
1: initialize pivot row k = 1, pivot column l = 1
3: among rows k to m, find the row p with the biggest* entry in
column l
4: Rk $ Rp
5: if akl is zero** then
6: l l + 1
7: else
8: Rk (1/akl)Rk
9: for i = 1, . . . , k � 1, k + 1, . . . ,m do
10: Ri Ri � (ail/akl)Rk
11: end for
12: k k + 1, l l + 1
13: end if
14: end while
*Here “biggest” means having the largest absolute value (abs in
Matlab).
**Consider a number to be zero if its absolute value is smaller
than 10�12.
Test cases:
my_rref([2,3,4; 1,1,0]) should return the matrix [1,0,-4; 0,1,4],
i.e.
2 3 4
1 1 0
�
!
1 0 �4
0 1 4
�
.
Partial pivoting: my_rref([0,6; -3,0]) should return the matrix
[1,0; 0,1]:
0 6
�3 0
�
!
1 0
0 1
�
.
Numerical error: my_rref([3,0,0,1; 0,3,0,1; 0,0,3,1; 1,1,1,1]):
2
664
3 0 0 1
0 3 0 1
0 0 3 1
1 1 1 1
3
775!
2
664
1 0 0 1/3
0 1 0 1/3
0 0 1 1/3
0 0 0 0
3
775
Free variables: my_rref([4,4,4,4; 2,2,4,4; 2,2,4,2]):
2
4
4 4 4 4
2 2 4 4
2 2 4 2
3
5!
2
4
1 1 0 0
0 0 1 0
0 0 0 1
3
5
You can also obtain a random m ⇥ n matrix with entries in {�1,
0, 1} by
calling A = randi([-1,1], m, n), then compare your result
my_rref(A) with
Matlab’s built-in rref(A).
4
Note: Solving linear systems (no points)
Once we have an RREF function, we can use it to solve linear
systems Ax = b
by computing the RREF of the augmented matrix [A | b]. For
simplicity, we
will assume that the system has a unique solution. In this case,
the RREF will
be of the form 2
64
1 x1
. . .
...
1 xn
3
75
and the solution vector is just its last column. This is easy to
implement in
Matlab (which is already provided to you as in solve.m):
function x = solve(A, b)
augmented_matrix = [A b];
R = my_rref(augmented_matrix);
x = R(:, end);
end
Test this function on some simple examples of linear systems
which you know to
have unique solutions.
4 GPS localization (30 points)
One real-life application of solving linear systems is GPS
localization. For
simplicity, let us work in a 2D world first. Suppose your
cellphone receives
signals from three GPS satellites at known positions A = (a1,
a2), B = (b1, b2),
and C = (c1, c2), and can measure its distances rA, rB, rC from
all of them, as
shown below.
1 2 4 5( , ) ( , )a a= =A
( , )x y
5r =A
1 2 5 2( , ) ( , )b b= = −B
5r =B
1 2 11 6( , ) ( , )c c= = −C
13r =C
This gives us three quadratic equations for our own position P =
(x, y):
r2A = (a1 � x)2 + (a2 � y)2, (1a)
r2B = (b1 � x)2 + (b2 � y)2, (1b)
r2C = (c1 � x)2 + (c2 � y)2. (1c)
5
Subtracting equation (1a) from equation (1b) and equation (1b)
from equa-
tion (1c), then simplifying, gives us a 2⇥ 2 linear system in x
and y:
r2B � r2A = 2(a1 � b1)x+ 2(a2 � b2)y � a21 � a22 + b21 +
b22, (2a)
r2C � r2B = 2(b1 � c1)x+ 2(b2 � c2)y � b21 � b22 + c21 +
c22. (2b)
Since this is a system of linear equations, it can be expressed in
matrix form,
⇤ ⇤
⇤ ⇤
�
x
y
�
=
⇤
⇤
�
, (3)
for some matrix and some vector on the right-hand side that you
should be able
to figure out. Your task in this part of the assignment is to
implement this
method in Matlab (in gps2D.m and gps3D.m). That is, given the
points A, B,
C and distances rA, rB, rC, you will have to:
1. construct the matrix and the right-hand side vector in (3)
corresponding
to the linear system (2a)-(2b),
2. pass this matrix and vector to the solve function (already
provided in
the zip file) to obtain the point P. Note: if you did not get your
my_rref
function to work, you can change it to the Matlab built-in rref
in the
solve function in solve.m.
Exactly the same approach also works in 3D, except we will
now need our
distances from four known points A = (a1, a2, a3), B = (b1, b2,
b3), C = (c1, c2, c3),
and D = (d1, d2, d3). Your second task is to work out the
analogous equations
to (2a)-(2b), and implement another program that works in 3D.
Specification:
function p = gps2d(a, b, c, ra, rb, rc)
Input: The 2D coordinates of three points A, B, C, and their
distances rA, rB,
rC from an unknown point P.
Output: The 2D coordinates of the point P.
function p = gps3d(a, b, c, d, ra, rb, rc, rd)
Input: The 3D coordinates of four points A, B, C, D, and their
distances rA, rB,
rC, rD from an unknown point P.
Output: The 3D coordinates of the point P.
Test cases:
gps2d([4;5], [5;-2], [-11;6], 5, 5, 13) should return the vector
[1;1].
gps3d([6;-3;3], [-1;-6;5], [-5;4;7], [6;8;4], 5, 7, 9, 9) should re-
turn [2;0;3].
In general, you can create a random 2D vector with entries
between say 0 and
10 by using a = 10*rand(2,1). Do this four times for a, b, c, and
p, and
set ra = norm(a - p) and so on. (The norm function returns the
length of a
vector.) Then check whether gps2d(a, b, c, ra, rb, rc) gives you
back p.
6
gps2d.m
%%
% Solve for the 2D coordinate of P as a problem of linear
system
%
% The parameters received are:
% - a (2 x 1): 2D coordinate of point A
% - b (2 x 1): 2D coordinate of point B
% - c (2 x 1): 2D coordinate of point C
% - ra (1 x 1): distance from A to P
% - rb (1 x 1): distance from B to P
% - rc (1 x 1): distance from C to P
%
% The function should return
% - p (2 x 1): 2D coordinate of P
function p = gps2d(a, b, c, ra, rb, rc)
%%%% YOUR CODE STARTS HERE
end
__MACOSX/._gps2d.m
gps3d.m
%%
% Solve for the 3D coordinate of P as a problem of linear
system
%
% The parameters received are:
% - a (3 x 1): 3D coordinate of point A
% - b (3 x 1): 3D coordinate of point B
% - c (3 x 1): 3D coordinate of point C
% - d (3 x 1): 3D coordinate of point D
% - ra (1 x 1): distance from A to P
% - rb (1 x 1): distance from B to P
% - rc (1 x 1): distance from C to P
% - rd (1 x 1): distance from D to P
%
% The function should return
% - p (3 x 1): 3D coordinate of P
function p = gps3d(a, b, c, d, ra, rb, rc, rd)
%%%% YOUR CODE STARTS HERE
end
__MACOSX/._gps3d.m
interchange.m
%%
% Swap row i and row j of a matrix
%
% The parameters received are:
% - A (m x n): a rectangular matrix
% - i (1 x 1)
% - j (1 x 1)
%
% The function should return
% - B (m x n): the resulting rectangular matrix
function B = interchange(A, i, j)
%%%% YOUR CODE STARTS HERE
end
__MACOSX/._interchange.m
my_rref.m
%%
% Computes the reduced row echelon form of a matrix
%
% The parameters received are:
% - A (m x n): a rectangular matrix
%
% The function should return
% - B (m x n): the reduce row echelon form of A
function B = my_rref(A)
%%%% YOUR CODE STARTS HERE
end
__MACOSX/._my_rref.m
replacement.m
%%
% Add the result of a scalar times row j to row i of a matrix
%
% The parameters received are:
% - A (m x n): a rectangular matrix
% - i (1 x 1)
% - j (1 x 1)
% - s (1 x 1): a scalar
%
% The function should return
% - B (m x n): the resulting rectangular matrix
function B = replacement(A, i, j, s)
%%%% YOUR CODE STARTS HERE
end
__MACOSX/._replacement.m
scaling.m
%%
% Multiply all entries in row i of a matrix by a scalar
%
% The parameters received are:
% - A (m x n): a rectangular matrix
% - i (1 x 1)
% - s (1 x 1): a scalar
%
% The function should return
% - B (m x n): the resulting rectangular matrix
function B = scaling(A, i, s)
%%%% YOUR CODE STARTS HERE
end
__MACOSX/._scaling.m
solve.m
%%
% Solve for the linear system Ax = b
%
% The parameters received are:
% - A (m x n): a rectangular matrix
% - b (m x 1): right hand side of a linear system
%
% The function should return
% - x (n x 1): solution vector [x_1, x_2, ..., x_n]
function x = solve(A, b)
augmented_matrix = [A b];
% you can change it to rref if your my_rref does not work
R = my_rref(augmented_matrix);
x = R(:, end);
end
__MACOSX/._solve.m

More Related Content

DOCX
B61301007 matlab documentation
PDF
Matlab booklet
PDF
Tutorial2
PPTX
Mechanical Engineering Homework Help
PDF
Dsp manual completed2
PPT
Matlab1
PPTX
Computation Assignment Help
PPTX
Matlab Functions for programming fundamentals
B61301007 matlab documentation
Matlab booklet
Tutorial2
Mechanical Engineering Homework Help
Dsp manual completed2
Matlab1
Computation Assignment Help
Matlab Functions for programming fundamentals

Similar to CSCI 2033 Elementary Computational Linear Algebra(Spring 20.docx (20)

PDF
Graph Algorithms, Sparse Algebra, and the GraphBLAS with Janice McMahon
PPTX
EPE821_Lecture3.pptx
PPTX
INTRODUCTION TO MATLAB presentation.pptx
PPT
Matlab introduction
PDF
Control system Lab record
PDF
Dsp lab _eec-652__vi_sem_18012013
PDF
Dsp lab _eec-652__vi_sem_18012013
PPT
Intro matlab and convolution islam
PDF
Alpine Spark Implementation - Technical
PDF
Multinomial Logistic Regression with Apache Spark
PDF
MATLAB Questions and Answers.pdf
PDF
Introduction to Data Science With R Lab Record
PDF
MATLAB
PPTX
MATLAB Workshop yugjjnhhasfhlhhlllhl.pptx
PPT
Matlab anilkumar
PDF
Quiz
PPTX
presentation.pptx
DOCX
SAMPLE QUESTIONExercise 1 Consider the functionf (x,C).docx
DOCX
SAMPLE QUESTIONExercise 1 Consider the functionf (x,C).docx
PPT
Matlab practical and lab session
Graph Algorithms, Sparse Algebra, and the GraphBLAS with Janice McMahon
EPE821_Lecture3.pptx
INTRODUCTION TO MATLAB presentation.pptx
Matlab introduction
Control system Lab record
Dsp lab _eec-652__vi_sem_18012013
Dsp lab _eec-652__vi_sem_18012013
Intro matlab and convolution islam
Alpine Spark Implementation - Technical
Multinomial Logistic Regression with Apache Spark
MATLAB Questions and Answers.pdf
Introduction to Data Science With R Lab Record
MATLAB
MATLAB Workshop yugjjnhhasfhlhhlllhl.pptx
Matlab anilkumar
Quiz
presentation.pptx
SAMPLE QUESTIONExercise 1 Consider the functionf (x,C).docx
SAMPLE QUESTIONExercise 1 Consider the functionf (x,C).docx
Matlab practical and lab session

More from mydrynan (20)

DOCX
CSIA 413 Cybersecurity Policy, Plans, and Programs.docx
DOCX
CSIS 100CSIS 100 - Discussion Board Topic #1One of the object.docx
DOCX
CSI Paper Grading Rubric- (worth a possible 100 points) .docx
DOCX
CSIA 413 Cybersecurity Policy, Plans, and ProgramsProject #4 IT .docx
DOCX
CSI 170 Week 3 AssingmentAssignment 1 Cyber Computer CrimeAss.docx
DOCX
CSE422 Section 002 – Computer Networking Fall 2018 Ho.docx
DOCX
CSCI  132  Practical  Unix  and  Programming   .docx
DOCX
CSCI 714 Software Project Planning and EstimationLec.docx
DOCX
CSCI 561Research Paper Topic Proposal and Outline Instructions.docx
DOCX
CSCI 561 DB Standardized Rubric50 PointsCriteriaLevels of .docx
DOCX
CryptographyLesson 10© Copyright 2012-2013 (ISC)², Inc. Al.docx
DOCX
CSCI 352 - Digital Forensics Assignment #1 Spring 2020 .docx
DOCX
CSCE 1040 Homework 2 For this assignment we are going to .docx
DOCX
CSCE509–Spring2019Assignment3updated01May19DU.docx
DOCX
CSCE 3110 Data Structures & Algorithms Summer 2019 1 of .docx
DOCX
CSCI 340 Final Group ProjectNatalie Warden, Arturo Gonzalez, R.docx
DOCX
CSC-321 Final Writing Assignment In this assignment, you .docx
DOCX
Cryptography is the application of algorithms to ensure the confiden.docx
DOCX
CSc3320 Assignment 6 Due on 24th April, 2013 Socket programming .docx
DOCX
Cryptography KeysCryptography provides confidentiality, inte.docx
CSIA 413 Cybersecurity Policy, Plans, and Programs.docx
CSIS 100CSIS 100 - Discussion Board Topic #1One of the object.docx
CSI Paper Grading Rubric- (worth a possible 100 points) .docx
CSIA 413 Cybersecurity Policy, Plans, and ProgramsProject #4 IT .docx
CSI 170 Week 3 AssingmentAssignment 1 Cyber Computer CrimeAss.docx
CSE422 Section 002 – Computer Networking Fall 2018 Ho.docx
CSCI  132  Practical  Unix  and  Programming   .docx
CSCI 714 Software Project Planning and EstimationLec.docx
CSCI 561Research Paper Topic Proposal and Outline Instructions.docx
CSCI 561 DB Standardized Rubric50 PointsCriteriaLevels of .docx
CryptographyLesson 10© Copyright 2012-2013 (ISC)², Inc. Al.docx
CSCI 352 - Digital Forensics Assignment #1 Spring 2020 .docx
CSCE 1040 Homework 2 For this assignment we are going to .docx
CSCE509–Spring2019Assignment3updated01May19DU.docx
CSCE 3110 Data Structures & Algorithms Summer 2019 1 of .docx
CSCI 340 Final Group ProjectNatalie Warden, Arturo Gonzalez, R.docx
CSC-321 Final Writing Assignment In this assignment, you .docx
Cryptography is the application of algorithms to ensure the confiden.docx
CSc3320 Assignment 6 Due on 24th April, 2013 Socket programming .docx
Cryptography KeysCryptography provides confidentiality, inte.docx

Recently uploaded (20)

PDF
O7-L3 Supply Chain Operations - ICLT Program
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PPTX
Lesson notes of climatology university.
PDF
VCE English Exam - Section C Student Revision Booklet
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
Anesthesia in Laparoscopic Surgery in India
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PPTX
Cell Structure & Organelles in detailed.
PDF
Pre independence Education in Inndia.pdf
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PDF
Microbial disease of the cardiovascular and lymphatic systems
PDF
Sports Quiz easy sports quiz sports quiz
O7-L3 Supply Chain Operations - ICLT Program
Renaissance Architecture: A Journey from Faith to Humanism
Lesson notes of climatology university.
VCE English Exam - Section C Student Revision Booklet
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
Anesthesia in Laparoscopic Surgery in India
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
STATICS OF THE RIGID BODIES Hibbelers.pdf
Cell Structure & Organelles in detailed.
Pre independence Education in Inndia.pdf
Supply Chain Operations Speaking Notes -ICLT Program
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
O5-L3 Freight Transport Ops (International) V1.pdf
human mycosis Human fungal infections are called human mycosis..pptx
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
Module 4: Burden of Disease Tutorial Slides S2 2025
Microbial disease of the cardiovascular and lymphatic systems
Sports Quiz easy sports quiz sports quiz

CSCI 2033 Elementary Computational Linear Algebra(Spring 20.docx

  • 1. CSCI 2033: Elementary Computational Linear Algebra (Spring 2020) Assignment 1 (100 points) Due date: February 21st, 2019 11:59pm In this assignment, you will implement Matlab functions to perform row operations, compute the RREF of a matrix, and use it to solve a real-world problem that involves linear algebra, namely GPS localization. For each function that you are asked to implement, you will need to complete the corresponding .m file with the same name that is already provided to you in the zip file. In the end, you will zip up all your complete .m files and upload the zip file to the assignment submission page on Gradescope. In this and future assignments, you may not use any of Matlab’s built-in linear algebra functionality like rref, inv, or the linear solve function Ab, except where explicitly permitted. However, you may use the high-level array manipulation syntax like A(i,:) and [A,B]. See “Accessing Multiple Elements” and “Concatenating Matrices” in the Matlab documentation for more informa- tion. However, you are allowed to call a function you have
  • 2. implemented in this assignment to use in the implementation of other functions for this assignment. Note on plagiarism A submission with any indication of plagiarism will be directly reported to University. Copying others’ solutions or letting another person copy your solutions will be penalized equally. Protect your code! 1 Submission Guidelines You will submit a zip file that contains the following .m files to Gradescope. Your filename must be in this format: Firstname Lastname ID hw1 sol.zip (please replace the name and ID accordingly). Failing to do so may result in points lost. • interchange.m • scaling.m • replacement.m • my_rref.m • gps2d.m • gps3d.m • solve.m 1
  • 3. Ricardo Ricardo Ricardo Ricardo � The code should be stand-alone. No credit will be given if the function does not comply with the expected input and output. Late submission policy: 25% o↵ up to 24 hours late; 50% o↵ up to 48 hours late; No point for more than 48 hours late.
  • 4. 2 Elementary row operations (30 points) As this may be your first experience with serious programming in Matlab, we will ease into it by first writing some simple functions that perform the elementary row operations on a matrix: interchange, scaling, and replacement. In this exercise, complete the following files: function B = interchange(A, i, j) Input: a rectangular matrix A and two integers i and j. Output: the matrix resulting from swapping rows i and j, i.e. performing the row operation Ri $ Rj . function B = scaling(A, i, s) Input: a rectangular matrix A, an integer i, and a scalar s. Output: the matrix resulting from multiplying all entries in row i by s, i.e. per- forming the row operation Ri sRi. function B = replacement(A, i, j, s) Input: a rectangular matrix A, two integers i and j, and a scalar s. Output: the matrix resulting from adding s times row j to row i, i.e. performing the row operation Ri Ri + sRj . Implementation tips: In each of these functions, you should check that the input indices i and j are of rows in the
  • 5. matrix (which may not be the same as the number of columns!). If any index is out of range, print an error using the built-in function disp, and return the original matrix. This could help you diagnose problems when you write your RREF function in the next part. Testing: Test your code on rectangular m⇥n matrices of various sizes, both when m < n and when m > n. You can generate some simple matrices for testing on using the functions eye(m,n), ones(m,n), and randi(10,m,n). 2 3 RREF (40 points) Next, you will use these row operations to write a function that performs Gauss- Jordan elimination and compute the reduced row echelon form of any matrix. We will call the function my_rref, because the rref function already exists in Matlab. Specification: function R = my_rref(A) Input: a rectangular matrix A. Output: the reduced row echelon form of A.
  • 6. For full credit, your function should handle the following: • Partial pivoting: At each step, you should swap the current row with the one whose entry in the pivot column has the largest absolute value. • Free variables: Due to numerical error, the entries in a column corre- sponding to a free variable may be extremely small but not precisely zero. Therefore, you should consider an entry to be zero if its absolute value is smaller than 10�12. We suggest first implementing the algorithm without considering these two issues, then adding code to deal with them one at a time. Implementation tips: There are two di↵ erent ways one can implement Gauss-Jordan elimination. • In Section 1.2 under “The Row Reduction Algorithm”, the book describes it in two phases: first do Gaussian elimination (Steps 1-4), then perform row operations equivalent to back-substitution (Step 5). • Gauss-Jordan elimination can also be done in a single phase: every time you find a pivot, perform scaling so the pivot entry becomes 1, then perform elimination on all the other rows, both above and below the pivot row.
  • 7. You may use either approach in your implementation. Below, we provide pseudocode for the latter approach. In either case, since we want to be able to handle free variables, the pivot entry won’t necessarily be on the di- agonal. Instead, you’ll need to keep track of both the pivot row, say k, and the pivot column, l, as you go along; see the illustration on the right. 3 Algorithm 1 RREF 1: initialize pivot row k = 1, pivot column l = 1 3: among rows k to m, find the row p with the biggest* entry in column l 4: Rk $ Rp 5: if akl is zero** then 6: l l + 1 7: else 8: Rk (1/akl)Rk 9: for i = 1, . . . , k � 1, k + 1, . . . ,m do 10: Ri Ri � (ail/akl)Rk 11: end for 12: k k + 1, l l + 1 13: end if 14: end while
  • 8. *Here “biggest” means having the largest absolute value (abs in Matlab). **Consider a number to be zero if its absolute value is smaller than 10�12. Test cases: my_rref([2,3,4; 1,1,0]) should return the matrix [1,0,-4; 0,1,4], i.e. 2 3 4 1 1 0 � ! 1 0 �4 0 1 4 � . Partial pivoting: my_rref([0,6; -3,0]) should return the matrix [1,0; 0,1]: 0 6 �3 0 � ! 1 0 0 1
  • 9. � . Numerical error: my_rref([3,0,0,1; 0,3,0,1; 0,0,3,1; 1,1,1,1]): 2 664 3 0 0 1 0 3 0 1 0 0 3 1 1 1 1 1 3 775! 2 664 1 0 0 1/3 0 1 0 1/3 0 0 1 1/3 0 0 0 0 3 775 Free variables: my_rref([4,4,4,4; 2,2,4,4; 2,2,4,2]): 2 4 4 4 4 4 2 2 4 4
  • 10. 2 2 4 2 3 5! 2 4 1 1 0 0 0 0 1 0 0 0 0 1 3 5 You can also obtain a random m ⇥ n matrix with entries in {�1, 0, 1} by calling A = randi([-1,1], m, n), then compare your result my_rref(A) with Matlab’s built-in rref(A). 4 Note: Solving linear systems (no points) Once we have an RREF function, we can use it to solve linear systems Ax = b by computing the RREF of the augmented matrix [A | b]. For simplicity, we will assume that the system has a unique solution. In this case, the RREF will be of the form 2
  • 11. 64 1 x1 . . . ... 1 xn 3 75 and the solution vector is just its last column. This is easy to implement in Matlab (which is already provided to you as in solve.m): function x = solve(A, b) augmented_matrix = [A b]; R = my_rref(augmented_matrix); x = R(:, end); end Test this function on some simple examples of linear systems which you know to have unique solutions. 4 GPS localization (30 points) One real-life application of solving linear systems is GPS localization. For simplicity, let us work in a 2D world first. Suppose your cellphone receives signals from three GPS satellites at known positions A = (a1, a2), B = (b1, b2),
  • 12. and C = (c1, c2), and can measure its distances rA, rB, rC from all of them, as shown below. 1 2 4 5( , ) ( , )a a= =A ( , )x y 5r =A 1 2 5 2( , ) ( , )b b= = −B 5r =B 1 2 11 6( , ) ( , )c c= = −C 13r =C This gives us three quadratic equations for our own position P = (x, y): r2A = (a1 � x)2 + (a2 � y)2, (1a) r2B = (b1 � x)2 + (b2 � y)2, (1b) r2C = (c1 � x)2 + (c2 � y)2. (1c) 5 Subtracting equation (1a) from equation (1b) and equation (1b) from equa- tion (1c), then simplifying, gives us a 2⇥ 2 linear system in x and y: r2B � r2A = 2(a1 � b1)x+ 2(a2 � b2)y � a21 � a22 + b21 + b22, (2a) r2C � r2B = 2(b1 � c1)x+ 2(b2 � c2)y � b21 � b22 + c21 +
  • 13. c22. (2b) Since this is a system of linear equations, it can be expressed in matrix form, ⇤ ⇤ ⇤ ⇤ � x y � = ⇤ ⇤ � , (3) for some matrix and some vector on the right-hand side that you should be able to figure out. Your task in this part of the assignment is to implement this method in Matlab (in gps2D.m and gps3D.m). That is, given the points A, B, C and distances rA, rB, rC, you will have to: 1. construct the matrix and the right-hand side vector in (3) corresponding to the linear system (2a)-(2b), 2. pass this matrix and vector to the solve function (already provided in
  • 14. the zip file) to obtain the point P. Note: if you did not get your my_rref function to work, you can change it to the Matlab built-in rref in the solve function in solve.m. Exactly the same approach also works in 3D, except we will now need our distances from four known points A = (a1, a2, a3), B = (b1, b2, b3), C = (c1, c2, c3), and D = (d1, d2, d3). Your second task is to work out the analogous equations to (2a)-(2b), and implement another program that works in 3D. Specification: function p = gps2d(a, b, c, ra, rb, rc) Input: The 2D coordinates of three points A, B, C, and their distances rA, rB, rC from an unknown point P. Output: The 2D coordinates of the point P. function p = gps3d(a, b, c, d, ra, rb, rc, rd) Input: The 3D coordinates of four points A, B, C, D, and their distances rA, rB, rC, rD from an unknown point P. Output: The 3D coordinates of the point P. Test cases: gps2d([4;5], [5;-2], [-11;6], 5, 5, 13) should return the vector [1;1]. gps3d([6;-3;3], [-1;-6;5], [-5;4;7], [6;8;4], 5, 7, 9, 9) should re- turn [2;0;3].
  • 15. In general, you can create a random 2D vector with entries between say 0 and 10 by using a = 10*rand(2,1). Do this four times for a, b, c, and p, and set ra = norm(a - p) and so on. (The norm function returns the length of a vector.) Then check whether gps2d(a, b, c, ra, rb, rc) gives you back p. 6 gps2d.m %% % Solve for the 2D coordinate of P as a problem of linear system % % The parameters received are: % - a (2 x 1): 2D coordinate of point A % - b (2 x 1): 2D coordinate of point B % - c (2 x 1): 2D coordinate of point C % - ra (1 x 1): distance from A to P % - rb (1 x 1): distance from B to P % - rc (1 x 1): distance from C to P % % The function should return % - p (2 x 1): 2D coordinate of P function p = gps2d(a, b, c, ra, rb, rc) %%%% YOUR CODE STARTS HERE end
  • 16. __MACOSX/._gps2d.m gps3d.m %% % Solve for the 3D coordinate of P as a problem of linear system % % The parameters received are: % - a (3 x 1): 3D coordinate of point A % - b (3 x 1): 3D coordinate of point B % - c (3 x 1): 3D coordinate of point C % - d (3 x 1): 3D coordinate of point D % - ra (1 x 1): distance from A to P % - rb (1 x 1): distance from B to P % - rc (1 x 1): distance from C to P % - rd (1 x 1): distance from D to P % % The function should return % - p (3 x 1): 3D coordinate of P function p = gps3d(a, b, c, d, ra, rb, rc, rd) %%%% YOUR CODE STARTS HERE end __MACOSX/._gps3d.m interchange.m %% % Swap row i and row j of a matrix % % The parameters received are: % - A (m x n): a rectangular matrix % - i (1 x 1)
  • 17. % - j (1 x 1) % % The function should return % - B (m x n): the resulting rectangular matrix function B = interchange(A, i, j) %%%% YOUR CODE STARTS HERE end __MACOSX/._interchange.m my_rref.m %% % Computes the reduced row echelon form of a matrix % % The parameters received are: % - A (m x n): a rectangular matrix % % The function should return % - B (m x n): the reduce row echelon form of A function B = my_rref(A) %%%% YOUR CODE STARTS HERE end __MACOSX/._my_rref.m
  • 18. replacement.m %% % Add the result of a scalar times row j to row i of a matrix % % The parameters received are: % - A (m x n): a rectangular matrix % - i (1 x 1) % - j (1 x 1) % - s (1 x 1): a scalar % % The function should return % - B (m x n): the resulting rectangular matrix function B = replacement(A, i, j, s) %%%% YOUR CODE STARTS HERE end __MACOSX/._replacement.m scaling.m %% % Multiply all entries in row i of a matrix by a scalar % % The parameters received are: % - A (m x n): a rectangular matrix % - i (1 x 1) % - s (1 x 1): a scalar % % The function should return % - B (m x n): the resulting rectangular matrix function B = scaling(A, i, s) %%%% YOUR CODE STARTS HERE
  • 19. end __MACOSX/._scaling.m solve.m %% % Solve for the linear system Ax = b % % The parameters received are: % - A (m x n): a rectangular matrix % - b (m x 1): right hand side of a linear system % % The function should return % - x (n x 1): solution vector [x_1, x_2, ..., x_n] function x = solve(A, b) augmented_matrix = [A b]; % you can change it to rref if your my_rref does not work R = my_rref(augmented_matrix); x = R(:, end); end __MACOSX/._solve.m