In: Advanced Math
2. (Solving linear systems)
Consider the linear system Ax = b
with A =[14 9 14 6 -10;-11 -11 5 8 6;15 -2 -14 8 -15;14 13 11 -3 -7;0 9 13 5 -14], and . b = [-4;8;6;0;10].
a) Verify that the linear system has a unique solution. Hint: use rref, rank, det, or any other Matlab method. Briefly explain the answer please.
You'll now solve Ax = b in three different ways. Store the three different solutions in four different variables (x1,x2,x3,x4, say), we need to compare them in parts c and d.
b1) Using rref, determine the solution x of the system Ax = b, and store it in variable x1. Hint: x1 is the final column of the reduced row echelon form of M = (A|b). Remember horzcat.
b2) Using Matlab's linsolve method, find a solution to the system Ax = b. Store the solution in x2.
b3) Using Matlab's inverse method (^(-1) or inv), solve the system as x = A^(-1)b, store the result in x3.
b4) Use the rref method from class to find an inverse matrix B of A. Use that to calculate x = Bb. Store the result in x4.
c) Compare the solutions x1, x2, x3,x4. Do any two of them agree? Does that contradict a) or not?
d) Try ranking the solutions by quality. To do so, compute Ax-b, for the various values of x, and see which x produces the smallest difference between Ax and b.
%%Matlab code for solving linear system
clear all
close all
%A matrix
A = [14 9 14 6 -10;-11 -11 5 8 6;15 -2 -14 8 -15;14 13 11 -3 -7;0 9
13 5 -14];
b = [-4;8;6;0;10];
%determinant of A
dt=det(A);
fprintf('Determinant of A=%f\n',dt)
fprintf('As determinant of A is nonzero hence linear system has
unique solution.\n')
%Augmented matrix
Ag=A;
Ag(:,6)=b;
%Solution using rref
rA=rref(Ag);
fprintf('rref of Ag=\n')
disp(vpa(rA,3))
fprintf('Solution using rref is \n')
disp(rA(:,end))
x1=squeeze((rA(:,end)));
%Solution using linsolve
lA=linsolve(A,b);
fprintf('Solution using linsolve is \n')
disp(lA)
x2=squeeze(lA);
%Solution using inverse
inA=inv(A)*b;
fprintf('Solution using inverse is \n')
disp(inA)
x3=inA;
%Solution using rref inverse
I=eye(5,5);
Arr=[A I];
bb=rref(Arr);
B=squeeze(bb(:,6:end));
fprintf('Solution using rref inverse is \n')
disp(B*b)
x4=B*b;
fprintf('\tError in rref solution =%e\n',norm(A*x1-b));
fprintf('\tError in linsolve solution =%e\n',norm(A*x2-b));
fprintf('\tError in inverse solution =%e\n',norm(A*x3-b));
fprintf('\tError in rref inverse solution =%e\n',norm(A*x4-b));
%%%%%%%%%%%%%%%%%%% End of Code %%%%%%%%%%%%%%%%