# MATLAB - Gaussian Elimination with Partial Pivoting

• azdang
In summary, Gaussian Elimination with Partial Pivoting is a numerical method used in MATLAB to solve systems of linear equations. It involves eliminating variables systematically and using the largest coefficient as the pivot element. It is different from other methods and has advantages such as handling large systems and producing accurate results. However, it may be computationally expensive for very large systems and may not work efficiently for ill-conditioned equations. To implement it in MATLAB, you can use the "lu" function or write your own code using matrix operations and for loops. There are many online resources available for assistance with this method.
azdang

## 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

## 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.

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:

Generate A:
function A = generateA(N)
A1 = tril(-1*ones(N),-1);
A2 = eye(n);
A2(:,end) = 1;
A = A1+A2;
end

Some sample output from this function is
Example Output:
>> generateA(4)

ans =

1     0     0     1
-1     1     0     1
-1    -1     1     1
-1    -1    -1     1

Next, generating b as a random vector is a straightforward use of the RAND function:
Generate b:
b = rand(n,1);

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

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

## 1. What is Gaussian Elimination with Partial Pivoting in MATLAB?

Gaussian Elimination with Partial Pivoting is a numerical method used to solve systems of linear equations in MATLAB. It involves systematically eliminating variables from the equations until only one variable remains, which can then be solved for. Partial pivoting means that the rows of the equations are rearranged to ensure that the largest coefficient is used as the pivot element for each elimination step.

## 2. How is Gaussian Elimination with Partial Pivoting different from other methods?

Gaussian Elimination with Partial Pivoting is different from other methods, such as LU decomposition or Jacobi iteration, because it is specifically designed for solving linear systems of equations. It is also more efficient and accurate than other methods, making it a popular choice for solving large systems of equations.

## 3. What are the advantages of using Gaussian Elimination with Partial Pivoting in MATLAB?

One major advantage of using Gaussian Elimination with Partial Pivoting in MATLAB is that it can handle large systems of equations with ease. It also produces more accurate results compared to other methods, making it a reliable choice for scientific and engineering applications. In addition, the use of partial pivoting helps to reduce the impact of rounding errors and improves numerical stability.

## 4. Are there any limitations of using Gaussian Elimination with Partial Pivoting in MATLAB?

While Gaussian Elimination with Partial Pivoting is a powerful method for solving linear systems of equations, it does have some limitations. It can be computationally expensive for very large systems, and it may not work efficiently if the system of equations is ill-conditioned (meaning the equations are sensitive to small changes in the input data).

## 5. How can I implement Gaussian Elimination with Partial Pivoting in MATLAB?

To implement Gaussian Elimination with Partial Pivoting in MATLAB, you can use the built-in "lu" function, which performs LU decomposition with partial pivoting. Alternatively, you can write your own code using MATLAB's matrix operations and for loops to carry out the elimination steps. There are also many online resources and tutorials available to help you understand and implement this method in MATLAB.

• Calculus and Beyond Homework Help
Replies
1
Views
679
• Engineering and Comp Sci Homework Help
Replies
3
Views
899
• Engineering and Comp Sci Homework Help
Replies
1
Views
2K
• Engineering and Comp Sci Homework Help
Replies
1
Views
2K
• Engineering and Comp Sci Homework Help
Replies
1
Views
33K
• MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
• Engineering and Comp Sci Homework Help
Replies
2
Views
2K
• Calculus and Beyond Homework Help
Replies
4
Views
1K
• Engineering and Comp Sci Homework Help
Replies
1
Views
28K
• Engineering and Comp Sci Homework Help
Replies
3
Views
2K