Computational Project 2
For this project you will write a Matlab program that solves Ax = b using
the Jacobi iterative method for a few different linear systems that are defined
in part 5. Below are the required steps to take towards building this program.
I suggest first reading the entire assignment all the way through and then
drawing a “code map” that helps you think about how all the different
functions are interacting. Important formatting requirements are provided at
1.Write a function that generates an n × n matrix A and an n × 1 vector b.
The function inputs should be n and m, and the outputs should be A
and b, where the latter are calculated within the function as
A = magic(n) + eye(n) * m;
(magic generates a matrix—a rather special one. What relevant impact
will the addition of m*eye(n) have? Recall eye(n) generates an n × n
b = [1/1, 1/2, 1/3, . . . , 1/n]
where T indicates the transpose of this vector.
2.Write a function to evaluate the condition number and diagonal dominance
of the matrix A. It should take as input the matrix A and output a
status variable. The status variable being equal to 0 means that A is
well conditioned and diagonally dominant. If this is not the case, the
function should return a non-zero value and display the reason (illconditioned or not diagonally dominant). Within the function, use
cond(A) to check that the condition number isn’t too large. Use
equation 4.52 from the textbook (also in the notes) to determine if all
rows are diagonally dominant.
3.Write a function to solve Ax = b using the Jacobi iterative method. The
function should take in the following as inputs:
• a matrix A of size n × n
• a vector b of size n × 1
• a variable kmax that specifies the maximum number of iterations.
The outputs of the function should be the solution vector x. The
function should iterate until either kmax is reached or the total
estimated error summed across all elements is less than a relative error
tolerance εt = 1E-10 (whichever is first), where relative error is
k − xi
Use the fprintf command to print the error at each iteration to the command
window. The function should only iterate until a converged solution has
been found. This happens when the relative error et is less than εt. If this
happens before kmax iterations are performed, no more iterations should be
performed. The function should also display a warning message that the
system did not converge if a solution is not found within kmax iterations. In
this case, return an answer of x = [−999].
4. Write a main function with the following features:
• Inputs are n, m, and kmax.
• Output is x.
• Call the function from (1) to build A and b.
• Call the function from (2) to check A.
• If the system is well conditioned and solvable iteratively, call the function
from part (3) to find x. Otherwise, display an error message and set x =
5. Write a function (called cp2_MEID) that calls the function from part (4)
with the following values:
(a) n=5, m=1E+2, kmax =10
(b) n=5, m=1E+2, kmax =50
(c) n=5, m=1E+6, kmax =50
(d) n=20, m=1, kmax =50
(e) n=20, m=0, kmax =50
Formatting: Your main function should be called cp2_MEID and have no
inputs or outputs (just hard code input parameters n, m, and kmax at the top
of cp2_MEID). Publish as a pdf and print this function to turn in a hard copy
Also print out your final outputs (suppress the outputs for each iteration
where needed) for each case in part 5 and a brief write-up explaining any
cases where the iterative approach failed. Submit your Matlab file to the
dropbox on D2L. Please label parts of your code with the corresponding
section number above and make sure to comment throughout. Remember to
submit only one m file to D2L that contains your main function and all
other local ones. Please do not compress your file.