Curve fitting using python
AIM:
1.To obtain the curve fit for the given data.
2.Program for linear curve fit and cubic curve fit and compare each with original data.
CURVE FITTING:
Curve fitting is the process of constructing a curve, or mathematical functions, which possess the closest proximity to the real series of data. By curve fitting, we can mathematically construct the functional relationship between the observed dataset and parameter values, etc. It is highly effective in the mathematical modeling of some natural processes.
in layman's language curve fitting is the process in which a curve can be made which may very close to the original curve and a mathematical formula can be genrated on the basis of that curve so that it can help to forecast future data.
Answer of some of the asked question is below:
1.What does popt and pcov mean?
popt is the optimal value of the parameters so that some of square residual is minimized.
pcov is a 2d array that contains the estimated covariane of the parameters in popt.
- What does np.array(temperature) do?
it coverts the array data in an array of shape 3200
- What does the * in *popt mean?
it helps to refer the coefficient in an order in a given equation.
4.What needs to be done in order to make the curve fit perfect?
to make the curve perfect degree of polynomila can be increased and also the by dividing the temprature and cp into particular ranges and obtain the curve fit for each can be used to obtain perfect fit.
CODE FOR ORIGINAL DATA VS LINEAR FIT:
import numpy as np
from scipy.optimize import curve_fit
import math
import matplotlib.pyplot as plt
def func(t,a,b,c,d,e,f,g):
return a*pow(t,6) + b*pow(t,5)+ c*pow(t,4) +d*pow(t,3) + e*pow(t,2) + f*t + g #polynomial of 5th degree
def read_file(): # function for reading the file and seprating temprature and cp
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() # calling the function
popt,pcov = curve_fit(func,temp,cp) # finding coefficients
fit_cp = func(np.array(temp),*popt) # new cp based on coefficient
#plotting original vs estimated data
plt.plot(temp,cp,color = 'red', linewidth = 2.5)
plt.plot(temp,fit_cp,color = 'blue',linewidth = 2.5)
plt.xlabel('temprature[k]')
plt.ylabel('cp')
plt.show()
CODE FOR ORIGINAL DATA VS CUBIC FIT:
import numpy as np
from scipy.optimize import curve_fit
import math
import matplotlib.pyplot as plt
def func(t,a,b,c):
return a*pow(t,3) + b*t + c #polynomial of 5th degree
def read_file(): # function for reading the file and seprating temprature and cp
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() # calling the function
popt,pcov = curve_fit(func,temp,cp) # finding coefficients
fit_cp = func(np.array(temp),*popt) # new cp based on coefficient
#plotting original vs estimated data
plt.plot(temp,cp,color = 'red', linewidth = 2.5)
plt.plot(temp,fit_cp,color = 'blue',linewidth = 2.5)
plt.xlabel('temprature[k]')
plt.ylabel('cp')
plt.show()
EXPLANATION OF THE CODE:
1.First of all a function is defiend in which linear equation is defiend having two coefficients a and .
2.Another function is created to read the file and extract the data of cp and temprature by using the open, lines split and float command.
3.Now the function is called and temprature and cp values are stored in their respective variables.
4.Using curve_fit command to find the coefiicients.
- new cp is found by calling function with the help of temprature and already found coefficients.
6.plotting the original vs linear fit data by using plot command.
7.Only equation needs to be changed in case of cubic fit.
ERROR FACED:
- Only typing error was faced and easily tackled by reading the error.
OUTPUT:
linear vs original fit:
original vs cubic fit:
By increasing the degree of polynomial to 8th degree nearly same curve can be obtained as shown below: