Curve fitting
- What does popt and pcov means ?
In curve fitting we give a function to fit with which has some constants to ve find out so popt is the array containing the constants reqired for given model to fit with.
While pcov is the covariance matrix which describes how far the data is distributed from the actual data
- What does np.array(temperature) means ?
In python to work with array and to declare it we have to use numpy module which easily helps with the scientific computing and work with the same type of the tuples.
- What does the * in *popt mean?
* in *popt is used to pass all the values in the popt to the function. It is like pointer in the c programming
- Linear curve fit
The code for the linear curve fit is given below
#importing the required module
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
#defining the function
def func(t,a,b):
return a*t + b
#defining the function for reading the file
def readfile():
temp = []
cp = []
#opening the file and reading the data from it line by line
for line in open('data','r'):
value = line.split(',')
temp.append(float(value[0]))
cp.append(float(value[1]))
return [temp,cp]
#read file and save the variable
temp,cp = readfile()
#defining the constant required for the curve fit
popt,pcov = curve_fit(func,temp,cp)
#defining teh new cp with curve fit
fitcp = func(np.array(temp),*popt)
#plotting the data
plt.plot(temp,cp,color="blue",LineWidth=3)
plt.plot(temp,fitcp,color="red",LineWidth=3)
plt.legend(['Aclual data','linear curve fit'])
plt.xlabel('temperature [K]')
plt.ylabel('cp')
plt.show()
Explaination for the linear curve fit
-first we import all the libraries required
-then define our liner function for the cp using temperature which requires some constants in linear curve fit there is two constants a and b.
-then we difine the function to read specific file named 'data' which contains the temperature in one column and co in second column seperated with ' , ' for each line
so we have to make for loop to scan each line in data and we split it wth comma( , ) we store it in the 'value'
now value has two strings one for temperature and other for cp so it need to be converted to the float value and append it into corresponding value and so on and we return this full array of the temperature and cp.
-Now we start our main program from reading file and assigning the temp and cp variable
-then we obtain the constants required for the curve fit according to the model and covariance matrix. curve_fit requires function and data to be fitted with such a model.
-Now that we have gained the constant according to our requred model we can get new cp which call fit cp using defined function here we have to pass the arrau of temperature and constans from popt so we have used *popt
-Now we plot actual data and linear model in same graph which is shown below
- Cubic curve fit
The code for the Cubic curve fit is given below
#importing the required module
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
#defining the function
def func(t,a,b,c,d):
return a*pow(t,3) + b*pow(t,2) + c*t + d
#defining the function for reading the file
def readfile():
temp = []
cp = []
#opening the file and reading the data from it line by line
for line in open('data','r'):
value = line.split(',')
temp.append(float(value[0]))
cp.append(float(value[1]))
return [temp,cp]
#read file and save the variable
temp,cp = readfile()
#defining the constant required for the curve fit
popt,pcov = curve_fit(func,temp,cp)
#defining teh new cp with curve fit
fitcp = func(np.array(temp),*popt)
#plotting the data
plt.plot(temp,cp,color="blue",LineWidth=3)
plt.plot(temp,fitcp,color="red",LineWidth=3)
plt.legend(['Aclual data','cubic curve fit'])
plt.xlabel('temperature [K]')
plt.ylabel('cp')
plt.show()
Explaination for the Cubic curve fit
-first we import all the libraries required
-then define our cubic function for the cp using temperature which requires some constants in cubic curve fit there is four constants a , b , c , d.
-then we difine the function to read specific file named 'data' which contains the temperature in one column and co in second column seperated with ' , ' for each line
so we have to make for loop to scan each line in data and we split it wth comma( , ) we store it in the 'value'
now value has two strings one for temperature and other for cp so it need to be converted to the float value and append it into corresponding value and so on and we return this full array of the temperature and cp.
-Now we start our main program from reading file and assigning the temp and cp variable
-then we obtain the constants required for the curve fit according to the model and covariance matrix. curve_fit requires function and data to be fitted with such a model.
-Now that we have gained the constant according to our requred model we can get new cp which call fit cp using defined function here we have to pass the array of temperature and constans from popt so we have used *popt
-Now we plot actual data and cubic model in same graph which is shown below
- What needs to be done in order to make the curve fit perfect?
In order to make the curve fit perfect we can calculate the R value from the covariance matrix now if this value is greater then the curve fit is more perfect and 0 for the less perfect. in the covariace matrix the diagonal elements give the variance in the constants that we have gained from the curve fit so we can see from that which curve fit is better. covariace matrix for the linear curve fit is 2*2 matrix and covariance matrix fot the cubic model is 4*4 matrix and it's diagonal elements are the variation in each constants.