where : ibrtses delphi

# Delphi - polynominal fit

#### disclaimer

the source code of this page may not appear correctly in certain browsers
due to special characters. Have a look at the source of this HTML page

## theory

A polynominal of nth order : p:=Sum(ai*x^i,i=0..n)=a0+a1*x+a2*x^2+ .. +an*x^n
shall be fitted to a bunch of m points (Xj,Yj). The square error shall be minimal:

error:=Sum(Sqr(p(Xj)-Yj),j=1..m)=minimal
We therefore derive the error to all variables, the aj's.
1. calculate the square
2. calculate the sum
3. derive the terms with a0 in them, omit the others
Diff(error,a0):=Diff(Sum(Sqr(a0+a1*Xj+a2*Xj^2+ .. +an*Xj^n-Yj),j=1..m)=
Diff(error,a0):=2*Sum(a0+a1*Xj+a2*Xj^2+.. an*Xj^n-Yj,j=1..m)=
Diff(error,a0):=2*Sum(p(Xj)-Yj,j=1..m)
and equally :
Diff(error,a1):=2*Sum(Xj*(p(Xj)-Yj),j=1..m)
Diff(error,a2):=2*Sum(Xj^2*(p(Xj)-Yj),j=1..m)
..
Diff(error,an):=2*Sum(Xj^n*(p(Xj)-Yj),j=1..m)

For the minimal error, the equations are each set to zero :
Sum(p(Xj)-Yj,j=1..m)=0
Sum(Xj*(p(Xj)-Yj),j=1..m)=0
Sum(Xj^2*(p(Xj)-Yj),j=1..m)=0
..
Sum(Xj^n*(p(Xj)-Yj),j=1..m)=0

Calculating the sums :
Sum(p(Xj)-Yj,j = 1..m)=
Sum(a0+a1*Xj+a2*Xj^2+ .. +an*Xj^n-Yj,j=1..m)=
a0*m+a1*Sum(Xj)+a2*Sum(Xj^2)+..+an*Sum(Xj^n)-Sum(Yj)
Sum(Xj*(p(Xj)-Yj),j = 1..m)=
Sum(a0*Xj+a1*Xj^2+a2*Xj^3+ .. +an*Xj^(n+1)-Xj*Yj,j=1..m)=
a0*Sum(Xj)+a1*Sum(Xj^2)+a2*Sum(Xj^3)+..+an*Sum(Xj^n+1)-m*Sum(Yj*Xj)
..

Ordering leaves n+1 equations for n+1 unknown. As equation system :
```|m         Sum(Xj)       Sum(Xj^2)     .. Sum(Xj^n)    | |a0|    | Sum(Yj)     |
|Sum(Xj)   Sum(Xj^2)     Sum(Xj^3)     .. Sum(Xj^(n+1))| |a1|    | Sum(Xj*Yj)  |
|Sum(Xj^2) Sum(Xj^3)     Sum(Xj^4)     .. Sum(Xj^(n+2))|*|a2| =  | Sum(Xj^2*Yj)|
|..                                                    | |..|    | ..          |
|Sum(Xj^n) Sum(Xj^(n+1)) Sum(Xj^(n+2)) .. Sum(Xj^(2n)) | |an|    | Sum(Xj^n*Yj)|
```
Which can be solved

### notes

• There should be more points than the order of the polynominal.
• As higher order polynominals lack stability, splines are prefered
• The least square linear approximation is the simplest case of a polynominal fit
• The calculations are similar when fitting functions other than polynominals

Feedback is welcome