Project

Curve Fit for Temperature vs Specific Heat using Python

Answers to the questions:-

1. popt= popt contains the values for the coefficients which minimize the sum of squares of the residuals

pcov= pcov is basically an array containing the estimated covariance of the coefficients obtained in popt

2. np.array(temperature) = this command basically converts the list of temperature into a numpy array. This is neccessary because few of the math operations cannot be carried out on lists.

3. The * in *popt basically acts as a reference operator to refer to the coefficients present in popt

4. Program to plot a curve fit for a graph between temperature and specific heat:-

For this challenge , I had to find a mathematical function which can best define the nature of the plotted curve of the given data. For this, the best method is to define a Curve Fit.

Curve Fit is a mathematical modelled function which determines the true nature of a given curve,it can also be used to predict the nature of the curve in the future if it changes according to time. So, a curve fit was used to find the closest possible function that determines the nature of the given curve.

Program:-

First, the neccessary files that are numpy,matplotlib, and scipy were imported. Now, to perform the curve fit, I created two function each of them representing a linear expression and a cubic expression. The parameters to these functions were temperature and coefficicnets to the expressions.

Then another function named \'read\' was created to read the file given to us. The file contains data of temperature with its corresponding cp value, separated by a comma. So, inside the function, the split command was used to separate both the values and store them in diferent arrays.

Now as in for the main program, the read function was called to fetch the values of temperature and cp. Then a statement:-

popt,pcov=curve_fit(linear_func,temp,cp)

was written to fetch the coefficients required for the linear expression. here, the curve fit function from scipy module is used which calculates the coeffcients and stores them in \'popt\', and \'pcov\' stores their covariance values. Then another statement:

fit_cp1 = linear_func(np.array(temp),*popt)

This basically passes the value of the temperature as a numpy array along with the coeffcients stored in *popt as parameters to the function defined and then stores the return value in a variable. These same steps are then repeated for the cubic expression by replacing the linear_func with cubic_func.

Then the obtained values stored in \'fit_cp1\' and \'fit_cp3\' were plotted along with the temperature values. For clarity, this is plotted along with the actual values of temp and cp to get a basic idea of how our curve fit function have performed. The same is done for the cubic plot as well.

The code for the program is given below:

\'\'\'
Program to Perform curve fit 
\'\'\'

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

#curve fit
def linear_func(t,a,b,c,d,p):
		return a*t+b

def cubic_func(t,a,b,c,d):
		return a*pow(t,3)+b*pow(t,2)+c*t+d

#reading the file
def read():
	temp=[]
	cp=[]
	for line in open(\'data\',\'r\'):
		value=line.split(\',\')
		temp.append(float(value[0]))
		cp.append(float(value[1]))
	return [temp,cp]

#main
temp,cp=read()
popt,pcov=curve_fit(linear_func,temp,cp)
fit_cp1 = linear_func(np.array(temp),*popt)

popt,pcov=curve_fit(cubic_func,temp,cp)
fit_cp3 = cubic_func(np.array(temp),*popt)

#plotting linear curve fit

plt.plot(temp,cp,color=\"blue\",linewidth=3)
plt.plot(temp,fit_cp1,color=\'red\',linewidth=3)
plt.legend([\'actual data\',\'curve fit\'])
plt.xlabel(\'Temperature[K]\')
plt.ylabel(\'CP\')
plt.show()


#plotting cubic curve fit

plt.plot(temp,cp,color=\"blue\",linewidth=3)
plt.plot(temp,fit_cp3,color=\'red\',linewidth=3)
plt.legend([\'actual data\',\'curve fit\'])
plt.xlabel(\'Temperature[K]\')
plt.ylabel(\'CP\')
plt.show()


Output:-

Linear curve:-

\"\"

Cubic Fit:-

\"\"

Conclusion:-

The results are really good because the graph clearly show that the cubic curve is better than the linear curve .

5. how to achieve the perfect curve fit:

By finding the right order or degree of the curve,i.e here, if we use the 4th order polynomial we might achieve a much better curve than the cubic one , but if we increase the degree further, it might increase the calculating complexity and we might not get the required resullts.

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.