Week 5 - Curve fitting
1. What does popt and pcov mean?
ANS.
" popt " is an array that stores the values of the coefficients that are being passed in a given function.
" pcov " is a two-dimensional array that stores the values of the estimated covariance of popt i.e the coefficients.
2. What does np.array(temperature) do?
ANS.
The command is used for passing the values of temperature as a numpy array.
3. What does the * in *popt mean?
ANS.
" * " in *popt helps us to refer to the coefficients in a given equation
4. What needs to be done in order to make the curve fit perfect?
ANS.
In order to make a perfect fit, we must consider error estimates as well. A perfect fit means, the curve should fit the original curve without showing any errors (such as centring and scaling errors) in that particular degree of a polynomial.
1.AIM:- Programme for the linear and cubic curve fitting between specific heat(cp)
and temperature.
2.OBJECTIVE:-Compare the curve fitting line between linear, cubic and get the value of the perfect curve fo linear and cubic.
3.BODY:-
FOR LINEAR CURVE
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
def func(t,a,b):
return a*t+b
def read_file():
temp = []
cp = []
for line in open('data','r'):
values = line.split(',')
temp.append(float(values[0]))
cp.append(float(values[1]))
return[temp,cp]
temp,cp = read_file()
popt,pcov = curve_fit(func,temp,cp)
fit_cp = func(np.array(temp),*popt)
plt.plot(temp,cp,color="red",linewidth='3')
plt.plot(temp,fit_cp,color="blue",linewidth='3')
plt.legend(['Actual Data','curve fit data'])
plt.xlabel('temperature[k]')
plt.ylabel('cp')
plt.show()
FOR CUBIC CURVE
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
def func(t,a,b,c,d):
return a*pow(t,3)+ b*pow(t,2) + c*t + d
def read_file():
temp = []
cp = []
for line in open('data','r'):
values = line.split(',')
temp.append(float(values[0]))
cp.append(float(values[1]))
return[temp,cp]
temp,cp = read_file()
popt,pcov = curve_fit(func,temp,cp)
fit_cp = func(np.array(temp),*popt)
plt.plot(temp,cp,color="red",linewidth='3')
plt.plot(temp,fit_cp,color="blue",linewidth='3')
plt.legend(['Actual Data','curve fit data'])
plt.xlabel('temperature[k]')
plt.ylabel('cp')
plt.show()
STEPS TAKEN TO WRITE A PROGRAMME
- First, we have to load data file which consists of temperature vs specific heat data).
- Use the polynomial equation for linear and cubic.
- Denote the first column as temperature and second column as a cp(specific heat).
- Use popt,pcov command for storing the values in an array
- Use plot command for plotting the result for linear and cubic also.
- Also, label it temp and specific heat.
PLOT FOR LINEAR CURVE
PLOT FOR CUBIC CURVE
4.CONCLUSION:-
From here we get a linear and cubic graph which show the result of how actual specific heat and temperature vary