Quick guide to Mathematica

To get help, place the cursor on the function and press F1
To run all commands in one cell, place the cursor there and press Shift + Enter or Enter on the numerical keyboard.
Output of many commands is suppressed by ; at the end of the command. If you want to see the output, delete ;
Argument of functions must be in square brackets [...], braces {...} are for arrays, ranges etc., use double brackets [[...]] to access elements of arrays
expr /. {x -> a} means substitute a for x in expr, expr // function means apply function to expr, i.e. it is equivalent to function[expr].
D[f[x],x] = derivative of f[x] with respect to x.
To define a function of x one can use f[x_] := 1+(x^2), notice the underscore and the colon, or # nad & like in f := 1+((#)^2) &
N[expr] evaluates expr with machine precision, if you use the decimal point in the expression it will be also evaluated with machine precision
Special characters can be inserted by pressing Esc ... Esc, e.g. Esc p Esc gives π
Clear[...] is used to unset any variables which could have been assigned previously.
Some other useful commands: Simplify, Expand, Factor; Integrate, Series, Sum

Clear all symbols from previous evaluations to avoid problems

Clear["Global`*"]

Least square method - application on fitting of functions

Run Basic.Functions.for.TMF057.m first !

Preparation of data to fit

Function sin(kx) with some random noise

Clear[m,x,fx];
f[x_]=Sin[8x];
xnoise=0.0;ynoise=0.1;
xmin=-1.0-xnoise;xmax=1.0+xnoise;
m=100;
x=Range[xmin,xmax,(xmax-xmin)/(m-1)]+xnoise*(RandomReal[{0,1},m]-0.5);
fx=f[x]+ynoise*(RandomReal[{0,1},m]-0.5);
data={x,fx};
ListPlot[Transpose[{x,fx}],PlotRange->{-1.0-ynoise,1.0+ynoise}]

Construct the matrix A as the Vandermode matrix

n=15;
A=ConstantArray[1.0,{m,n+1}];
A[[All,2;;n+1]]=Table[Table[x[[im]]^in,{in,1,n}],{im,1,m}];
A//MatrixForm;

Solve the overdetermined system Ac = f where c are coefficients of polynomial of order n using Mathematica built-in function

Print["Polynomial coefficients:"];
c=LeastSquares[A,fx]
p[y_]=MyPolynomial[c,y];
Print["Sum of squared residuals: ", Sum[Abs[p[x[[i]]]-fx[[i]]]^2,{i,1,n}]];
Show[ListPlot[Transpose[{x,fx}]],Plot[p[y],{y,xmin,xmax}],PlotRange->{-1.0-ynoise,1.0+ynoise}]

Solve Ac = f using normal equations

Print["Polynomial coefficients:"];
c1=LinearSolve[ConjugateTranspose[A].A,ConjugateTranspose[A].fx]
Print["Maximal difference from coeeficients obtained using Mathematica: ", Max[Abs[c1-c]]];
p[y_]=MyPolynomial[c1,y];
Print["Sum of squared residuals: ", Sum[Abs[p[x[[i]]]-fx[[i]]]^2,{i,1,n}]];
Show[ListPlot[Transpose[{x,fx}]],Plot[p[y],{y,xmin,xmax}],PlotRange->{-1.0-ynoise,1.0+ynoise}]

Solve Ac = f using QR factorization

{Q,R}=QRDecomposition[A];
b=Q.fx;
Print["Polynomial coefficients:"];
c2=LinearSolve[R[[1;;n+1,All]],b[[1;;n+1]]]
Print["Maximal difference from coeeficients obtained using Mathematica: ", Max[Abs[c2-c]]];
p[y_]=MyPolynomial[c2,y];
Print["Sum of squared residuals: ", Sum[Abs[p[x[[i]]]-fx[[i]]]^2,{i,1,n}]];
Show[ListPlot[Transpose[{x,fx}]],Plot[p[y],{y,xmin,xmax}],PlotRange->{-1.0-ynoise,1.0+ynoise}]

Solve Ac = f using SVD

{U,Σ,V}=SingularValueDecomposition[A];
Print["Singular values:"]
Diagonal[Σ]
Σ1=DiagonalMatrix[1/Diagonal[Σ]];
b=ConjugateTranspose[U].fx;
Print["Polynomial coefficients:"];
c3=V.Σ1.b[[1;;n+1]]
Print["Maximal difference from coeeficients obtained using Mathematica: ", Max[Abs[c3-c]]];
p[y_]=MyPolynomial[c3,y];
Print["Sum of squared residuals: ", Sum[Abs[p[x[[i]]]-fx[[i]]]^2,{i,1,n}]];
Show[ListPlot[Transpose[{x,fx}]],Plot[p[y],{y,xmin,xmax}],PlotRange->{-1.0-ynoise,1.0+ynoise}]