user3516612
user3516612

Reputation: 21

How to curve fit data to a known formula with two independence variables?

Suppose I have a nonlinear formula like y = (ax + bx^2 + cx^3) * dx1^2

Where a,b,c,d are coefficients to be found and x and x1 are data in a table I am trying to fit.

I need an optimization algorithm I can write in code (e.g C or Delphi) to run through an iteration to get reasonable coefficients a,b,c,d.

Don't want to use Matlab or packages as this must be a stand alone program. Reference to a delphi or active X unit is helpful. Don't mind to pay for the software if I can use it freely.

Upvotes: 1

Views: 200

Answers (1)

MvG
MvG

Reputation: 60958

Your problem is linear in a, b, c and d even if it is cubic in the data. Therefore I would suggest formulating this as an ordinary linear least squares problem. Allow me to rename your x1 to z. The idea is this: you have

axi + bxi2 + cxi3 + dzi2 ≈ yi

for some i∈{1,2,3…n}. You can write that as an approximate matrix equation:

⎡x₁ x₁² x₁³ z₁²⎤   ⎡a⎤   ⎡y₁⎤
⎢x₂ x₂² x₂³ z₂²⎥   ⎢b⎥   ⎢y₂⎥
⎢x₃ x₃² x₃³ z₃²⎥ ∙ ⎢c⎥ ≈ ⎢y₃⎥
⎢ ⋮ ⋮  ⋮  ⋮ ⎥   ⎣d⎦   ⎢⋮⎥
⎣xₙ xₙ² xₙ³ zₙ²⎦         ⎣yₙ⎦

Or more shortly

M ∙ X ≈ Y

Now you multiply both sides with the transpose of that matrix M:

MT ∙ M ∙ X = MT ∙ Y

Notice that I changed from ≈ to = because the least squares solution will satisfy this modified equation exactly (for lengthy reasons I don't want to go into here). This is a simple 4×4 system of linear equations. Solve using common techniques (such as Gaussian elimination) to find X=(a,b,c,d).

If n is large you can even compute (MT ∙ M) and (MT ∙ Y) on the fly without ever storing M itself. That way 4×4+4=20 numbers will be all the memory you need to maintain between input records. Actually (MT ∙ M) is symmetric so 10 numbers are enough for the matrix, 14 in total.

Upvotes: 3

Related Questions