Linear System Experiments

(*
Project #1
explores working with linear systems, using two kinds of matrices: Hilbert matrices and a made-up family of matrices. 

Your assignment is to experiment with the three methods below for various values of n (the size of the matrices) and the two families of matrices.
Hand in a description of your experiments and observations.*)

n=3; (* size of the matrix *)

(* mat=HilbertMatrix[n]//N;*)
mat=Table[Sin[(i+j)^2-i-j],{i,1,n},{j,1,n}]//N ;

b=Table[1.0,{i,1,n}];(* the RHS of the system *)

(* solve the system mat.x=b: *)

x=LinearSolve[mat,b];

(* check the solution: *)
Print["Max error:  ", Max[mat.x-b]]

(* Now the same using the inverse: *)

matinv=Inverse[mat];
y=matinv.b;

(* check the solution: *)
Print["Max error:  ", Max[mat.y-b]]

(* Cramer's rule: *)

carray=Table[mat,{i,1,n}];
 Do[carray[[i,i]]= b,{i,1,n}];

det=Det[mat];
sol=Table[Det[carray[[i]]]/det,{i,1,n}];

(* check the solution: *)
Print["Max error:  ", Max[mat.sol-b]]