Search This Blog

Tuesday, December 13, 2011

Quadratic Fitting of Data

I recently had to tear my hair out finding an algorithm for the coefficients of a second order fit for a set of data points ( first order fits are easy).  I found this below, in FORTRAN, and translated it into VB:

  Sub QuadFit(N As Double, P As Double, i As Integer, X() As Double, _
              Y() As Double, Q As Double, N As Double, R As Double, _
              S As Double, T as Double, U As Double, V As Double, _
              W as Double, as As Double, b As Double, c As Double)
  
    P = 0


    For i = 1 To N
      P = P + X(i)
    Next i


    Q = 0

    For i = 1 To N
      Q = Q + X(i) ^ 2
    Next i


    R = 0

    For i = 1 To N
      R = R + X(i) ^ 3
    Next i


    S = 0

    For i = 1 To N
      S = S + X(i) ^ 4
    Next i


    T = 0

    For i = 1 To N
      T = T + Y(i)
    Next i


    U = 0

    For i = 1 To N
      U = U + X(i) * Y(i)
    Next i


    V = 0

    For i = 1 To N
      V = V + X(i) ^ 2 * Y(i)
    Next i


    W = N * Q * S + 2 * P * Q * R - Q ^ 3 - P ^ 2 * S - N * R ^ 2
    a = (N * Q * V + P * R * T + P * Q * U - Q ^ 2 * T - P ^ 2 * V - N * R * U) / W
    b = (N * S * U + P * Q * V + Q * R * T - Q ^ 2 * U - P * S * T - N * R * V) / W
    c = (Q * S * T + Q * R * U + P * R * V - Q ^ 2 * V - P * S * U - R ^ 2 * T) / W


  End Sub

I hope you can use it.

Neurophilosophy

From Wikipedia, the free encyclopedia https://en.wikipedia.org/wiki/Neurophilosophy ...