18.3: Using Cramer’s rule to solve Ax=b
( \newcommand{\kernel}{\mathrm{null}\,}\)
Let Ax=b be a system of n linear equations in n variables such that |A|≠0. The system has a unique solution given by:
x1=|A1||A|,x2=|A2||A|,…,xn=|An||A|
where Ai is the matrix obtained by replacing column i of A with b. The following function generates Ai by replacing the ith column of A with b:
Create a new function called cramersRule
, which takes A and b and returns x using the Cramer’s rule.
Note: Use numpy
and NOT mydet
to find the required determinants. mydet
is too slow.
Test your cramersRule
function on the following system of linear equations:
x1+2x2=3
3x1+x2=−1
Verify the above answer by using the np.linalg.solve
function:
Test your cramersRule
function on the following system of linear equations and verify the answer by using the np.linalg.solve
function:
x1+2x2+x3=9
x1+3x2−x3=4
x1+4x2−x3=7
Cramer’s rule is a O(n!) algorithm and the Gauss-Jordan (or Gaussian) elimination is O(n3). What advantages does Cramer’s rule have over elimination?