Pivoting a matrix using largest magnitude coefficent and diagonal values

8 ビュー (過去 30 日間)
Braeden McKeown
Braeden McKeown 2021 年 4 月 4 日
I have a question for an assignment asking this
c. Write a function in MATLAB (rowpivot.m) that performs pivoting on a matrix (determines the location of the largest magnitude coefficient at or below the specified diagonal value and swaps the rows as necessary). The function will work for any location along the diagonal 1 through n). This function will call rowswap.m written in part (a) as necessary. Verify that the function works by pivoting on the original matrix in part (a) for the second element along the diagonal.
I did the other parts but i am alittle confused on what to do with this. The code I have for it is below
function Matrix_Pivot=rowpivot(Matrix,y)
D=diag(Matrix);
[a,b]=max(D(y,:));
Matrix_Pivot=rowswap(D,b,1);
end
would this be the correct way to get the diagonal value and swap the required rows.
Rest of the code is below if that helps
%this is the main script of the program
%Sets the matrix paramaters
Matrix=[1 7 -4 3 -3; 0 3 1 -2 2;0 4 3 4 5; 0 -5 8 1 3];
%Commands for rowswap
fprintf('The following matrix has had its rows swapped\n');
Matrix_Swap=rowswap(Matrix,2,3);
disp(Matrix_Swap);
%commands for row reduction
fprintf('The following matrix has had its rows reduxed\n')
Matrix_Redux=rowredux(Matrix,3,2,4/3);
disp(Matrix_Redux)
%Commands for row pivot
fprintf('The following matrix has had its rows pivoted\n')
Matrix_Pivot=rowpivot(Matrix,2);
disp(Matrix_Pivot)
rowswap
function Matrix_Swap=rowswap(Matrix,a,b)
Matrix_Swap=Matrix;
Matrix_Swap(a,:)=Matrix(b,:);
Matrix_Swap(b,:)=Matrix(a,:);
end
rowredux
function Matrix_Redux=rowredux(Matrix,c,d,e)
Matrix_Redux=Matrix;
Matrix_Redux(c,:)=Matrix(c,:)-Matrix(d,:)*e;
end

回答 (0 件)

カテゴリ

Help Center および File ExchangeOperating on Diagonal Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by