Curve Fitting y = ab^x Python Program
This Python program implements least square method to fit curve of type y = abx.
We first read n data points from user and then we implement curve fitting for y = abx using least square approach in Python programming language as follow:
Python Source Code: Fitting y = abx
# This is naive approach, there are shortcut methods for doing it!
# Least square method
# Fitting y = ab^x to given n data points
import numpy as np
# Reading value of n
n = int(input("How many data points? "))
# Creating numpy array x & y to store n data points
x = np.zeros(n)
y = np.zeros(n)
# Reading data
print("Enter data:")
for i in range(n):
x[i] = float(input("x["+str(i)+"]= "))
y[i] = float(input("y["+str(i)+"]= "))
# Finding required sum for least square methods
sumX,sumX2,sumY,sumXY = 0,0,0,0
for i in range(n):
sumX = sumX + x[i]
sumX2 = sumX2 +x[i]*x[i]
sumY = sumY + np.log(y[i])
sumXY = sumXY + x[i]*np.log(y[i])
# Finding coefficients A and B
B = (n*sumXY-sumX*sumY)/(n*sumX2-sumX*sumX)
A = (sumY - B*sumX)/n
# Obtaining a and b
a = np.exp(A)
b = np.exp(B)
# Displaying coefficients a, b & equation
print("\nCoefficients are:")
print("a: ", a)
print("b: ", b)
Output
How many data points? 5 Enter data: x[0]= 0 y[0]= 2 x[1]= 1 y[1]= 7 x[2]= 2 y[2]= 11 x[3]= 3 y[3]= 26 x[4]= 4 y[4]= 50 Coefficients are: a: 2.4386066263877857 b: 2.170578677954671