MATLAB: Problem with factorization of a matrix in a linear system

MATLABmatrix

I just started working with MatLab and i cannot resolve this problem: Ax=b is a linear system with A as a matrix 18×18 symmetric 3-diagonal with all the elements equal to 6 on the principal diagonal and equal to 3 on the superior and inferior co-diagonals and b=linspace(5,8,18). You have to calculate eigenvalues of A. Considering the properties of these eigenvalues, solve Ax=b using 2 triangular systems and using the most efficient factorization of A. After that, sum the vector which is the solution of the superior triangular system with the other one (the solution of the inferior triangular system). Do the norm (1) of the result. Sorry for bad English. The correct answer is: 4.4944e+01 . Here is my solution but doesn't work:
A=zeros(18,18)+diag(6*ones(1,18),0)+diag(3*ones(1,17),1)+diag(3*ones(1,17),-1)
b=linspace(5,8,18)'
eigenv=eig(A)
x1=b\triu(A)
x2=b\tril(A)
x=x1+x2
n=norm(x,1)

Best Answer

Argh. Well, you did make an effort. But this is for what appears to be a basic numerical linear algebra class. So you need to start thinking about linear algebra.
To start ... Why do you think you need to add diagonal matrices to a zeros array? Just add them to each other.
A = diag(6*ones(1,18),0) + diag(3*ones(1,17),1) + diag(3*ones(1,17),-1);
Next, learn to use semi-colons on the end of your lines, unless there is some reason to inspect the result. Otherwise, you see tons of useless crap on the command line.
b=linspace(5,8,18)';
So, A is a tridiagonal matrix. Is it full rank? Are the eigenvalues positive? Is it symmetric? Yes. Yes. Yes.
rank(A)
ans =
18
isequal(A,A.')
ans =
logical
1
eig(A)
ans =
0.081832
0.3251
0.72316
1.2652
1.9363
2.7183
3.5898
4.5271
5.5045
6.4955
7.4729
8.4102
9.2817
10.064
10.735
11.277
11.675
11.918
So A is SPD (symmetric, positive, definite.)
Next, why would you possibly think that triu(A) and tril(A) are factorizations of A? Or, even that b\tril(A) is how you solve the indicated linear system? That is close, but the wrong use of backslash. So you need to read the help for slash again. Read what it says about backslash carefully.
What does the characterization of A (above) tell you will be the most efficient factorization of A?