Efficient factorization with eigenvalues
4 次查看(过去 30 天)
显示 更早的评论
I've been trying to solve this problem for a while with no luck:
Let Ax=b a linear system of order 18, where A is a symmetric, tridiagonal matrix with all elements equal to 6 on the main diagonal and to 3 on the superior and inferior codiagonals and b has linearly spaced elements in [5,8].
(This part I understand and I've solved it. It's the second part I have problems with.)
Compute the eigenvalues of A and, based on their properties, solve the linear system Ax=b by the solution of two triangular systems, using the most computationally efficient factorization of A.
This is what I thought the solution could look like but it doesn't give me the correct result.
A = zeros(18,18);
a = 6*ones(18,1);
A = A + diag(a);
b = 3*ones(17,1);
A = A + diag(b,1) + diag (b,-1);
b = linspace(5,8, 18)';
L = chol(A, 'lower');
R = chol(A, 'upper');
xl = L\(L'\b);
xu = R\(R'\b);
I'm thankful for help and ideas to improve the solution.
0 个评论
回答(2 个)
John D'Errico
2017-6-25
编辑:John D'Errico
2017-6-25
Well, did you try it? If you did not compute the eigenvalues as directed, why not? Seriously, COMPUTE THE EIGENVALUES OF A!!!!! Try it!
What will those eigenvalues tell you? Remember that A is a symmetric matrix. Is there some factorization of A that might make things easy? In fact, is there some factorization implied by the question?
The question is there to make you think. Time to start thinking. If we tell you anymore than this, then we might as well do your homework for you.
=====================================
Edit: Ok, with some attempt made, and what appears to beat least some credible effort, i'll take a look.
First, be really careful. First, you used b as a diagonal of A. Then you used b as the vector on the right hand side of A*x=b. BAD IDEA.
MATLAB does not charge you more if you use another variable name! What will happen if you travel down the road you are taking is your code will be unreadable. It will be buggy as hell.
A= diag(repmat(6,1,18)) + diag(repmat(3,1,17),-1) + diag(repmat(3,1,17),1);
So A is clearly symmetric.
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
The eigenvalues of A are all real and positive. (Symmetry makes them real.) So A is symmetric positive definite. It fits the requirements for a Cholesky decomposition.
b = linspace(5,8, 18)';
Be just a little careful there too. The transpose operator (') computes a conjugate transpose. Since you just want to convert a row vector into a column vector here and b is real, you can use ' operator, but safer is to use the .' operator. Safer because that makes things explicit. Sloppy code often works, but it is just that, a bit sloppy. And sloppy code is easier to contain less than obvious bugs.
b = linspace(5,8,18).';
Ok. So we have A and b. We need to solve the linear system A*x=b. And apparently the rules are we cannot simply use backslash on A. Homework is so picky.
What does a cholesky factorization mean? Thus, if we form
R = chol(A);
for positive definite A, what does that mean??????? If you are not sure, then read the help for chol!
"If A is positive definite, then
R = chol(A) produces an upper triangular R so that R'*R = A."
So we could write the problem as
(R'*R)*x = b
What does that mean? How can we interpret it? Matrix multiplication is associative. So we can also write it like this:
R'*(R*x) = b
where R*x is a vector. So you can break it into two subproblems to be solved.
R'*u = b
Then once you have u, solve for x.
R*x = u
Both of them can be solved using backslash of course. Or, you can write it in one line, using backslash twice.
x = R\(R'\b);
Personally, I feel the former solution is better, because it makes things a bit more explicit, a bit more clear. Until you get to the point where you can follow multiple operations done in one line of code, split things apart!
When you are done, verify the result.
[x,A\b]
Do both columns yield the same numbers? (Don't subtract them and expect an exact zero though. Perhaps that may be your problem.)
0 个评论
Burak Özpoyraz
2022-8-17
A = diag(6 * ones(1, 18)) + diag(3 * ones(1, 17), 1) + diag(3 * ones(1, 17), -1);
b = linspace(5, 8, 18).';
R = chol(A);
Rt = R.';
inf_sol_vec = Rt \ b; % Lower triangular system
sup_sol_vec = R \ inf_sol_vec; % Upper triangular system
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!