Curve Fitting of Type y=ax^b Algorithm

In this article we are going to develop an algorithm for fitting curve of type y = axb using least square regression method.

Procedure for fitting y = axb

We have,

	
y = axb ----- (1)

Taking log on both side of equation (1), we get

	
log(y) = log(axb)

log(y) = log(a) + log(xb)

log(y) = log(a) + b*log(x) ----- (2)

Now let Y = log(y), A = log(a) and X = log(x) 

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 a from A using:
   a = exp(A)

4. Substitute the value of a and b in 
   y= axb to find line of best fit.

Algorithm for fitting y = axb

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 + log(Xi)
     sumX2 = sumX2 + log(Xi) * log(Xi)
     sumY = sumY + log(Yi)
     sumXY = sumXY + log(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:
   a = exp(A)
   
8. Display value of a and b

8. Stop

Recommended Readings

  1. Algorithm for fitting Curve y = axb
  2. Pseudocode for fitting y = axb
  3. C Program for fitting curve y = axb
  4. C++ Program for fitting curve y = axb
  5. Algorithm for fitting Curve y = abx
  6. Pseudocode for fitting y = abx
  7. C Program for fitting curve y = abx
  8. C++ Program for fitting curve y = abx