多項式への最小２乗フィッティング
 polynomial: a[1]+a[2]*x+a[3]*x^2+....    
 赤文字部分は適宜変更すべき箇所

SetDirectory["z:fitfile"]                                                         
data=ReadList["12.dat",Number,RecordLists -> True];    
x = Transpose[data][[1]];y = Transpose[data][[2]];         
ndata = Length[data]

fig1=ListPlot[data]

ndim=2

M = 
          Table[Sum[
              If[x[[i]] == 0, 1, x[[i]]]^(m + n - 2) , {i, 
                1, ndata} ], {m, 1, ndim} , {n, 1, ndim}   ];  
                                 \( (*\ 注意 : \ 
              If[x[\([i]\)] == 0, 1, 
                  x[\([i]\)]]\ は\ x[\([i]\)] が0のとき\ 0^0\ が計算できないので, \ それを避けるために書いてある\ *) \)
u = 
          Table[Sum[
              If[x[[i]] == 0, 1, 
                    x[[i]]]^(n - 1)  y[[i]], {i, 1, 
                ndata}], {n, 1, ndim}];     
Print["M = ", 
        MatrixForm[M]];
Print["u = ", 
        MatrixForm[u]];

Minverse = Inverse[M];                                                                                                 (* \(Minverse\  : \ Mの逆行列\) *)
a = Minverse . u;                                                                                                                   
Print["M-1 = ", 
        MatrixForm[Minverse]];
Print["a= ", 
        MatrixForm[a]];

SumOfSqRes = 
        Sum[(Sum[
                  a[[n]] If[x[[i]] == 0, 1, 
                        x[[i]]]^(n - 1) , {n, 1, ndim}] - 
                y[[i]])^2, {i, 1, ndata}] 
                                                                                                                                                    
sigma2 = SumOfSqRes/(ndata - ndim)                                                                        
sigmaA = 
          Table[Sqrt[sigma2 * Minverse[[n, n]]], {n, 1, ndim}];         
Print["sigmaA= ", 
        MatrixForm[sigmaA]];

fig2 = 
        Plot[Sum[
            a[[n]] If[X == 0, 1, X]^(n - 1) , {n, 1, 
              ndim}], {X, x[[1]], x[[ndata]]}, 
          PlotStyle -> Hue[1]];

Show[fig1, fig2]                                                                                                               \( (*\ まとめのプロット\ \ *) \)
Do[
      Print["a[", n, "] = ", a[[n]], "  +-  ", 
        sigmaA[[n]] , "        (x^", 
        n - 1, "\:306e\:4fc2\:6570)" ], {n, 1, 
        ndim}]