(*tut 2A, Q1*)
A={{1,3,1},{2,-1,-3},{1,1,-2}};
H={1,1,5};
Print["A=",MatrixForm[A]];

B=Inverse[A];
Print["Inverse[A]=",MatrixForm[B]];
Print["Check: A*Inverse[A]=",MatrixForm[B.A]];

Adjoin=Det[A]*B;
Print["Adjoin=",Adjoin//MatrixForm]

Print["solution, {x1,x2,x3} = ",LinearSolve[A,H]]

(*tut 2A, Q2*)
A={{1,3,1,-2},{2,-1,-3,0},{1,2,-2,0},{0,1,0,3}};
H={2,1,2,0};X={x1,x2,x3,x4};
aug={{1,3,1,-2,2},{2,-1,-3,0,1},{1,2,-2,0,2},{0,1,0,3,0}};
Print["A=",MatrixForm[A]];
Print["H=",MatrixForm[H]];
Print["Rank[A]=",MatrixRank[A]];
Print["Rank[A|H]=",MatrixRank[aug]];
Print["Since Rank[A]=Rank[A|H], the system is consisten"];
Print["AX=H is ",MatrixForm[A] .MatrixForm[X],"=",MatrixForm[H]];
Print["particular solution, X_p = ",LinearSolve[A,H]//MatrixForm]
Print["A.X_p= ",MatrixForm[A.LinearSolve[A,H]]];
Print["Now solve for the homogeneous solution, X_h, for the non-homogeneous system, AX=0"];
Print["RREF[A]=",RowReduce[A]//MatrixForm];
Print["A.X =",A//MatrixForm ,X//MatrixForm,"=",{0,0,0,0}//MatrixForm];
Print["≡ RREF[A].X =",RowReduce[A]//MatrixForm ,X//MatrixForm,"=",{0,0,0,0}//MatrixForm];
Print["Hence the solution for the homegenoues equation, AX=0 is X_h=",{0,0,0,0}//MatrixForm];
Print["Hence the complete solution for the non-homegenoues equation, AX=H is Xc=X_p+X_h=X_p"];
Print["The particular solution for the AX=H, is unique because the number of unknonws n is the same as the number of equation, = 4 , "];

(*tut 2A, Q3*)

A={{3,1,2,1,0},{2,1,3,-1,0},{1,0,1,2,-1}};
H={-2,3,4};X={x1,x2,x3,x4,x5};n=Length[X];
aug={{3,1,2,1,0,-2},{2,1,3,-1,0,3},{1,0,1,2,-1,4}};
Print["A=",MatrixForm[A]];
Print["H=",MatrixForm[H]];
Print["Rank[A]=",MatrixRank[A]];
Print["Rank[A|H]=",MatrixRank[aug]];
Print["Since Rank[A]=Rank[A|H], the system is consisten"];
Print["AX=H is ",MatrixForm[A] .MatrixForm[X],"=",MatrixForm[H]];
Print["particular solution, X_p = ",LinearSolve[A,H]//MatrixForm]
Print["A.X_p= ",MatrixForm[A.LinearSolve[A,H]]];
Print["Now solve for the homogeneous solution, X_h, for the non-homogeneous system, AX=0"];
Print["n-Rank[A]=",n,"-",MatrixRank[A],"=",n-MatrixRank[A]];
Print["There will be ",n-MatrixRank[A], " linearly independent solution for AX=0"];
Print["A.X =",A//MatrixForm ,X//MatrixForm,"=",{0,0,0}//MatrixForm];
Print["≡ RREF[A].X =",RowReduce[A]//MatrixForm ,X//MatrixForm,"=",RowReduce[A].X//MatrixForm ,"=",{0,0,0}//MatrixForm];
Print["Let x4=a, x5=b"];
Print["The solution for the homegenoues equation, AX=0 is"];
Print["X_h="
MatrixForm[X],"=",a*MatrixForm[{-2,5,0,1,0}]+b*MatrixForm[{1/2,-5/2,1/2,0,1}]
];
Print["Hence the complete solution for the non-homegenoues equation, AX=H is Xc=X_p+X_h"];
Print["The particular solution for the AX=H, is not unique because the number of unknonws, n = ", n," is not equal the number of equation, = 3"];

(*
A={{3,1,2,1},{2,1,3,-1},{1,1,2,-1}};
(*H={-2,3,4};*)
H={0,0,0};
Print["A=",MatrixForm[A]];
Print["H=",MatrixForm[H]];
Print["Rank[A]=",MatrixRank[{{3,1,2,1},{2,1,3,-1},{1,1,2,-1}}]]

Print["Trivial solution, of AX=0 is X={x1,x2,x3,x4} = ",LinearSolve[A,H]]*)