Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

MATLAB: Write a function named get_soln_Cramer()that finds the solution of a lin

ID: 3573106 • Letter: M

Question

MATLAB:

Write a function named get_soln_Cramer()that finds the solution of a linear system of equations AX=B using Cramer’s rule, which takes (as inputs) a square matrix A of size n and a column vector b of size n×1 and returns (as output) the solution vector x of size n×1. To do this, your function must call your function get_det_LU() to obtain all of the necessary determinants.

System 2: 9 -4 -2 01 TX1 24 16 17 -6 3 x 34 12 6 x3 32 0 -3 -6 11] LX4 18 where x1,x2, x3 and x4 are unknown variables.

Explanation / Answer

function get_soln_Cramer(m)
disp(' ');
A = rand(m,m);
b = rand(m,1);
disp('Timing backslash:');
tic
x = A; % Backslash operator for solving A*x = b
toc
xc = cramersrule(A,b);
disp(['norm of backslash residual = '
num2str(norm(A*x-b),4)]);
disp(['norm of Cramer''s Rule residual = '
num2str(norm(A*xc-b),4)]);
disp(' ');
return
end

function x = cramersrule(A,b) % Demo of Cramer's Rule for
solving A*x = b
disp('Timing Cramer''s Rule:');
tic
[m n] = size(b);
z = zeros(m,1);
Ai = A;
for k=1:m
    Ai(:,k) = b;
    z(k) = det(Ai);
    Ai(:,k) = A(:,k);
end
detA = det(A);
x = z / detA;
toc
disp(['Max abs(det(Ai)) = ' num2str(max(abs(z)),4)]);
disp(['abs(det(A)) = ' num2str(abs(detA),4)]);
return
end