TO WRITE A PROGRAM FOR CURVE FITTING USING PYTHON
AIM
To write a code to obtain curve which fit a linear and a cubic polynomial for the cp data.
BODY OF CONTENT
Definition of curve fitting
Curve Fitting is the process of constructing a curve or the mathematical function which has the best fit for the series of original data.
In other words, curve obtain after minimizing the error to improve accuracy of the fitting curve when compared to original data curve.
Curve fitting method is used to find the best fit curve to study the behavior of the function. In order to obtain best fit or perfect fit we have to increase the degree of polynomial because it improves the accuracy. But in this program, we plot a best fit without increasing the order of polynomial I use splitting of data to obtain a fit for the original data set.
STEPS OF PYTHON PROGRAM
Before start of coding we have to confirm that the data files are in same directory where you are going to save the PYTHON program.
- Provide topic of the code which is under comment
- Calling of library which will used for plotting and calculating i.e. math, Scipy and matplotlib.pyplot library.
- From line 11 to 13 program is written to form function for linear equation.
In line 12, defining of function is done for linear equation which include t, a, b where t is variable and a, b are constant.
In line 13, the value obtain for linear equation is return to func_linear.
- From line 16 to 18 program is written to form function for cubic equation.
In line 17, defining of function is done for cubic equation which include t, a, b, c, d where t is variable and a, b, c, d are constant.
In line 18, the value obtain for linear equation is return to func_cubic.
- From line 21 to 35, program is written to form function for reading the data file.
In line 23 and 24 contain empty array for storing temperature and cp values.
In line 26, for loop is used to open the data file in read mode.
In line 29, split command is used to split the each line in the data file where it find “,” sign.
In line 31 and 33, append command is used to store the value of temperature and cp.
In line 35, the value of temperature and cp are return to the function read_file.
- from line 37 to 120, the program is written to obtain the curve fit for linear as well as cubic curve.
In line 39, separating the values of temperature and cp by calling the read_file function
- In line 43, by the help of curve fit command we can find the value of coefficient which fit with the original set of data.
In line 44, by the help of set of array of temperature and the values of coefficient of linear equation is used to calculate the fit of cp for linear equation.
- From line 46 to 53, code is written to plot the curve for linear equation.
In line 47, command is used to define the figure.
In line 48, command to plot the curve for original set of data.
In line 49, command is used to plot the curve which is fit for linear equation.
In line 50, legend command is written to define the curve with name
In line 51 and 52, command is used to label the X and Y axis of the plot.
In line 53, show command is used to display the curve.
- From line 58 to 62, code is used to find the curve fit for cubic equation without using splitting method.
In line 61, by the help of curve fit command we can find the value of coefficient which fit with the original set of data.
In line 62, by the help of set of arrays of temperature and the values of coefficient of linear equation is used to calculate the fit of cp for cubic equation.
- From line 64 to 71, code is written to plot the curve for cubic equation without using splitting method.
In line 65, command is used to define the figure.
In line 66, command to plot the curve for original set of data.
In line 67, command is used to plot the curve which is fit for cubic equation without splitting the set of data.
In line 68, legend command is written to define the curve with name.
In line 69 and 70, command is used to label the X and Y axis of the plot.
In line 71, show command is used to display the curve.
- From line 79 to 88, code is used to split the temperature values and cp values.
Like for temperature in line 79, temperature from 0 to 400 is saved in the temp1 variable.
Similarly, for cp in line 84, cp from 0 to 400 is saved in the cp1.
This process is repeated until the final temperature is not reached.
- From line 93 to 106, code is written to solve the equation for cubic equation for all the values of splited temperature and cp
In line 93, by the help of curve fit command we can find the value of coefficient which fit with the original set of data.
In line 94, by the help of set of arrays of temperature and the values of coefficient of cubic equation is used to calculate the fit of cp1 for cubic equation.
Similarly, we do for all split set of temperature and cp values.
- From line 108 to 120, code is written to plot the curve for cubic equation for different values of temperature and cp with using splitting method.
In line 109, command is used to define the figure.
In line 110, command to plot the curve for original set of data.
From line 111 to 115, command is used to plot the curve which is fit for cubic equation with splitting the set of data.
In line 117, legend command is written to define the curve with name.
In line 118 and 119, command is used to label the X and Y axis of the plot.
In line 120, show command is used to display the curve.
14. From line 127 to 142, code is written to find the error for linear equation in order to obtain R square value to judge the fitness of curve.
In line 127, code is used to find the mean of cp value.
From line 129 to 131, command is used to find the SSE error from linear equation.
From line 133 to 135, command is used to find the SSR error from linear equation.
In line 137, code is used to find SST value.
From line 139 to 140, code is written to find the value of R square by the help of SSE, SSR and SST.
In line 142, code is written to print the value of R square.
15. From line 150 to 165, code is written to find the error for cubic equation in order to obtain R square value to judge the fitness of curve.
In line 150, code is used to find the mean of cp value.
From line 152 to 154, command is used to find the SSE error from cubic equation.
From line 156 to 158, command is used to find the SSR error from cubic equation.
In line 160, code is used to find SST value.
From line 162 to 163, code is written to find the value of R square by the help of SSE, SSR and SST.
In line 165, code is written to print the value of R square.
PYTHON PROGRAM
"""
curve fitting
"""
#importing of library such as numpy, matplotlib.pyplot, scipy.optimize
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
#curve fit function for linear equation
def func_linear(t, a,b): #defining function for linear equation
return a*t + b #Returning the values to function
#curve fit function for cubic polynomial
def func_cubic(t, a, b, c, d): #defining function for cubic equation
return a*pow(t,3) + b*pow(t,2) + c*t + d #Returning the values to function
#reading thermodynamics data file
def read_file(): #defing function to read the file
temperature = [] #empty array of temperature
cp = [] #empty array of temperature
for line in open('data','r'): #for loop to open and read the file multiple time
#splitting of values from the data file
values = line.split(',')
#storing of the temperature from file to empty array
temperature.append(float(values[0]))
#storing of the cp from file to empty array
cp.append(float(values[1]))
#returning the values of temperature and cp to the function
return [temperature,cp]
#main program
#storing the values of temperature and cp
temperature,cp = read_file()
#curve fitting for linear equation
popt_linear,pcov_linear= curve_fit(func_linear,temperature,cp)
fit_cp_linear = func_linear(np.array(temperature),*popt_linear)
#plotting of figure for linear equation
plt.figure(1) #defining of figure1
plt.plot(temperature,cp,,linewidth=3) #plotting of original data set
plt.plot(temperature,fit_cp_linear,color="red",linewidth=3) #plotting of curve fit data
plt.legend(['actaul data','curve fit for linear']) #defining of curve
plt.xlabel('Temperature [k]') #defining of x axis
plt.ylabel('cp') #defining of y axis
plt.show() #commmand to show the plot
"""
Fitting of curve with original data set without splitting method
"""
#curve fit for cubic polynomial without splitting
popt_cubic,pcov_cubic= curve_fit(func_cubic,temperature,cp)
fit_cp_cubic = func_cubic(np.array(temperature),*popt_cubic)
#plotting of figure2
plt.figure(2) #defining of figure 2
plt.plot(temperature,cp,,linewidth=3) #plotting of original data set
plt.plot(temperature,fit_cp_cubic,color="red",linewidth=3) #plotting of curve fit data
plt.legend(['actaul data','cubic curve fit without splitting method']) #defining of curve
plt.xlabel('Temperature [k]') #defining of x axis
plt.ylabel('cp') #defining of y axis
plt.show() #commmand to show the plot
"""
Fitting of curve with original data set with splitting method
"""
temp1 = temperature[0:400] #splitting of tempereture 0 to 400
temp2 = temperature[401:800] #splitting of tempereture 401 to 800
temp3 = temperature[801:1200] #splitting of tempereture 801 to 1200
temp4 = temperature[1201:1600] #splitting of tempereture 1201 to 1600
temp5 = temperature[1601:3200] #splitting of tempereture 1601 to 3200
cp1 = cp[0:400] #splitting of cp 0 to 400
cp2 = cp[401:800] #splitting of cp 401 to 800
cp3 = cp[801:1200] #splitting of cp 801 to 1200
cp4 = cp[1201:1600] #splitting of cp 1201 to 1600
cp5 = cp[1601:3200] #splitting of cp 1601 to 3200
#curve fit 1 for cubic polynomial with splitting
popt1,pcov1 = curve_fit(func_cubic,temp1,cp1)
fit_cp1 = func_cubic(np.array(temp1),*popt1)
#curve fit 2 for cubic polynomial with splitting
popt2,pcov2 = curve_fit(func_cubic,temp2,cp2)
fit_cp2 = func_cubic(np.array(temp2),*popt2)
#curve fit 3 for cubic polynomial with splitting
popt3,pcov3 = curve_fit(func_cubic,temp3,cp3)
fit_cp3 = func_cubic(np.array(temp3),*popt3)
#curve fit 4 for cubic polynomial with splitting
popt4,pcov4 = curve_fit(func_cubic,temp4,cp4)
fit_cp4 = func_cubic(np.array(temp4),*popt4)
#curve fit 5 for cubic polynomial with splitting
popt5,pcov5 = curve_fit(func_cubic,temp5,cp5)
fit_cp5 = func_cubic(np.array(temp5),*popt5)
#plotting of figure 3
plt.figure(3) #defining of figure 3
plt.plot(temperature,cp,,linewidth=3) #plotting of original data set
plt.plot(temp1,fit_cp1,color="red",linewidth=3) #plotting of curve fit data 1
plt.plot(temp2,fit_cp2,color="red",linewidth=3) #plotting of curve fit data 2
plt.plot(temp3,fit_cp3,color="red",linewidth=3) #plotting of curve fit data 3
plt.plot(temp4,fit_cp4,color="red",linewidth=3) #plotting of curve fit data 4
plt.plot(temp5,fit_cp5,color="red",linewidth=3) #plotting of curve fit data 5
plt.legend(['actaul data','cubic curve fit with splitting method']) #defining of curve
plt.xlabel('Temperature [k]') #defining of x axis
plt.ylabel('cp') #defining of y axis
plt.show() #commmand to show the plot
"""
Calculating of error for linear equation
"""
MEAN = np.mean(cp) #calculating mean of Cp
error1_linear = cp - fit_cp_linear #calculating error 1 for linear eqaution to find SSE
sse_linear = pow(error1_linear,2) #squaring of error
SSE_linear = sum(sse_linear) #calculating SSE error for linear
error2_linear = fit_cp_linear- MEAN #calculating of error 2 for linear eqaution to find SSR
ssr_linear = pow(error2_linear,2) #squaring of error
SSR_linear = sum(ssr_linear) #calculating SSR error for linear
SST_linear = SSR_linear + SSE_linear #calculating SST error for linear
R_linear = pow((SSR_linear/SST_linear),0.5) #calculating R value for linear
R_sq_linear = pow(R_linear,2) #calculating R square value for linear
print('R_sq_linear =',R_sq_linear) #printing of R_square value for linear
"""
Calculating of error for cubic equation
"""
MEAN = np.mean(cp) #calculating mean of Cp
error1_cubic = cp - fit_cp_cubic #calculating error 1 for cubic eqaution to find SSE
sse_cubic = pow(error1_cubic,2) #squaring of error
SSE_cubic = sum(sse_cubic) #calculating SSE error for cubic
error2_cubic = fit_cp_cubic- MEAN #calculating of error 2 for cubic eqaution to find SSR
ssr_cubic = pow(error2_cubic,2) #squaring of error
SSR_cubic = sum(ssr_cubic) #calculating SSR error for cubic
SST_cubic = SSR_cubic + SSE_cubic #calculating SST error for Cubic
R_cubic = pow((SSR_cubic/SST_cubic),0.5) #calculating R value for cubic
R_sq_cubic = pow(R_cubic,2) #calculating R square value for cubic
print('R_sq_cubic =',R_sq_cubic) #printing of R_square value for cubic
How to achieve perfectly fit curve?
In order to achieve perfectly fit curve, we must consider error estimates also.
Perfect fit means, the curve should fit the original data curve without showing any kind of errors.
Note:- Best fit may be perfect fit but perfect fit always best fit. So, in order to obtain perfect fit for cubic polynomial we have to eliminate the possible errors.
But it is still not the best fit. To find the best fit, we must increase the order of the polynomial or we use different technique such as split of data i.e. split wise method.
How to achieve best fit curve?
To find the best fit, we must increase the order of the polynomial or we use different technique such as split of data i.e. split wise method
Here I am not going to increase the order of polynomial because our aim is to obtain best fitting curve for cubic polynomial. So, we will use split wise method to obtain best fit.
ERROR OBTAIN WHILE PROGRAMMMING
- By calling the function with incomplete name the program was not work properly. Instead of func_cubic I wrote func only. By calling the function with right name it will work.
RESULTS
- Curve fit for linear equation
- Curve fit for cubic polynomial without splitting of data
The curve is not perfectly fit, in order to achieve perfectly fit curve, we must consider error estimates also.
To find the best fit, we must increase the order of the polynomial or we use different technique such as split of data i.e. split wise method.
To achieve best fit curve
To find the best fit, we must increase the order of the polynomial or we use different technique such as split of data i.e. split wise method
Here I am not going to increase the order of polynomial because our aim is to obtain best fitting curve for cubic polynomial. So, we will use split wise method to obtain best fit.
- Curve fit for cubic polynomial with splitting of data
- Values of R square for cubic as well as linear
CONCLUSION
- we can obtain the best fit curve by splitting the set of data for cubic polynomial.
- The value of R square near to 1 is consider as a best possible fit of data set.
“popt” = It is the array which contain the optimum values of coefficient for the polynomial.
“pcov” = It is a 2D array that contain the estimated covariance of the parameter in ‘popt’.
“np.array(temperature)” = it is used to create a numpy array that contains the set of data of temperature array.
“*popt” = “ * ” is along with the popt option which is used to provide the values of coefficient to expand the data of values stored in the ‘popt’ array to perform the operation of the function. If we will not use * then it will not take the values of coefficient.
REFRENCES
- Skill lync lectures of PYTHON course