Complete the functions myElimBelow and myElimAbove which are called by the funct
ID: 3184920 • Letter: C
Question
Complete the functions myElimBelow and myElimAbove which are called by the function myInv (given). myInv takes a square, invertible matrix and returns its inverse ure The procedure for calculating the inverse is as follows 1. Augment the matrix A (the example above is shown here) with an identity matrix of the same size 5.774 17.1187 4.6171 31.70991 0 0 0 74.313270.6046 9.7132 95.0222 0 1 0 0 9.22273.1833 82.3458 3.44460 01 0 5.5478 27.6923 69.4829 43.87440 0 0 1 2. Perform the Forward sweep on this augmented matrix. This will be accomplished my myElimBelow. 75.774 71187 4.6171 31.70991 0 53.8160 5.1850 63.92360.98071 0 0 0 80.5029-6.2251 -0.621 0.1055 0 0 6.1084-0.13460.3236-0.7981 1 3. Perform a modified forward sweep again, this time eliminating everything above the diagonal. This will be accomplished by myE1imAbove. 5.7740 0 1.5905 0.2929 47941.9000 0.4764 4.4010 8.3398-10.5306 0 80.50290-0.7582 -0.2243 0.1867 1.0191 0 53.8160 0 0 6.1084-0.1346-0.3236-0.7981 Divide each row of the augmented matrix by the corresponding diagonal element A(ii) 4. 1 00 0 0.0210 0.0039 0.0195-0.0251 01 0 0 0.0089 0.0818 0.1550-0.1957 0010|-0.0094-0.0028 0.0023 0.0127 0 0 0 -0.0220-0.0530-0.1307 0.1637 5. The augmented portion (columns n+1 through 2"n) is the inverse of A.
Explanation / Answer
matlab code
close all
clear
clc
A = [75.774 17.1187 4.6171 31.7099;
74.3132 70.6046 9.7132 95.0222;
39.2227 3.1833 82.3458 3.4446;
65.5478 27.6923 69.4829 43.8744];
disp('A');
for i=1:size(A,1)
fprintf('%10.5f %10.5f %10.5f %10.5f ',A(i,:));
end
disp(' ');
[~,A_inv] = myInv(A);
disp('A Inverse');
for i=1:size(A,1)
fprintf('%10.5f %10.5f %10.5f %10.5f ',A_inv(i,:));
end
% Function for calculating inverse of a matrix
function [A2,I2] = myInv(A)
I = eye(size(A,1));
[A1,I1] = myElimBelow(A,I);
[A2,I2] = myElimAbove(A1,I1);
for i=1:size(A2,1)
c = A2(i,i);
A2(i,:) = A2(i,:)/c;
I2(i,:) = I2(i,:)/c;
end
end
% Function for obtaining an Upper triangular matrix
function [AA,II] = myElimBelow(A,I)
AA = A;
II = I;
c = 2;
while 1
for i=c:size(A,1)
a = AA(c-1,c-1);
b = AA(i,c-1);
AA(i,:) = AA(i,:)*a-AA(c-1,:)*b;
II(i,:) = II(i,:)*a-II(c-1,:)*b;
end
if c < size(A,1)
c = c+1;
else
break;
end
end
end
% Function for obtaining an Lower triangular matrix
function [AA,II] = myElimAbove(A,I)
AA = A;
II = I;
c = 1;
while 1
for i=1:c
a = AA(c+1,c+1);
b = AA(i,c+1);
AA(i,:) = AA(i,:)*a-AA(c+1,:)*b;
II(i,:) = II(i,:)*a-II(c+1,:)*b;
end
if c < size(A,1)-1
c = c+1;
else
break;
end
end
end
output
A
75.77400 17.11870 4.61710 31.70990
74.31320 70.60460 9.71320 95.02220
39.22270 3.18330 82.34580 3.44460
65.54780 27.69230 69.48290 43.87440
A Inverse
0.02099 0.00386 0.01952 -0.02507
0.00885 0.08178 0.15497 -0.19568
-0.00942 -0.00279 0.00232 0.01266
-0.02203 -0.05298 -0.13065 0.16371
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.