To use MATLAB as an aid to implement GaussJordan elimination

To use MATLAB as an aid to implement Gauss-Jordan elimination. To gain experience in writing MATLAB script m-files. Write a MATLAB script m-file that applies Gauss-Jordan elimination to A = [A b] with A = [-1 2 -1 1 3 -6 3 -3 1 0 2 2 1 2 3 5] b = [-5 8 -6 2] for the purpose of solving the linear system of equations Ax = b. Download the MATLAB script m-file gauss_jordan elimination_example.m from the Presentations area in Blackboard and use it as a guide to get started. Rename the file as \'exercise2.m\' and update the comment header to include your name. Implement the row operations necessary to transform [A] = [A B] into [R] = [R f] in reduced row echelon form. Have your script compare your final reduced row echelon form to that produced by the MATLAB command. Contents of gauss_jordan_elimination_example.m: A=[-1 3 1 1;2 -6 0 2;-1 3 2 3;1 -3 2 5] b=[-5;8;-6;2] Ab=[A b]

Solution

function [x,err]=gauss_jordan_elim(A,b)A = [-1 3 1 1;2 -6 0 2;-1 3 2 3;1 -3 2 5] % input for augmented matrix Ab = [-5 ; 8; -6;2] % intput for matrix B [n,m]=size(A); % finding the size of matrix A err =0; % calculation of error x=zeros(n,1); % calling fuction zeroif n ~= m disp(\'error: n~=m\'); % displaying error if found err = 1;end % end of the scope of if if length(b) ~= n % finding the legth of matrix B disp(\'error: wrong size of b\'); % displaying error, if found err = 2;else if size(b,2) ~= 1 b=b\'; end % end of the scope of if-else if size(b,2) ~= 1 disp(\'error: b is a matrix\'); % displaying erron in matrix B err = 3; endendif err == 0 Aa=[A,b]; for i=1:n [Aa(i:n,i:n+1),err]=gauss_pivot(Aa(i:n,i:n+1)); if err == 0 Aa(1:n,i:n+1)=gauss_jordan_step(Aa(1:n,i:n+1),i); end end x=Aa(:,n+1);endA=0;function A1=gauss_jordan_step(A,i) % calling of fuction function [n,m]=size(A); % determination of size of matrix A A1=A; % assigning A to A1 s=A1(i,1);A1(i,:) = A(i,:)/s;k=[[1:i-1],[i+1:n]];for j=k s=A1(j,1); A1(j,:)=A1(j,:)-A1(i,:)*s;end % end of for loop function [A1,err]=gauss_pivot(A) % calling of fucntion [n,m]=size(A); % finding the size of matrix AA1=A; % process of assigning err = 0; % error flagif A1(1,1) == 0 check = logical(1); % logical(1) - TRUE i = 1; while check i = i + 1; if i > n disp(\'error: matrix is singular\'); err = 1; check = logical(0); else if A(i,1) ~= 0 & check check = logical(0); b=A1(i,:); % process to change row 1 to i A1(i,:)=A1(1,:); A1(1,:)=b; end end endend

 To use MATLAB as an aid to implement Gauss-Jordan elimination. To gain experience in writing MATLAB script m-files. Write a MATLAB script m-file that applies G

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site