Week 5 - Curve fitting
Objective:
Curve fitting is one of the most powerful and most widely used analysis tools in Origin. Curve fitting examines the relationship between one or more predictors (independent variables) and a response variable (dependent variable), with the goal of defining a "best fit" model of the relationship.
Procedure:
1.Import math, matplotlib, scipy, numpy modules
2.Define the function for Quadratic and Cubic expression
3.open() command is used to open the data file and ‘r’ arg to read the data file
4.for loop():Read each and every line in data
5.split() :split the data using arg=’,’
6.Create an empty list for temperature and C_p
6.appned():add each values to an empty list
7.curve_fit:
Syntax : scipy.optimize.curve_fit(f, xdata, ydata, p0=None, sigma=None, absolute_sigma=False, check_finite=True, bounds=(-inf, inf), method=None, jac=None, **kwargs)
- f calling the function
- xdata array like independent variable where the data is measured
- ydata array like dependent variable where the data is measured
- p0 array like, optional, initial guess for the parameters
- sigma, optional, determine the uncertainty in data
- absolute sigma, optional,
- check_finite, optional
- bound tuple of array like, optional
- method, optional to use optimization
- jac :callable ,string or none, optional, it computes the jacobian matrix of the model function
- kwargs keyword arguments
8.array() command from numpy module to use the data in a array format
9.plot() :To craete the 2D plot
10.label():labelling the axis
11.title():Naming to the graph
What does popt and pcov mean?
“popt” is an array that stores the values of the coefficients that are being, passed in a given function.
“pcov” is a 2D array that stores the values of the estimated covariance of popt i.e the coefficients
What does np.array(temperature) do?
Array() command in numpy module is used for passing the values of a temperature as a numpy array
What does the *popt mean?
It refers to the coefficient of a given function
'''
Curve Fitting
'''
import math
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
import numpy as np
#Curve fit Quadratic function
def func(t,a,b,c):
y=a*pow(t,2)+b*t+c
return y
#Curve fit cubicc function
def func1(t,a,b,c,d):
y1=a*pow(t,23)+b*pow(t,2)+c*t+d
return y1
#Reading thermodynamic data file
def read_file():
temperature=[]
c_p=[]
for line in open('data','r'): #open the data file in read format
values=line.split(',') # split each line in data
temperature.append(float(values[0])) # add each value to the list in float format
c_p.append(float(values[1]))
return [temperature, c_p] # store all values into respctive list
# Main program
#Quadratic function
temperature,c_p=read_file() # calling function to store temp and C_p values into a list
popt,pcov=curve_fit(func,temperature,c_p) #curve_fit function from scipy.optimize module,pcov stores estimated co variance
fit_cp1=func(np.array(temperature), *popt) #popt stores the coefficients of a function
#Cubic function
popt,pcov=curve_fit(func1,temperature,c_p) #popt stores the coefficients of a function,pconv stores estimated covariance of popt
fit_cp=func1(np.array(temperature), *popt) # passing temperature as an array
plt.plot(temperature,c_p,color='green',linewidth=2,label='Actual data')
plt.plot(temperature,fit_cp,color='red',linewidth=2,label='Quadratic_data')
plt.plot(temperature,fit_cp1,color='blue',linewidth=2,label='Cubic_data')
plt.xlabel('Temperature [k]')
plt.ylabel('c_p')
plt.title('Temperature Vs c_p')
plt.show()
Results:
The below figure shows the curve fitting using Actual input data,Quadratic function and Cubic function
As observed if increasing the order of the function it gives good_fit