Python sample code:
The purpose of curve fitting is to find the function that is best fit to the original data, where the estimated data does not need to pass through the original data set. The numpy function np.polyfit can be used to fit the data to a polynomial of the specified degree using the least squares algorithm. The output of the function is the coefficients of the polynomial. See the highlight lines 4, 6, 9 and 16.
import numpy as np import matplotlib.pyplot as plt x = np.array([0, 1, 2, 3, 4, 5]) print('x = ', x) y = np.array([0, 11, 26, 37, 54, 61]) print('y = ', y) coef1 = np.polyfit(x, y, 1) print('coef1 = ', coef1) plt.plot(x,y) plt.xlabel("x", fontsize=12) plt.ylabel("y", fontsize=12) plt.show() coef1 = np.polyfit(x, y, 1) print('coef1 = ', coef1) plt.plot(x,y) plt.xlabel("x", fontsize=12) plt.ylabel("y", fontsize=12) plt.show()
Figure 1. Fitting a Quadratic Curve