MATLAB - Gaussian Elimination with Partial Pivoting

Click For Summary
SUMMARY

This discussion focuses on solving the linear system Ax = b using Gaussian Elimination with Partial Pivoting in MATLAB. The matrix A is constructed with elements aij = -1 for i > j, aii = ain = 1, and zero elsewhere, generated using the functions tril, eye, and indexing. The random vector b is created using the rand function. The solution involves LU factorization, where A is decomposed into L and U matrices, followed by solving the system using back substitution.

PREREQUISITES
  • Understanding of Gaussian Elimination and Partial Pivoting
  • Familiarity with MATLAB programming, specifically matrix operations
  • Knowledge of matrix decomposition techniques, particularly LU factorization
  • Basic understanding of random vector generation in MATLAB using the rand function
NEXT STEPS
  • Learn how to implement LU factorization in MATLAB using the lu function
  • Explore advanced matrix manipulation techniques in MATLAB, including indexing and matrix addition
  • Study the implications of partial pivoting on numerical stability in Gaussian elimination
  • Investigate the performance of different methods for solving linear systems in MATLAB
USEFUL FOR

Students and professionals in computer science, particularly those studying numerical methods, linear algebra, and MATLAB programming. This discussion is beneficial for anyone looking to enhance their skills in solving linear systems using MATLAB.

azdang
Messages
80
Reaction score
0

Homework Statement


Let A be the nxn matrix with elements aij = -1 for i > j, aii = ain = 1, 1 <= i <= n, and zero everywhere else. Let b be a random vector. Solve the linear system Ax = b by Gaussian Elimination with partial pivoting.

Use tril, eye, and ones to generate A,
Generate b with rand.
No loops


Homework Equations





The Attempt at a Solution


I'm not sure if this is the correct place to post this, but I figured someone in Computer Science might know MATLAB and be able to help. For one, I am HORRIBLE with MATLAB. I never seem to be able to do it exactly the way my teacher expects it. First off, I'm having trouble generating A. So far, I've used G = eye(n,n) to get a matrix with 1's on the diagonal, and C = tril(-ones(n,n),-1) to get a matrix with -1's where I think they should be. However, I still need a way to get 1's in the last column. Also, I'm not sure how to put these three matrixes together in MATLAB to construct A.

Another thing, I know for G.E.P.P. we need to factor A = PLU. I am assuming for this particular matrix, those will be obvious? Maybe I am wrong. Then, I guess I just have to solve Ly = PTb and Ux = y.

I'm really just not good with MATLAB, as this is the first class I've ever had to use it for. I've tried reading up on it, and although I generally get the desired outcome, my method never seems to be correct. It would be greatly appreciated if someone could give me some guidance on this problem. Thank you so much.
 
Physics news on Phys.org
I doubt this question is still active, but I'll post a reply for posterity at least!
Let A be the nxn matrix with elements aij = -1 for i > j, aii = ain = 1, 1 <= i <= n, and zero everywhere else. Let b be a random vector. Solve the linear system Ax = b by Gaussian Elimination with partial pivoting.
Conceptually, to construct A you want to create several different matrices with nonzero values in different locations, and then add the matrices together. If the nonzero values are in different locations then this has the effect of combining all of the information about where the nonzeros are into one matrix. Here is how to generate each of those pieces:
  • aij = -1 for i > j is a matrix with -1's below the main diagonal. You can generate this matrix by first generating a matrix of all -1's with -1*ones(n). Then, use TRIL to make that matrix lower triangular, e.g. tril(-1*ones(n),-1).
  • aii = ain = 1 is a matrix with ones along the main diagonal as well as in the final column. You can generate the identity matrix easily with eye(n), and then you can add the final column of ones with indexing.
Finally, a function that accepts the matrix order N as an input and generates A using these pieces is:

[CODE lang="matlab" title="Generate A"]function A = generateA(N)
A1 = tril(-1*ones(N),-1);
A2 = eye(n);
A2(:,end) = 1;
A = A1+A2;
end
[/CODE]

Some sample output from this function is
[CODE title="Example Output"]>> generateA(4)

ans =

1 0 0 1
-1 1 0 1
-1 -1 1 1
-1 -1 -1 1[/CODE]

Next, generating b as a random vector is a straightforward use of the RAND function:
[CODE lang="matlab" title="Generate b"]b = rand(n,1);[/CODE]

Finally, solving using Gaussian elimination with partial pivoting is simply a matter of performing LU factorization:
[CODE title="Solve A*x=b"][L,U] = lu(A);
x = U\(L\b)[/CODE]

You can check the results by comparing against x = A\b.
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
34K
Replies
1
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
28K
Replies
2
Views
1K
  • · Replies 14 ·
Replies
14
Views
3K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K