Question: Write a function that will accept a matrix and return a matrix. The function should take the passed matrix and using Gauss Elimination put zeros

Write a function that will accept a matrix and return a matrix. The function should take the passed matrix and using Gauss Elimination put zeros in the first column of the matrix. The first step is to put the row with the largest absolute value in the first row by swapping rows using functions you have already created. Then calculate the factor necessary to multiply the first row by so that when it is added to the second row, the element in row 2 column 1 becomes zero. Use your linear combination function to accomplish this task. Then move down to the next row. Keep looping through the rows until all the elements in column 1, except row 1, are zero. The function should work with any size matrixnot necessarily square. Please use variable names that make sense and easy to follow with comments.

For example, if the matrix

[ 1 9 4 5 ; -11 -2 3 0 ; 0 -6 5 4 ; 3 5 7 -3]

is passed to the function, the function would return

[ -11 -2 3 0 ; 0 8.82 4.27 5 ; 0 -6 5 4 ; 0 4.45 7.82 -3]

Code:

matrixGiven=[1 9 4 5;-11 -2 3 0; 0 -6 5 4; 3 5 7 -3] newMatrix=GaussElimination(matrixGiven)

Functions I used:

function matrix=GaussElimination(matrix) % performs gauss elimination on the first column newMatrix=LargestNumber(matrix,1,1) end

function matrix=LargestNumber(matrix,rowGiven,col) % scan a matrix to find the largest absolute value in a given row % arguments are the matrix, the row to scan and the column to scan [rows,cols]=size(matrix); for row=rowGiven:rows-1 matrix(row,col)=sqrt(matrix(row,col)^2); if matrix(row,col)>matrix(rowGiven,col) largest=matrix(row,col); rowWithLarge=row; end end nextMatrix=Swap(matrix,1,rowWithLarge) end

function matrix=Swap(matrix,row_1,row_2) % swaps two rows in a matrix [rows,cols]=size(matrix) tempMatrix=matrix(row_1,:); matrix(row_1,:)=matrix(row_2,:); matrix(row_2,:)=tempMatrix; for row=2:rows for col=1:cols scalar=-matrix(1,col)/matrix(row,col) nextMatrix=TwoRowCombo(matrix,1,row,scalar) end end end

function matrix = TwoRowCombo(matrix,rowNum_1,rowNum_2,scalar) % multiplies specified row in a matrix by a constant and added by another row % arguments are a matrix, first row number, second row number and constant [rows,cols]=size(matrix); for col=1:cols matrix(rowNum_2,col)=scalar*(matrix(rowNum_1,col)+matrix(rowNum_2,col)); % adds the two row given and multpilies the changed row by the given scalar number end end

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!