Week 5 - Curve fitting
1. What is Curve fitting?
Curve fitting is the process of constructing a curve, or mathemetical function, that has the best fit to a series of data points possibly subject to constraints.
We are going to plot the curve for specficic heat as a function of temperature, and it will be compared to an actual plot.
2. What is POPT?
POPT is an array with the potimal values for the parameters so the sum of the squared residuals of data is minimised.
What is PCOV?
PCOV is 2d arrat with the estimated covarience of the parameters in POPT. It can be used to calculate the standard dericvation errors in POPT.
3. What does np.array(temperre)atu: do?
The np.array(temperre) : stores the values of temperature in the form of array and passes it to the function numpy.
4. What adoes the * in *POPT mean?
The * in *POPT stores the values of constant parameters used in the function.
CURVE FIT FOR LINEAR EQUATION:
#code
#curve fit for linear equation
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
#curve fit
def func(t,a,b):
return a*t + b
#reading thermodynamic data file
def read_file():
temprature = []
cp = []
for line in open('data', 'r'):
values = line.split(',')
temprature.append(float(values[0]))
cp.append(float(values[1]))
return [temprature,cp]
#program
temperature, cp = read_file()
popt, pcov = curve_fit(func, temperature, cp)
fit_cp = func(np.array(temperature), *popt)
plt.plot(temperature, cp,, linewidth=3)
plt.plot(temperature, fit_cp, color='red',linewidth=3)
plt.xlabel('temperature[k]')
plt.ylabel('cp')
plt.title('Specific Heat Vs Temperature')
plt.legend(['Actual data','Curve fit'])
plt.grid()
plt.show()
OUTPUT:

CURVE FIT FOR QUADRATIC EQUATION:
#code
#curve fit for quadratic equation
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
#curve fit
def func(t,a,b,c):
return a*pow(t,2) + b*t + c
#reading thermodynamic data file
def read_file():
temprature = []
cp = []
for line in open('data', 'r'):
values = line.split(',')
temprature.append(float(values[0]))
cp.append(float(values[1]))
return [temprature,cp]
#program
temperature, cp = read_file()
popt, pcov = curve_fit(func, temperature, cp)
fit_cp = func(np.array(temperature), *popt)
plt.plot(temperature, cp,, linewidth=3)
plt.plot(temperature, fit_cp, color='red',linewidth=3)
plt.xlabel('temperature[k]')
plt.ylabel('cp')
plt.title('Specific Heat Vs Temperature')
plt.legend(['Actual data','Curve fit'])
plt.grid()
plt.show()
OUTPUT:

CURVE FIT FOR CUBIC EQUATION:
#Code
#curve fit for cubic equation
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
#curve fit
def func(t,a,b,c,d):
return a*pow(t,3)+b*pow(t,2)+c*t+d
#reading thermodynamic data file
def read_file():
temprature = []
cp = []
for line in open('data', 'r'):
values = line.split(',')
temprature.append(float(values[0]))
cp.append(float(values[1]))
return [temprature,cp]
#program
temperature, cp = read_file()
popt, pcov = curve_fit(func, temperature, cp)
fit_cp = func(np.array(temperature), *popt)
plt.plot(temperature, cp,, linewidth=3)
plt.plot(temperature, fit_cp, color='red',linewidth=3)
plt.xlabel('temperature[k]')
plt.ylabel('cp')
plt.title('Specific Heat Vs Temperature')
plt.legend(['Actual data','Curve fit'])
plt.grid()
plt.show()
OUTPUT:

COMPARING THE OUTPUTS:
1: LINEAR POLYNOMIAL

2: QUADRATIC POLYNOMIAL

3: CUBIC POLYNIMIAL

CONCLUSION:
It is seen that the curve fits better when the degree of polynomial is increased. IF we consider the deviation for the curve fit w.r.t the actual data, the linear equation gives us more deviation and it decreases as the degree of polynomial is increased. If the degree of polynomial is increased further, i.e. if we consider fourth or fifth order polynomial, then the curve fit will overlap with the actual data.