I have an assignment which requires me to write a general function that can handle any number of unknowns for Gauss Elimination. I have already written a code but I am wondering how do I write it in a way where if the first element of the first row and column is zero to carry on with the pivoting but if it's not a 0 then continue with the Gauss Elimination? Below is my code:
function[x,y,z] = Question2(a,b)
C=[a b]; %Augmented Matrix
if C(1,:)==0
C([1 3],:)=C([3 1],:); %Pivoting R1 with R3
else
%Upper Triangular Matrix
C(2,:) = C(2,:) - (C(2,1)*C(1,:)/C(1,1));
C(3,:)=C(3,:) - (C(3,1)*C(1,:)/C(1,1));
C(3,:)=C(3,:) - (C(3,2)*C(2,:)/C(2,2));
%Back Substitution
z=C(3,4)/C(3,3);
y=(C(2,4)-z*C(2,3))/C(2,2);
x=(C(1,4)-z*C(1,3)-y*C(1,2))/C(1,1);
end
end

2 commentaires

Walter Roberson
Walter Roberson le 2 Oct 2022
Note that you talk about "any number" but you do not test before assuming that the matrix has 3 rows or 4 columns.
Clarissa Chen
Clarissa Chen le 2 Oct 2022
ah sorry my bad, let me just attach my entire question here.

Connectez-vous pour commenter.

 Réponse acceptée

Walter Roberson
Walter Roberson le 2 Oct 2022

0 votes

If you
[row, col, pv] = find(C,1);
then if row is empty then the entire C matrix is 0; otherwise pv would be the pivot value and C(row,col) is the location of the pivot value.
Then, if row is not the same as the iteration number but col is the same as the iteration number, then exchange that row with the row matching the iteration number. If row is not the same as the iteration number but col is greater than the iteration number, the implication is that you have a column that has become zero, and you have to do the appropriate thing for that case... possibly exchange columns.

6 commentaires

Clarissa Chen
Clarissa Chen le 3 Oct 2022
Can I just manually change the rows myself? I'm not sure if that is allowed which is why I'm trying to find a way to write a code for the if else. Also, what does
find(C,1)
mean? Does it help me find out if the first element of the 1st row and column equals to 0? So sorry for not understanding.
No, you cannot just change the rows yourself: you are required to make a general-purpose elimination routine that will handle any number of rows and columns.
find(C,1)
searches through the array C in linear indexing order looking for the first entry which is not 0. It is effectively an abbreviation for
find(C~=0, 1)
except that find( C) would locate NaN but C~=0 would not locate nan.
The ,1 part means to find the first 1 match.
MATLAB stores arrays "down" columns. So for example
1 2
3 4
5 6
would be stored in memory in the order [1 3 5 2 4 6] - finish all of the first column, then all of the second column is after that, then all of the third column, and so on. And find(C,1) asks where the first non-zero is.
So for example,
C = [
0 0 21 18
0 0 5 2
0 7 2 11
]
then [row, col, pv] = find(C,1) would scan down the first column and see no non-zero values, then it would scan down the second column and see that the two entries are 0 and keep going. Then it would find the 7 is non-zero and it would keep an internal record of where it found the entry -- at the 6th (linear) entry in memory. Then it would look at the count of how many values to return, and see that it has accumulated all 1 locations so it would stop searching. At that point it would take the record of locations and check how many outputs are expected, and see that three outputs are expected. It would know that it then had to convert the linear index (6) to row and column, outputing row 3 column 2 in this case, and with the third output also being requested it would return the value there (7) into pv . This would tell you that the pivot value is 7 and that it was found at row 3, column 2. If you were doing the second elimination iteration at the time you would be focused on row 2, so you would know you had to exchange row 2 and row 3, giving you
C = [
0 0 21 18
0 7 2 11
0 0 5 2
]
and that would have the pivot in the expected location for the second iteration.
In practice, you would be expecting that after the first iteration, C(1,1) would be 1, so you would probably be wanting to find(C(n:end, n:end), 1) for iteration n, and adding (n-1) to the row and column numbers returned.
Clarissa Chen
Clarissa Chen le 3 Oct 2022
Thank you so much, I sort of understand now. I will try and implement this in my code. Thank you once again!
Walter Roberson
Walter Roberson le 3 Oct 2022
Each time you do a row change, you need to keep a record of what you swapped, of which row each row was originally. You can use the same row exchange logic ROWRECORD([newrow oldrow],:) = ROWRECORD([oldrow newrow], :) that you use for the pivot itself . After you finish the eliminations, you will need the record of locations to figure out what order to return the x values in.
When you do a column exchange you might need to keep a record of the column order as well.
Clarissa Chen
Clarissa Chen le 3 Oct 2022
Modifié(e) : Clarissa Chen le 3 Oct 2022
Oh, I see. I understand all of these but how would I write the code such that if C(1,1) is zero, pivoting needs to happen and how would I write the code to pivot it? Do I use an if else? For example
if C(1,1)=0
find(C,1)
then how would I swap the column which returns a non-zero value to the top row?
Suppose that n is your iteration number. Then under the assumption that you have already rearraged C for the iterations before n, then
[row, col, pv] = find(C(n:end, n:end), 1);
row = row + n - 1; col = col + n - 1;
Now row and col are the locations in C where the next pivot value is to be found -- examining only the rows and columns "below and to the right" of where you are currently examining.
Then examine col: if col is not equal to n, then there are no non-zeros in the current column, and you need to do a column exchange between column n and column col to move the first column to the right with non-zero entries over to the column you are paying attention to. After that, if row is not equal to n, then exchange rows n and row . Once those column and row exchanges are done, then the pivot value will be at row n column n.
(Make sure you test that row and column are not empty -- the case where there are no more non-zero values "below and right" in the array.)

Connectez-vous pour commenter.

Plus de réponses (0)

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by