Matlab: Solving linear system with QR/Householder

In summary, There are two main methods for solving linear systems in Matlab: QR decomposition and Householder transformation. QR decomposition breaks down a matrix into an orthogonal matrix and an upper triangular matrix, while Householder transformation transforms a matrix into a tridiagonal matrix. To use the QR method, you can use the built-in function "qr" and then solve the system using the backslash operator. The Householder method can also be used for systems with complex numbers in Matlab, and the accuracy of the solution can be checked using the "norm" function. Besides QR and Householder, Matlab also offers other methods like LU, Cholesky, and SVD decomposition, each with its own advantages and suitable for different problems. It is important to
  • #1
wu_weidong
32
0

Homework Statement


Hi all,
I'm trying to implement the QR method for solving the linear system Ax = b. The QR factorization is achieved using Householder method.

The Attempt at a Solution


The main function is

Code:
function x = lin_solve(A,b)
[R,v] = householder(A);
y = Qt_times_b(v,b);
x = R\y;

Here are the individual functions:

Code:
function [R,v] = householder(A)

[m,n] = size(A);

if m>=n,
    NumberOfReflections = n;
else
    NumberOfReflections = m-1;
end

R = A;

v = cell(NumberOfReflections,1);

for k = 1:NumberOfReflections,
    x = R(k:m,k);

    xnorm = norm(x);

    if xnorm>0,
        % Compute the normal vector of the reflector
        v{k} = -x;
        v{k}(1) = v{k}(1) - sign(x(1))*xnorm;
        v{k} = (sqrt(2)/norm(v{k}))*v{k};

	% Update R
        for j = k:NumberOfReflections,
		R(k:m,j) = R(k:m,j) - (v{k}'*R(k:m,j))*v{k};
	end
    else
        v{k} = zeros(m-k+1,1);
    end
end

Code:
function [Q,y] = Qt_times_b(v,b)

NumberOfReflections = length(v);
y = b;
mv = length(v{1});
Q=eye(mv);
for k = 1:NumberOfReflections,
   F = eye(mv-k+1)-2*(v{k}*v{k}')/norm(v{k})^2;
   Qk = [eye(k-1) zeros(k-1,mv-k+1);zeros(mv-k+1,k-1) F];
   y = Qk*y;
end

What I don't understand is the function works for square matrices, i.e. A that are n-by-n, but if m < n, or m > n, then I get the incorrect solution x.

I tried to check my R and Qk against the qr factorization function provided by matlab. Say [QR, RR] = qr(A).

If m < n, then my R is not equal to RR; if m > n, R = RR. In both cases, Q' = QR, where Q' = transpose of matrix Q.

What is wrong with my program?

Thank you.

Regards,
Rayne
 
Physics news on Phys.org
  • #2


Hi Rayne,

It seems like your main issue is with the dimensions of your matrices. In your lin_solve function, you are using the backslash operator to solve for x, which is only valid for square matrices. If m > n, then R is not square and the backslash operator will not work. You can try using the pseudoinverse function, pinv, instead. Also, make sure to check the dimensions of your matrices at each step to ensure they are compatible for the operations you are performing.

Another issue could be with your implementation of the householder function. Make sure to follow the steps correctly and check for any errors in your code. You can also try debugging by using a simpler example and comparing your results to the hand calculations.

Hope this helps! Good luck with your implementation.
 
  • #3


Your code looks correct, but there may be some issues with how you are calling the functions or how you are setting up your matrices. Make sure that your input matrix A is a full rank matrix, otherwise the QR factorization may not work correctly. Also, check your indexing when updating R in the householder function, as it may be causing some issues when m < n. Another possibility is that you are not transposing the Q matrix correctly in the Qt_times_b function. It may be helpful to compare your code with examples or other implementations to see if there are any differences. Good luck with your implementation!
 

1. What is the difference between solving a linear system with QR and Householder in Matlab?

Both QR and Householder methods are used to solve linear systems in Matlab. The main difference is that QR decomposition breaks down a matrix into an orthogonal matrix and an upper triangular matrix, while Householder transformation transforms a matrix into a tridiagonal matrix. Both methods are efficient and accurate, but the choice between them depends on the specific problem at hand.

2. How do I implement the QR method to solve a linear system in Matlab?

To use the QR method in Matlab, you can use the built-in function "qr". This function takes in a matrix as input and returns the orthogonal matrix Q and the upper triangular matrix R. You can then use these matrices to solve the linear system using the backslash operator (\) in Matlab. For example, if you have a system Ax = b, you can solve it by writing x = Q \ (R\b).

3. Can I use the Householder method to solve a linear system with complex numbers in Matlab?

Yes, the Householder method can be used to solve linear systems with complex numbers in Matlab. The "hess" function in Matlab can be used to compute the Householder transformation of a complex matrix, and the "hessqr" function can be used to solve the linear system using the transformed matrix.

4. How can I check the accuracy of my solution when using the QR or Householder method in Matlab?

In Matlab, you can use the "norm" function to calculate the norm of a vector or matrix. To check the accuracy of your solution, you can calculate the norm of the residual vector (b-Ax) and compare it to a small tolerance value. If the norm is within the tolerance, then your solution is accurate enough.

5. Are there any other methods in Matlab that can be used to solve linear systems besides QR and Householder?

Yes, Matlab offers several other methods for solving linear systems, such as LU decomposition, Cholesky decomposition, and SVD decomposition. Each method has its own advantages and is suitable for different types of problems. It is important to choose the appropriate method based on the characteristics of your problem to obtain an accurate and efficient solution.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
126
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
570
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
997
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
  • Advanced Physics Homework Help
Replies
3
Views
503
  • Advanced Physics Homework Help
Replies
6
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
18
Views
3K
Back
Top