Mathematica Asked on June 25, 2021
I want to solve the following linear equations:
$$left{begin{aligned}
x_{1}+3 x_{2}+x_{3} &=0
2 x_{1}+6 x_{2}+3 x_{3}-2 x_{4} &=0
-2 x_{1}-6 x_{2}-4 x_{4} &=0
end{aligned}right.$$
The form of the solution given in the textbook is as follows:
$$boldsymbol{X}=left(begin{array}{c}
x_{1}
x_{2}
x_{3}
x_{4}
end{array}right)=left(begin{array}{rr}
-3 k_{1}-2 k_{2}
k_{1} & 2 k_{2}
k_{2}
end{array}right)=k_{1}left(begin{array}{r}
-3
1
0
0
end{array}right)+k_{2}left(begin{array}{r}
-2
0
2
1
end{array}right), quad k_{1}, k_{2} text { are arbitrary constants }$$
Solve[{x1 + 3 x2 + x3 == 0,
2 x1 + 6 x2 + 3 x3 - 2 x4 == 0, -2 x1 - 6 x2 - 4 x4 == 0}, {x1, x2,
x3, x4}]
LinearSolve[{{1, 3, 1, 0}, {2, 6, 3, -2}, {-2, -6, 0, -4}}, {0, 0, 0}]
NullSpace[{{1, 3, 1, 0}, {2, 6, 3, -2}, {-2, -6, 0, -4}}]
$$left{begin{aligned}
x_{1}+x_{2}-2 x_{3}-x_{4}=& 4
3 x_{1}-2 x_{2}-x_{3}+2 x_{4}=& 2
5 x_{2}+7 x_{3}+3 x_{4}=-2
2 x_{1}-3 x_{2}-5 x_{3}-x_{4}=& 4
end{aligned}right.$$
The form of the solution given in the textbook is as follows:
$$boldsymbol{X}=left(begin{array}{c}
x_{1}
x_{2}
x_{3}
x_{4}
end{array}right)=left(begin{array}{r}
1
1
-1
0
end{array}right)+kleft(begin{array}{r}
-frac{2}{3}
frac{1}{3}
-frac{2}{3}
1
end{array}right)=left(begin{array}{r}
1
1
-1
0
end{array}right)+k_{1}left(begin{array}{r}
-2
1
-2
3
end{array}right), quad k, k_{1} text { are arbitrary constants }$$
Solve[{{1, 1, -2, -1}, {3, -2, -1, 2}, {0, 5, 7,
3}, {2, -3, -5, -1}}.{x1, x2, x3, x4} == {4, 2, -2, 4}, {x1, x2,
x3, x4}]
LinearSolve[{{1, 1, -2, -1}, {3, -2, -1, 2}, {0, 5, 7,
3}, {2, -3, -5, -1}}, {4, 2, -2, 4}]
NullSpace[{{1, 1, -2, -1}, {3, -2, -1, 2}, {0, 5, 7,
3}, {2, -3, -5, -1}}]
But the solution form of the above code output is not the style of the textbook. How can we make the LinearSolve
function output consistent with the result form of the textbook (arbitrary constant can be replaced by c
)?
In addition, I have another problem. To prove that two linear equations have the same solution, we need the row vectors of their augmented matrices to be equivalent to each other.
For example, to prove that the linear equations represented by the following two matrices have the same system of solutions, I need to remove the meaningless zero row vector at the end:
RowReduce[{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}]
RowReduce[{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}, {2, 4, 6}}]
I wonder if there’s any more clever way to get rid of the meaningless zero row vector at the end of a matrix:
Select[RowReduce[{{1, 2, 3}, {4, 5, 6}, {7, 8, 9}, {2, 4,
6}}], # 0 != # &]
By the way, it’s inconvenient to customize the display size of the inserted images.
One might Solve
over the integers (like LinearSolve
does in this case) to avoid fractions:
solvec = {x1, x2, x3, x4} /.
Solve[
{{1, 1, -2, -1},
{3, -2, -1, 2},
{0, 5, 7, 3},
{2, -3, -5, -1}
}.{x1, x2, x3, x4} == {4, 2, -2, 4},
{x1, x2, x3, x4}, Integers
] // Normal // First
(* {1 + 2 C[1], 1 - C[1], -1 + 2 C[1], -3 C[1]} *)
Output formatting with CoefficientArrays
:
vectorToColumnMatrix = Transpose@List[#] &;
columnMatrices = Transpose /@ List /@ Transpose[#] &;
Flatten@ MapThread[Apply]@ {
{MatrixForm @* vectorToColumnMatrix,
Map[MatrixForm] @* columnMatrices},
List /@ CoefficientArrays[solvec, Variables[solvec]]
} . Prepend[Variables[solvec], 1] // Normal
One could add GeneratedParameters -> k
to Solve
and
k /: Format[k[n_]] := Subscript[k, n];
to get
Correct answer by Michael E2 on June 25, 2021
I wrote a simple custom function to implement this requirement:
linearSolve[mat_?MatrixQ, b_] :=
Module[{m, n, L, k, generalsolution, specialsolution,
specialsolutionColumn},
{m, n} = Dimensions[mat];
L = Length[NullSpace[mat]];
generalsolution =
MapThread[
Defer[#1*MatrixForm[#2]] &, {Array["k" <> ToString[#] &, L],
Map[List[#][Transpose] &, NullSpace[mat]]}];
specialsolution = LinearSolve[mat, b];
specialsolutionColumn = {LinearSolve[mat, b]}[Transpose];
If[specialsolution.specialsolution == 0 || b.b == 0,
If[Length[generalsolution] != 1,
StandardForm[
Defer @@
MakeExpression[StringRiffle[generalsolution, "+"],
StandardForm]], StandardForm[Defer @@ First[generalsolution]]],
StandardForm[
Defer @@
MakeExpression[
StringRiffle[
PrependTo[generalsolution, MatrixForm[specialsolutionColumn]],
"+"], StandardForm]]]]
linearSolve[{{1, 1, -2, -1}, {3, -2, -1, 2}, {0, 5, 7,
3}, {2, -3, -5, -1}}, {4, 2, -2, 4}]
linearSolve[{{1, 0, -1}, {1, -1, 0}, {-2, 1, 1}}, {0, 0, 0}]
linearSolve[{{1, 1, 1}, {2, 2, 2}, {3, 3, 3}}, {1, 2, 3}]
linearSolve[{{1, 1, 1}, {2, 2, 2}, {3, 3, 3}}, {0, 0, 0}]
{{1, 0, -1}, {0, 0, 0}, {0, 0, 1}, {0, 0,
0}} /. {a__List, {0 ..} ..} -> {a}
I'd like to see other people do that in a more subtle way.
Answered by A little mouse on the pampas on June 25, 2021
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP