Project

Curve fitting over Specific heat vs Temperature data on Python

AIM: Write a python code to fit a linear and cubic polynomial for the given Cp data

CURVE FIT:

Curve fitting is a process of constructing a curve or a mathematical function that has been best fit to the series of data points. By curve fitting, we can obtain the relationship between the observed datasets and the parameter values.

Question1: What does popt and pcov mean?

Answer: popy means Optimal values for the parameters or optimum values of the coefficients of the cubic equation; pcov means the estimated covariance of popt.

Question2: What does np.array(temperature) do?

Answer: Numpy array is a central data structure of the numpy library, which is a numerical library of Python. 'np.array()' is used to create a numpy array and the temperature is passed as the data of the array.

Question3: What does the * in *popt mean?

Answer: *popt unpacks the popt array, so the two optimized parameter values become the second and third arguments to the function.

Question4: What needs to be done in order to make the curve fit perfect?

Answer: Using a polynomial with degree two you can fit the curve to your point. Generally it is not a proper way. It may cause over-fitting. It means that the generalization is not considered here. In other words the error of the chosen model will be high when the model is applied for a new data. Therefore, validation techniques such as cross validation are used to solve this problem. In cross validation, the data is split into k folds (e.g. 10 folds). Then, the first fold is used to validate the model. After that, the next fold is used for validation and so on(i.e divide the data points in subregions and fit curve for each region seperately).

CODE:

import math
import matplotlib.pyplot as plt 
import numpy as np
from scipy.optimize import curve_fit

#linear function
def functionlinear(t,a,b):
	return a*t+b

#cubic function
def functioncubic(t,a,b,c,d):
	return  a*pow(t,3)+b*pow(t,2)+c*t+d	

def read_file():
	temp =[]
	c_p =[]
	for line in open('data','r'):
		values = line.split(',')
		temp.append(float(values[0]))
		c_p.append(float(values[1]))
	return [temp,c_p]

#main program
temp,c_p = read_file()
print(temp,c_p)

#linear fit
popt,pcov = curve_fit(functionlinear,temp,c_p)
fit_cp = functionlinear(np.array(temp),*popt)

#cubic fit
popt,pcov = curve_fit(functioncubic,temp,c_p)
fit_cpcubic = functioncubic(np.array(temp),*popt)
#plotting
plt.plot(temp,c_p,color='r',linewidth=3)
plt.plot(temp,fit_cp,color='b',linewidth=3)
plt.plot(temp,fit_cpcubic,color='g',linewidth=3)
plt.legend(['actualcp','linearfit_cp','cubicfit_cp'])
plt.xlabel('TEMPERATURE')
plt.ylabel('CP')
plt.show()

EXPLANATION

  • Two functions are defined for the linear and cubic polynomial and the respective equations are returned
  • Using another function the file which contains the data is read
  • ' for loop' is used for opening and reading the file. The file is splited with a comma.
  • Empty arrays were created for both cp and the temperature
  • Using 'append' command the values of the cp and the temperature from the file are stored in the respective arrays
  • Then print the temperature and the cp value
  • Curve fit option is available in python. To use it, first, we have to import it from the scipy.optimise
  • Two fit cps were created for both the linear and the cubic function
  • Plot the temperature with the original cp, fit cp for linear and fit cp for cubic
  • The plot will show how the linear and cubic function fits the original data

COMPARISON OF THE LINEAR AND CUBIC WITH THE ORIGINAL DATA:

The graph clearly shows that the cubic polynomial fits perfect with the data than the linear polynomial. Increasing the order of the polynomial will result in a perfect fit.

Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.