Week 5 - Curve fitting
Aim : Write code to fit a linear and cubic polynomial for the Cp data and Plot the linear and cubic fit curves
Theory :
Curve fitting is the process of constructing a curve, or mathematical function, that has the best fit to a series of data points possibly subject to constraints. Curve fitting can involve either interpolation where an exact fit to the data is required, or smoothining in which a "smooth" function is constructed that approximately fits the data.
Most commonly, one fits a function of the form y=f(x).
The first degree polynomial equation

is a line with slope a. A line will connect any two points, so a first degree polynomial equation is an exact fit through any two points with distinct x coordinates.
If the order of the equation is increased to a second degree polynomial, the following results:

This will exactly fit a simple curve to three points.
If the order of the equation is increased to a third degree polynomial, the following is obtained:
-
-

- This will exactly fit four points.
-
Q/A
1. What does popt and pcov mean?
popt array Optimal values for the parameters so that the sum of the squared residuals
pcov 2d arrayThe estimated covariance of popt. The diagonals provide the variance of the parameter estimate. To compute one standard deviation errors on the parameters
How the sigma parameter affects the estimated covariance depends on absolute_sigma argument, as described above.
If the Jacobian matrix at the solution doesn’t have a full rank, then ‘lm’ method returns a matrix filled with np.inf, on the other hand ‘trf’ and ‘dogbox’ methods use Moore-Penrose pseudoinverse to compute the covariance matrix.
2. What does np.array(temperature) do?
A numpy array is a grid of values, all of the same type, and is indexed by a tuple of nonnegative integers it stores the values in the temperature column into an array
3. What does the * in *popt mean?
* is refer the element present the popt and gives all the value in it
4. Write code to fit a linear and cubic polynomial for the Cp data. Explain if your results are good or bad.
python code:
linear fit
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():
temperature = []
cp = []
for line in open('data','r'):
values = line.split(',')
temperature.append(float(values[0]))
cp.append(float(values[1]))
return[temperature,cp]
#main program
temperature,cp = read_file()
popt ,pcov = curve_fit(func,temperature,cp)
fit_cp= func(np.array(temperature),*popt)
plt.plot(temperature,cp,color="blue",linewidth=3)
plt.plot(temperature,fit_cp,color="red",linewidth=3)
plt.legend(['actual data','curve fit'])
plt.xlabel('temperature[K]')
plt.ylabel('cp')
plt.show()
result 1:
cubic fit :
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():
temperature = []
cp = []
for line in open('data','r'):
values = line.split(',')
temperature.append(float(values[0]))
cp.append(float(values[1]))
return[temperature,cp]
#main program
temperature,cp = read_file()
popt ,pcov = curve_fit(func,temperature,cp)
fit_cp= func(np.array(temperature),*popt)
plt.plot(temperature,cp,color="blue",linewidth=3)
plt.plot(temperature,fit_cp,color="red",linewidth=3)
plt.legend(['actual data','curve fit'])
plt.xlabel('temperature[K]')
plt.ylabel('cp')
plt.show()
result 2
5. What needs to be done in order to make the curve fit perfect?
perfect fit means the curve should the fit the original curve without showing errors . so by increasing the value of degree of polynomial ot to achive this we have to use the regression method