curve fitting for cubic polynominal
Aim: Write code to fit a linear and cubic polynomial for the Cp data.
Introduction:
curve fitting is the process of constructing a curve or function that has the best fit to a series of data points. curve fitting can invovle either interpolation, where an exact fit to the data is requried or smoothing. fitted curves can be used as an aid for data visualization.
Answers:
1] popt and pcov are which are in bulit fuctions under scipy.optimize module for curve fit.
1a] popt: the array that contains best fir parameters for given function which is fitted using non linear LSM(least square method.
1b) pcov: which conatins convariance matrix indicates correlations between parameters.
2] np.array is used is used to convert list of items into array.np.array(temp) which converts of list of temp into array.
3] *popt: will expand a list of values into the arguments of the function.
Program to fit cubical polynominal in to cp-temp relationship data
import math
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
import numpy as np
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(\'data1\',\'r\'):
values=line.split(\',\')
temp.append(float(values[0]))
cp.append(float(values[1]))
return[temp,cp]
temp,cp=read_file()
popt,pocv=curve_fit(func,temp,cp)
fit_cp=func(np.array(temp),*popt)
plt.plot(temp,cp,color=\"blue\",linewidth=3)
plt.plot(temp,fit_cp,color=\"red\",linewidth=3)
plt.legend([\'actal data\',\'curve_fit\'])
plt.xlabel(\'temperature\')
plt.ylabel(\'cp\')
plt.show()
Expalintion
1] import modules(scipy, numpy,matplot).
2] read the data from data1 file and transfer that information to program variables.
3] with help of curve fit function for temp and cp fit the actual and cubical polynomial.
4] after results obtained the for cubic polyniminal model will have less errors and nearer to linear and hence its good. if polynominal increased with higher value the better results will get.
3rd order
4th order
5] to have best curve fit we go for hiher order polynominals which will reduce the deviations from data and to best fits in data.
Conculsion: program is written and excuted for given conditions.