Curve Fitting of Type y=ab^x Algorithm
In this article we are going to develop an algorithm for fitting curve of type y = abx using least square regression method.
Procedure for fitting y = abx
We have,
y = abx ----- (1)
Taking log on both side of equation (1), we get
log(y) = log(abx) log(y) = log(a) + log(bx) log(y) = log(a) + x*log(b) ----- (2) Now let Y = log(y), A = log(a) and B = log(b) then equation (2) becomes, Y = A + Bx ----- (3), Now we fit equation (3) using least square regression as: 1. Form normal equations: ∑Y = nA + B ∑x ∑xY = A∑x + B∑x2 2. Solve normal equations as simulataneous equations for A and B 3. We calculate afrom A and bfrom B as: a = exp(A) b = exp(B) 4. Substitute the value of a and b in y= abx to find line of best fit.
Algorithm for Fitting Curve y = abx
1. Start 2. Read Number of Data (n) 3. For i=1 to n: Read Xi and Yi Next i 4. Initialize: sumX = 0 sumX2 = 0 sumY = 0 sumXY = 0 5. Calculate Required Sum For i=1 to n: sumX = sumX + Xi sumX2 = sumX2 + Xi * Xi sumY = sumY + log(Yi) sumXY = sumXY + Xi * log(Yi) Next i 6. Calculate Required Constant A and b of Y = A + Bx: B = (n * sumXY - sumX * sumY)/(n*sumX2 - sumX * sumX) A = (sumY - B*sumX)/n 7. Transformation of A to a and B to b: a = exp(A) b = exp(B) 8. Display value of a and b 8. Stop
Recommended Readings