Curve fitting using python
Aim:
- To write a python program for curve fitting.
Problem statement:
Program must read the given data files and to sort the tempreature and cp values avaliable in the data files. These values are to be used for futher calculations. plot the actual datas and curve fit in a single plot aganist tempreature vs cp and to dicuss about the obtained results.
To provide explanation for these questions:
1. What does popt and pcov mean?
2. What does np.array(temperature) do?
3. What does the * in *popt mean?
4. Write code to fit a linear and cubic polynomial for the Cp data. Explain if your results are good or bad.
5. What needs to be done in order to make the curve fit perfect?
Program for linear fit:
#Program to plot the quadratic fit
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
file = open('data','r')
def read_file() :
temperature = []
cp =[]
for line in file :
values = line.split(',')
temperature.append(float(values[0]))
cp.append(float(values[1]))
return [temperature,cp]
def func(t,a,b):
return (a*t)+b
temperature,cp = read_file()
popt, pcov = curve_fit(func,temperature,cp)
fit_cp = func(np.array(temperature),*popt)
plt.plot(temperature,cp,linewidth = 3 , color = 'red')
plt.plot(temperature,fit_cp,linewidth = 3 , color = 'blue')
plt.xlabel('temperature in K')
plt.ylabel('specific heat')
plt.legend(['actual fit','linear fit'])
plt.grid()
plt.show()
Output:
Program for quadratic fit:
#Program to plot the quadratic fit
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
file = open('data','r')
def read_file() :
temperature = []
cp =[]
for line in file :
values = line.split(',')
temperature.append(float(values[0]))
cp.append(float(values[1]))
return [temperature,cp]
def func(t,a,b,c):
return a*pow(t,2)+b*t +c
temperature,cp = read_file()
popt, pcov = curve_fit(func,temperature,cp)
fit_cp = func(np.array(temperature),*popt)
plt.plot(temperature,cp,linewidth = 3 , color = 'red')
plt.plot(temperature,fit_cp,linewidth = 3 , color = 'blue')
plt.xlabel('temperature in K')
plt.ylabel('specific heat')
plt.legend(['actual fit','quadratic fit'])
plt.grid()
plt.show()
Output:
Program for cubic 3rd order polynimial:
#Program to plot the quadratic fit
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
file = open('data','r')
def read_file() :
temperature = []
cp =[]
for line in file :
values = line.split(',')
temperature.append(float(values[0]))
cp.append(float(values[1]))
return [temperature,cp]
def func(t,a,b,c,d):
return a*pow(t,3)+b*pow(t,2) +(c*t)+d
temperature,cp = read_file()
popt, pcov = curve_fit(func,temperature,cp)
fit_cp = func(np.array(temperature),*popt)
plt.plot(temperature,cp,linewidth = 3 , color = 'red')
plt.plot(temperature,fit_cp,linewidth = 3 , color = 'blue')
plt.xlabel('temperature in K')
plt.ylabel('specific heat')
plt.legend(['actual fit','quadratic fit'])
plt.grid()
plt.show()
Output:
Program for 4th order polynomial:
#Program to plot the quadratic fit
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
file = open('data','r')
def read_file() :
temperature = []
cp =[]
for line in file :
values = line.split(',')
temperature.append(float(values[0]))
cp.append(float(values[1]))
return [temperature,cp]
def func(t,a,b,c,d,e):
return a*pow(t,4)+b*pow(t,3) +(c*pow(t,2))+(d*t)+e
temperature,cp = read_file()
popt, pcov = curve_fit(func,temperature,cp)
fit_cp = func(np.array(temperature),*popt)
plt.plot(temperature,cp,linewidth = 3 , color = 'red')
plt.plot(temperature,fit_cp,linewidth = 3 , color = 'blue')
plt.xlabel('temperature in K')
plt.ylabel('specific heat')
plt.legend(['actual fit','quadratic fit'])
plt.grid()
plt.show()
Output
Observation:
- The quadratic polynomial is seems to be bad as the curve fit hardly matches with the exact data.
- The cubic polynomial is seems to better result than the qudratic polynomial but looking for more accurate results to produce.
- From the above obtained graphs it is clear that as the maximum order of polynomial increase the the curve fit becomes more accurate to the actual data.
REsults:
Explanation for the arised questions:
- popt is nothing which consits of all the coefficients such as a, b, c, d, e. While calling the function the popt is used as input parameters hence it consits of all the coefficients. Pcov means the convariance matrics, which represents the uncertanities and the correlation between the parameters.
- np refers to numpy library where the array of tempreature obtained from the data file is allowed to passes througth the function. In this program this is used as input parameter for tempreature as it passes all the tempreature values read from the data files is passed throught the 't' parameter.
- The '*' is used with a meaning of "refer". Thus, when the called function gets excutes it needs the value for a, b, c, d, e ... coeffiecients where popt consits these values. so that * symbol tells the solver to get the values for these coefficients from the popt variable.
- Python code forr linear fit and cubic fit is provided, from the obtained plots Linear curve fit is seems to be worst and cubic fit is some what better.
- From the 4th order polynomial plot it becomes clear that to get a perfect fit needs to increase our maximum order of the polynomial as the order of the polynomial increases the accuracy between the actual data and the curve fit gets increases.
Conclusion:
- Python program for curve fit is writeen and its is executed successfully.
- The linear and the cubic fit curves are aslo plotted.
- The better Explanation for the questions also provided.