Project

Python Report on: - Code to perform Curve Fitting

Objective: - write a python code to perform curve fitting.

  • We often have a dataset comprising of data following a general path, but each data has a standard deviation which makes them scattered across the line of best fit.
  • scipy is the scientific computing module of python providing inbuilt functions on a lot well known mathematical functions. The scipy.optimize package equips us with multiple optimization procedure.
  • The formulas used for the linear and cubic has been mentioned below: i.e. linear = y = a + bx
    cubic = y = a + bx + cx.^2 + dx.^3

Popt: optimal value for the parameters. so that the sum of squared residuels of f(xdata, popt) - ydata is minimized.

Pcov: The estimated covariance of popt. The diagonals provide the variance of the parameter estimated. To compute one standard deviation error on the parameter use.

np.array(temperature): Numpy array is a general-purpose array processing package. It provides a high-performance multidimensional array object and tools for working with these arrays.

Step 1: Write a code to generate curve fitting for linear and cubic fit.

\"\"\"
Code by Ravi Thakur
Subject: Write a program to plot linear and cubic fit

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

#curve fit function
def func(t,a,b,c):
	return a*pow(t,2) + b*t + c

#reading thermodynamic data
def read_file():
	temperature = []
	cp = []
	for line in open(\'data\',\'r\'):
		Values = line.split(\',\')
		temperature.append(float(Values[0]))
		cp.append(float(Values[1]))
	return [temperature,cp]

#main program
temperature, cp = read_file()
popt, pcov = curve_fit(func,temperature,cp)
fit_cp = func(np.array(temperature), *popt)

pyplot.figure()
pyplot.plot(temperature,cp,color = \"blue\",linewidth = 4)
pyplot.plot(temperature,fit_cp,color=\"red\",linewidth=4)
pyplot.xlabel(\'temperature, [k]\')
pyplot.ylabel(\'cp\')
pyplot.legend([\'actual data\',\'curve fit\'])
pyplot.grid()
pyplot.savefig(\'Graph_1.png\')
pyplot.show()


#curve fit function
def func(t,a,b):
	return a*t + b

#reading thermodynamic data
def read_file():
	temperature = []
	cp = []
	for line in open(\'data\',\'r\'):
		Values = line.split(\',\')
		temperature.append(float(Values[0]))
		cp.append(float(Values[1]))
	return [temperature,cp]

#main program
temperature, cp = read_file()
popt, pcov = curve_fit(func,temperature,cp)
fit_cp_linear = func(np.array(temperature), *popt)

pyplot.figure()
pyplot.plot(temperature,cp,color = \"blue\",linewidth = 4)
pyplot.plot(temperature,fit_cp_linear,color=\"red\",linewidth=4)
pyplot.xlabel(\'temperature, [k]\')
pyplot.ylabel(\'cp\')
pyplot.legend([\'actual data\',\'linear fit\'])
pyplot.grid()
pyplot.savefig(\'Graph_2.png\')
pyplot.show()


#curve fit function
def func(t,a,b,c,d):
	return a*pow(t,3) + b*pow(t,2) + c*t + d

#reading thermodynamic data
def read_file():
	temperature = []
	cp = []
	for line in open(\'data\',\'r\'):
		Values = line.split(\',\')
		temperature.append(float(Values[0]))
		cp.append(float(Values[1]))
	return [temperature,cp]

#main program
temperature, cp = read_file()
popt, pcov = curve_fit(func,temperature,cp)
fit_cp_cubic = func(np.array(temperature), *popt)

pyplot.figure()
pyplot.plot(temperature,cp,color = \"blue\",linewidth = 4)
pyplot.plot(temperature,fit_cp_cubic,color=\"red\",linewidth=4)
pyplot.xlabel(\'temperature, [k]\')
pyplot.ylabel(\'cp\')
pyplot.legend([\'actual data\',\'cubic fit\'])
pyplot.grid()
pyplot.savefig(\'Graph_3.png\')
pyplot.show()

#curve fit function
def func(t,a,b,c,d,e,f,g,h,i):
	return a*pow(t,8) + b*pow(t,7) + c*pow(t,6) + d*pow(t,5) + e*pow(t,4) + f*pow(t,3) + g*pow(t,2) + h*t + i

#reading thermodynamic data
def read_file():
	temperature = []
	cp = []
	for line in open(\'data\',\'r\'):
		Values = line.split(\',\')
		temperature.append(float(Values[0]))
		cp.append(float(Values[1]))
	return [temperature,cp]

#main program
temperature, cp = read_file()
popt, pcov = curve_fit(func,temperature,cp)
fit_cp = func(np.array(temperature), *popt)

pyplot.figure()
pyplot.plot(temperature,cp,color = \"blue\",linewidth = 4)
pyplot.plot(temperature,fit_cp,color=\"red\",linewidth=4)
pyplot.xlabel(\'temperature, [k]\')
pyplot.ylabel(\'cp\')
pyplot.legend([\'actual data\',\'Predicted fit\'])
pyplot.grid()
pyplot.savefig(\'Graph_4.png\')
pyplot.show()

pyplot.figure()
pyplot.plot(temperature,cp,color = \"black\",linewidth = 4)
pyplot.plot(temperature,fit_cp_linear,color=\"red\",linewidth=4)
pyplot.plot(temperature,fit_cp_cubic,color=\"yellow\",linewidth=4)
pyplot.plot(temperature,fit_cp,color=\"blue\",linewidth=4)
pyplot.xlabel(\'temperature, [k]\')
pyplot.ylabel(\'cp\')
pyplot.legend([\'actual data\',\'linear\',\'cubic\',\'Predicted fit\'])
pyplot.grid()
pyplot.savefig(\'Graph_5.png\')
pyplot.show()


Step 2: Results

The plot shows Actual data, Curve fit on Specific heat Vs. Temperature graph.

The plot shows Actual data, Linear fit on Specific heat Vs. Temperature graph.

The plot shows Actual data, Cubic fit on Specific heat Vs. Temperature graph.

The plot shows Actual data, Predicted fit on Specific heat Vs. Temperature graph.

The plot shows All the curve fitting in the same plot of Specific heat Vs. Temperature.

Step 3: Conclusion

  • We have got that goodness fit for the following data is getting 100% fit for only predicted fit for which we have to consider \'a*pow(t,8) + b*pow(t,7) + c*pow(t,6) + d*pow(t,5) + e*pow(t,4) + f*pow(t,3) + g*pow(t,2) + h*t + i\'
    formula.
  • We understood that linear fit gives us 92% accuracy which is not a get fit and cubic fit gives us 98% of accuracy which gives us a good fit.

Step 4: References

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.