Answer the question as asked. Code is MATLAB. The 2 code examples mentioned in t
ID: 3119165 • Letter: A
Question
Answer the question as asked. Code is MATLAB. The 2 code examples mentioned in the question are below.
%VeryNaiveGaussJordanElimination.m
% This code uses an inefficient naive Gauss-Jordan elimination solve AX = B
% It presumes no row switching is required and the solution is unique
[m,n] = size(A);
if (rank(A) ~= n)
'matrix A is invalid for this code'
return
end
Aug = [A B];
for col_j = 1:n %For each column where there will be a leading 1
%Create leading 1 in row i, col_j: Row i = Row i/a(i,i)
row_i = col_j; %True for this type of coefficient matrix only
Aug(row_i,:) = Aug(row_i,:)/Aug(row_i,col_j);
%Create zeros above and below leading 1: Row ii = - Row ii (ii,j) Row i + Row ii
for row_ii = 1:m
if row_ii ~= row_i
Aug(row_ii,:) = -Aug(row_ii, col_j)*Aug(row_i,:) + Aug(row_ii,:);
end
end
end
X = Aug(:,n+1)
……………………………………………………………………………………………………………
NEW CODE
Ngauussel.m
function ngaussel(A,b)
% Solve the system Ax=b using naive gaussian elimination
n=length(b);
x=zeros(n,1);
fprintf(' ');
disp(' The augmented matrix is')
augm =[A b]
for k=1:n-1
for i=k+1:n
m=A(i,k)/A(k,k);
for j=k+1:n
A(i,j)=A(i,j)-m*A(k,j);
end
A(i,k)=m;
b(i)=b(i)-m*b(k);
end
end
x(n)=b(n)/A(n,n);
for i=n-1:-1:1
S=b(i);
for j=i+1:n
S=S-A(i,j)*x(j);
end
x(i)=S/A(i,i);
end
% Print the results
fprintf(' ');
disp(' The transformed upper triangular augmented matrix C is =')
fprintf(' ');
for i=1:n
for j=1:n
if (j<i) A(i,j)=0; end
end
end
C=[A b]
fprintf(' ');
disp(' Back substitution gives the vector solution')
x
Question 4: Naive Elimination 16 points a Very Naive Gauss JordanElimination.m is inefficient because it performs unnecessary operations. For example, defining cols (col j+1): (n +1), and replacing Line 16 with the two lines of code Aug row i,cols) /Aug row i,col j) Aug (row i r cols) Aug (row i r col j results in the same new row i that is calculated using col j fewer divisions. Provide two lines of code to replace Line 21 that similarly reduce the unnecessary operations performed at that line Here you can assume that cols has already been defined. b) VeryNaiveGaussJordanElimination.m can easily be modified such that it implements Gaussian Elimination i.e. forward only by replacing Lines 19-23 with the code for row ii row i 1 m. Aug row i, Aug row ii, Aug (row ii Aug (row ii Col J end Complete the code below such that it implements the matrix form of Back Substitution discussed in class on the matrix Aug that results from the Gaussian Elimination above for col j col row l for ii Aug row ii col j *Auq row i Aug (row ii, Aug (row ii, end end c Most Gaussian Elimination codes do not create leading 1s during the forward step. Instead they divide the row by the pivot value in the Back Substitution step. State the Line number of the textbook's ngaussel.m where the division occurs d) Many Back Substitution codes do not use the approach we did in class, in which one variable is eliminated from all-but-one equation at each step. Instead they eliminate all-but-one variable from one equation at each step. (i) Fill in the blanks: This alternative approach is the same as going row by row, from the to the State creating zeros where (ii) Lines 20 24 in the textbook's ngaussel.m calculate x(i) using a for loop. Write a 24 of GaussNaivem produces the identical calculation for x(i) as in (ii) using multiplication of two matrices instead of the inner for loop. Defining this matrix product as CD, state the sizes of both matrices, and clearly indicate what are the elements cij and dijExplanation / Answer
Gauss-Jordan Method MATLAB Program
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
function [x,err]=gauss_jordan_elim(A,b)
A = [1 1 1;2 3 5; 4 0 5] % input for augmented matrix A
b = [5 ; 8; 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 zero
if 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;
end
end
if 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);
end
A=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 A
A1=A; % process of assigning
err = 0; % error flag
if 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
end
end
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
function [x,err]=gauss_jordan_elim(A,b)
A = [1 1 1;2 3 5; 4 0 5] % input for augmented matrix A
b = [5 ; 8; 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 zero
if 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;
end
end
if 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);
end
A=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 A
A1=A; % process of assigning
err = 0; % error flag
if 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
end
end
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.