2
$\begingroup$

Consider the linear system AX=B,where A={{3, 0, 2}, {-3, 2, 2}, {2, -3, 3}},B={{3}, {-1}, {4}} and x={{x},{y},{z}}.So i tried like this:

A1 = {{, ,}, {, ,}, {, ,}}; A3 = {{, ,}, {, ,}, {, ,}}; A2 = {{, ,}, \
{, ,}, {, ,}}; For[i = 1, i <= 3, i++,
 For[j = 1, j <= 3, j++, 
  If[j == 1, A1[[i, j]] = B[[i, 1]], A1[[i, j]] = A[[i, j]]]]]; For[
 i = 1, i <= 3, i++,
 For[j = 1, j <= 3, j++, 
  If[j == 2, A2[[i, j]] = B[[i, 1]], A2[[i, j]] = A[[i, j]]]]]; For[
 i = 1, i <= 3, i++,
 For[j = 1, j <= 3, j++, 
  If[j == 3, A3[[i, j]] = B[[i, 1]], 
   A3[[i, j]] = A[[i, j]]]]]; x = (Det[A1]/Det[A]); y = (Det[A2]/
   Det[A]); z = (Det[A3]/Det[A]); Print["x=", x, " y=", y, " z=", z]

But i want to do this using only one loop.i am new user of mathematica.So can Anyone help me to figure out this. Any hints or solution will be appreciated.
Thanks in Advanced.

$\endgroup$
2
  • $\begingroup$ Have you seen this? $\endgroup$ Commented Jan 6, 2019 at 13:04
  • $\begingroup$ @J.M.iscomputer-less i will try to avoid For loop.But for the seek of answer can you help me to solve it?!! Thanks for your link $\endgroup$ Commented Jan 7, 2019 at 14:46

3 Answers 3

2
$\begingroup$

If you just want the solution, you may use Inverse[A].B. If you want to illustrate Cramer's rule, the A1, A2, A3 matrices do not need to be constructed via any loop (using lower case letter below):

a1 = Join[B, A[[;; , 2 ;; 3]], 2];
a2 = Join[A[[;; , 1 ;; 1]], B, A[[;; , 3 ;; 3]], 2];
a3 = Join[A[[;; , 1 ;; 2]], B, 2];

then

x = Det[a1]/Det[A]
y = Det[a2]/Det[A]
z = Det[a3]/Det[A]

gives 13/23, -7/23, 15/23. You can verify that the a1 through a3 matrices are identical to your A1 ... A3.

For more on the use of Join, refer to the documentation or here. Also, to initialize a matrix for later use you may try something like Table[Null, 3, 3] instead of typing out {{, ,}, {, ,}, {, ,}}

Alternatively, you may just use:

a1 = a2 = a3 = A;
a1[[;; , 1]] = a2[[;; , 2]] = a3[[;; , 3]] = Flatten[B];
{x, y, z} = Det[#]/Det[A] & /@ {a1, a2, a3}
$\endgroup$
2
  • 1
    $\begingroup$ only tangentially: in general, LinearSolve[A, B] is better to do than Inverse[A].B. $\endgroup$ Commented Jan 5, 2019 at 4:54
  • $\begingroup$ @egwene sedai thanks for your answer but i really want to do it by loop.Yes i understood using 'LinearSolve[A,B]' maybe less working.but i want to learn it by loop method.could you update your answer? Thanks again $\endgroup$ Commented Jan 5, 2019 at 6:31
2
$\begingroup$

Well, even without using the built-in LinearSolve (which is much faster and more accurate for numerical systems), you can still avoid introducing procedural auxiliary variables (e.g., A1, A2, and A3):

In[1]:= A = {
            {3, 0, 2},
            {-3, 2, 2},
            {2, -3, 3}
           }; B = Flatten[{
             {3},
             {-1},
             {4}
            }, 1];

In[2]:= {x, y, z} = Det[SubsetMap[B &, A, {All, #}]] & /@ 
           Range[3]/Det[A]

Out[2]= {13/23, -7/23, 15/23}

In[3]:= A . {x, y, z} \[LongEqual] B

Out[3]= True

Alternatively, you may just use:

In[4]:= Det[ReplacePart[A \[Transpose], B, #] \[Transpose]] & /@ 
         Range[3]/Det[A]

Out[4]= {13/23, -7/23, 15/23}

Note that the second transposition operation here is in fact unnecessary.

$\endgroup$
1
$\begingroup$
a = {{3, 0, 2}, {-3, 2, 2}, {2, -3, 3}};
b = {3, -1, 4};
m = Transpose[Transpose[a]~Join~{b}];
cr = MatrixForm[#] -> Det[#] & /@ Table[m[[All, 
  j]], {j, {{1, 2, 3}, {4, 2, 3}, {1, 4, 3}, {1, 2, 4}}}]; 
sol = {##2}/#1 & @@ Values[cr]

Confirming solution:

LinearSolve[a, b]

enter image description here

Update

cramer[a_, b_] := Module[{v = Join[Transpose[a], {b}], n, m},
  n = Length[v];
  m = Transpose /@ Table[Permute[v, Cycles[{{j, n}}]], {j, n - 1}];
  Det[#[[All, 1 ;; n - 1]]]/Det[a] & /@ m
  ]

cramer[a,b]

-> {13/23, -(7/23), 15/23}

Comment: original answer was wrong and now corrected.

$\endgroup$
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.