Group # Student Name Person # Lab Section
EAS 230 Spring 2017 - Programming Project
Score: / 100
1. This project must be done in groups of two students each. The partners of each group are
randomly selected by the instructors of the course. You are not allowed to change your
partner for any reason or under any circumstances. You can nd your partner name, email
and your group number on UBLearns.
2. Each group must submit one full report and one set of m- les. Every individual partner of
each group must submit a maximum of one page report showing his role in the project and
his partner’s role and how they collaborated together.
3. All your m- les (scripts and/or functions) must be named as required and described in this
document.
4. All m- les must be uploaded as one zip le named EAS230SP17PP group# to UBlearns before
11:59 PM on Sunday, April 30, 2017 (please do not include les ending in .m~, .sav or .mat
in your zip le).
5. Your report should be written in a professional way and subdivided into sections such as
executive summary, introduction, analysis, results and discussion and a conclusion. It must
include a print-out of your script/function les, the results as displayed in MATLAB command
window and any gures as required in this document. Script. les can be copied from MATLAB
editor and pasted in color to your document le.
6. All print-outs must be stapled to this document and turned-in at the start of your rst lecture
of the week of Monday, May 1, 2017 (Monday May 1 or Tuesday May 2, depending on your
section). Be sure to write your name, UB Person # and lab section on every page.
7. As a group, you must write your own function/script. les and follow all instructions to get full
credit. You are prohibited to use scripts found on the Internet or MATLAB built-in functions
unless speci cally mentioned or for testing your code as described in this document. A grade
of zero will be assigned to any copied code/document.
8. You must use good programming practices, including indentation, commenting your func-
tion/script. les and using meaningful variable names to make your codes self-documenting.
9. It is your responsibility to make sure that your code works properly and is free of errors by
utilizing the resources at your disposal.
10. Figures must be properly titled and labeled on both the x-axis and the y-axis. If the gure
contains multiple lines on the same axis, you must add a legend and use di erent markers,
line styles and colors as needed for the plot to be neat.
EAS 230 Spring 2017 - Programming Project Page 1
Group # Student Name Person # Lab Section
Background: Systems of linear algebraic equations have many applications in engineering dis-
ciplines such as mechanical, electrical, chemical, and civil engineering. These systems range from
two equations to hundreds or thousands of equations. A system of two to three equations can be
easily solved by hand, however, as the number of equations increases the problem becomes more
di cult and time consuming and the need for a numerical technique becomes urgent. One of the
main objectives of EAS 230 Engineering Computations is to develop numerical techniques to solve
linear systems of equations. MATLAB can be a helpful tool for this purpose as well. For this
project, assume you are on MATLAB’s developer team and you must develop a function for solving
a linear system of equations using a new numerical iterative technique.
Deliverables: You will perform. several calculations in MATLAB. You will create several gures.
You will produce a report, in narrative form, discussing the problems as described below. All gures
should be embedded in your report. You should include gure numbers and gure captions. You
will be required to submit the following documents in UBLearns:
• report/write-up: report group#.pdf
• the following matlab les
{ m- le/function (axb1 group#.m)
{ m- le/function (axb2 group#.m)
{ m- le/script. (tol group#.m)
{ m- le/script. (reactor1 group#.m)
{ m- le/script. (reactor2 group#.m)
{ m- le/script. (truss1 group#.m)
{ m- le/script. (truss2 group#.m)
Problems:
1. (60 points) Iterative technique to solve the system of equations Develop an
iterative technique to solve the system of equations (1)
a11x1 + a12x2 + ::: + a1nxn = b1
a21x1 + a22x2 + ::: + a2nxn = b2
... ... ... ... ...
an1x1 + an2x2 + ::: + annxn = bn
A~x =~b (1)
EAS 230 Spring 2017 - Programming Project Page 2
Group # Student Name Person # Lab Section
A =
2
66
64
a11 a12 ::: a1n
a21 a22 ::: a2n
... ... ... ...
an1 an2 ::: ann
3
77
75 and ~x =
2
66
64
x1
x2
...
xn
3
77
75 and ~b =
2
66
64
b1
b2
...
bn
3
77
75
First rewrite the system of equations in an explicit form. as:
x1 = (b1 a12x2 a13x3 ::: a1nxn)=a11
x2 = (b2 a21x1 a23x3 ::: a2nxn)=a22
... ...
xn = (bn an1x1 an2x2 ::: ann 1xn 1)=ann
(2)
An initial value is assumed for each of the unknowns x(0)1 ;x(0)2 ;x(0)3 ;:::;x(0)n . Where the number
0 in the superscript. of x(0)n is the iteration number. For the 0th iteration, initialize to zeros:
x(0)i = 0, for i = 1;2;:::;n. Substitute these values into the right hand side the of the explicit
form. (Eq. 2) to obtain the rst approximation, x(1)1 ;x(1)2 ;x(1)3 ;:::;x(1)n . This accomplishes one
iteration. In the same way, the second approximation x(2)1 ;x(2)2 ;x(2)3 ;:::;x(2)n is computed by
substituting the rst approximation values into the right hand side of the rewritten equations.
The general form. of Equations 2 is:
x(k)i = 1a
ii
2
66
4
nX
j=1
j6=i
aijx(k 1)j
+bi
3
77
5 for i = 1;2;3;:::;n (3)
where k is the iteration number. The kth estimate of the solution is determined from the
(k 1)th estimate. The iterations continue until the di erences between the values that are
obtained in successive iterations are small. The iterations can be stopped when the following
error estimate is smaller than some tolerance
k~x(k)k k~x(k 1)k
k~x(k 1)k
< (4)
where
~x(k)
=
vu
ut nX
i=1
x(k)i
2
INSTRUCTIONS
EAS 230 Spring 2017 - Programming Project Page 3
Group # Student Name Person # Lab Section
(a) (30 points) Write a function named axb1 group#.m that takes the matrix of coe cients
A and the vector~b as input and returns the solution vector ~x as an output. The function
should have three input arguments: 1) the matrix of coe cients A, 2) the vector of right-
hand-sides ~b and 3) the tolerance . The function should have two output arguments:
1) the solution ~x and 2) the number of iterations k.
(b) (10 points) Equation 4 does not represent the only choice for de ning an error. The
original equation A~x = ~b is not solved exactly by any of the iterates ~x(k). That is,
A~x(k) 6= ~b. De ne a residual error vector ~r(k) which represents the di erence between
left-hand side A~x(k) and the right-hand side~b
A~x(k) ~b = ~r(k) (5)
Finally, we enforce the tolerance based on this residual error:
~r(k)
< (6)
where
~r(k)
=
vu
ut nX
i=1
r(k)i
2
Write a MATLAB function named axb2 group#.m that uses the error de ned in Equa-
tion 6 to solve the system A~x =~b. The function should have three input arguments: 1)
the matrix of coe cients A, 2) the vector of right-hand-sides ~b and 3) the tolerance .
The function should have two output arguments: 1) the solution ~x and 2) the number
of iterations k. (Hint: Use axb1 group#.m as a template and change the de nition of
the error.)
(c) (10 points) A Numerical solution can be obtained by this iterative technique only for
square system of equations where the matrix A is a non-singular matrix of dimensions
n n and the column vector ~b is n 1. Accordingly, your function must check for the
square-ness and non-singularity of matrixA. For the previous two functions axb1 group#
and axb2 group#, do the following:
• If matrix A is rectangular, the function must throw an error message \Error: The
input matrix must be a square matrix." and the program must stop. This
can be done by using the MATLAB built-in function error().
• Your function must check the singularity of matrix A using MATLAB built-in func-
tion det() and if the matrix A is singular, the function must throw an error message
\Error: This system may have no solution or infinitely many solutions.
This function cannot be used to solve this system." and the program must
stop.
• Furthermore, the iterative numerical technique shown previously is stable and con-
verged only for diagonally dominant matrices A, where, a square matrix A is said
to be diagonally dominant if for every row of the matrix,
jaiij
nX
j=1
j6=i
jaijj; for i = 1;2;:::;n
EAS 230 Spring 2017 - Programming Project Page 4
Group # Student Name Person # Lab Section
where aij denotes the entry in the ith row and jth column. Accordingly if matrix A
is not diagonally dominant, the function must throw a warning message, \Warning:
Matrix A is not diagonally dominant. The solution obtained by this
function may be incorrect."
• In addition, your functions should set the maximum number of iterations to 1000. If
this number has been exceeded, the function must stop and through a warning mes-
sage, \Warning: The system has not converged in the allowable number
of iterations."
• Test your functions for all of the above functionality using the Sample Systems
on the last page of this document.
(d) (10 points) Write a MATLAB script. named tol group#.m that solves the following
system using the two methods above (parts 1a and 1b) for the values of listed below.