Linear Systems

We find the solution x of the linear system mat.x=b. The vector b is black, guesses for x are green, and the resulting mat.x is red. The solution is found once the red and black arrows are the same.

Several 2 x2 systems

orig={0,0};
mat={{2,1},{0,-2}};

b={-4.0,1};
sol=LinearSolve[mat,b];

Manipulate[Graphics[{Green,Thick,Arrow[{orig,p}],Black,Arrow[{orig,b}],Red,Arrow[{orig,mat.p}]},Axes->True,ImageSize->200,PlotRange->{{-4,4},{-4,4}},GridLines->Automatic],{{p,{2,0}},Locator}]

Print[MatrixForm[mat],", ",MatrixForm[b],"  solution: ",MatrixForm[LinearSolve[mat,b]]]
Print["check: ",MatrixForm[mat],".",MatrixForm[sol],"=",MatrixForm[b]]

orig={0,0};
mat={{2,-1},{3,2}};

b={-4.0,1};
sol=LinearSolve[mat,b];

Manipulate[Graphics[{Green,Thick,Arrow[{orig,p}],Black,Arrow[{orig,b}],Red,Arrow[{orig,mat.p}]},Axes->True,ImageSize->200,PlotRange->{{-4,4},{-4,4}},GridLines->Automatic],{{p,{2,0}},Locator}]

Print[MatrixForm[mat],", ",MatrixForm[b],"  solution: ",MatrixForm[LinearSolve[mat,b]]]
Print["check: ",MatrixForm[mat],".",MatrixForm[sol],"=",MatrixForm[b]]orig={0,0};

An unsolvable sytem

mat={{2,1},{4,2}};

b={-4.0,1};
sol=LinearSolve[mat,b];

Manipulate[Graphics[{Green,Thick,Arrow[{orig,p}],Black,Arrow[{orig,b}],Red,Arrow[{orig,mat.p}]},Axes->True,ImageSize->200,PlotRange->{{-4,4},{-4,4}},GridLines->Automatic],{{p,{2,0}},Locator}]

Print[MatrixForm[mat],", ",MatrixForm[b],"  solution: ",MatrixForm[LinearSolve[mat,b]]]
Print["check: ",MatrixForm[mat],".",MatrixForm[sol],"=",MatrixForm[b]]

A simple linear system with a diagonal matrix

mat1={{1,0,0},{0,2,0},{0,0,-1}};
b1={1,1,1};
sol1=LinearSolve[mat1,b1];


Print[MatrixForm[mat1],", ",MatrixForm[b1],"  solution: ",MatrixForm[sol1]]

Print["check: ",MatrixForm[mat1],".",MatrixForm[sol1],"=",MatrixForm[b1]]

A larger system

mat3=Table[Abs[i-j],{i,1,5},{j,1,5}];
b3=Table[i,{i,1,5}];
sol3=LinearSolve[mat3,b3];


Print[MatrixForm[mat3],", ",MatrixForm[b3],"  solution: ",MatrixForm[sol3]]

Print["check: ",MatrixForm[mat3],".",MatrixForm[sol3],"=",MatrixForm[b3]]

Using the inverse matrix

The linear system Ax=b has the solution (A^(-1))Ax=(A^(-1))b or x=(A^(-1))b. We use the above mat3 for A.

mat3inv=Inverse[mat3];
Print["check inverse: ",MatrixForm[mat3],".",MatrixForm[mat3inv],"=",MatrixForm[mat3.mat3inv]]
Print["check solution: ",MatrixForm[mat3inv],".",MatrixForm[b3],"=",MatrixForm[mat3inv.b3]]