Project

curve fitting using python

Aim:

To write code to fit linear and cubic polynomial curve fit with a comparison of the original data and to plot the linear and cubic fit with raw data.

Curvefit:

The curve fit is the process of constructing the curve or mathematical function which posses the closet proximity to the real series of data points.

By curve fitting, we can mathematically construct the function relationship between observed data and parameters. These involve either interpolation(exact fit to the data) or smoothing(approximate fit to the data). It is a high effect in mathematical modelling of some natural process like forecasting the population of the state/country, or prediction of the company sales over a period by mathematical assumptions.

Note: If there are n polynomials then there are n-1 turning points

  1. what do popt and pcov mean?
  • Curve fit function returns the two functions they are popt and pcov.
  • popt: It returns the coefficients of the given function (eg: a,b,c....etc)
  • pcov: It contains the covariance matrix which indicates the uncertainties and co-relationship between the parameters

2. What does np.array(temperature) do?

  • numpy module is used to perform mathematical operation either on every element of an array or between the arrays. Here, np.array(temperature) will create the array which contains the temperature values of the given data.

3. what does * in *popt mean?

  • * refers to the values of the coefficients that are stored in the \'popt\' array.

4. what needs to be done in order to make curve-fit perfect?

  • We can make a curve fit as a perfect fit by increasing the degree of a polynomial (cubic polynomial is perfect fit when compared to the linear polynomial in the given problem) or by split-wise polynomial i.e splitting the enter data into certain intervals and calculating the coefficients, they are plotted together to get curve fit perfectly.

Code for the linear curve fit:

#program for the linear curve fit

import matplotlib.pyplot as plt 
import numpy as np 
from scipy.optimize import curve_fit
def func(t,a,c):
	return a*t +  c

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 - calculates the coefficeints of the function
pcov -  predictes the values of cp for the values of the coefficents
\"\"\"
popt,pcov = curve_fit(func,temperature,cp)
fit_cp = func(np.array(temperature),*popt)
plt.plot(temperature,cp,color=\'blue\',linewidth=3)
plt.plot(temperature,fit_cp,color = \'red\',linewidth=3)
plt.legend([\'acutal data\',\'curve fit\'])
plt.xlabel(\'temperature[k]\')
plt.ylabel(\'Cp\')
plt.show() 

Result:

Code for the cubic curve fit

# program for the cubic curvefit
import matplotlib.pyplot as plt 
import numpy as np 
from scipy.optimize import curve_fit
def func(t,a,b,c,d):
	return a*pow(t,3) + b*pow(t,2) + c*t + d 

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]

temperature, cp = read_file() # calling the function


popt,pcov = curve_fit(func,temperature,cp)
fit_cp = func(np.array(temperature),*popt)
plt.plot(temperature,cp,color=\'blue\',linewidth=3)
plt.plot(temperature,fit_cp,color = \'red\',linewidth=3)
plt.legend([\'acutal data\',\'curve fit\'])
plt.xlabel(\'temperature[k]\')
plt.ylabel(\'Cp\')
plt.show() 

Result:

Code for the comparison of linear and cubical curve fit:

\"\"\"
code for the comparsion of the cubical and linear curve fit
\"\"\"
import matplotlib.pyplot as plt 
import numpy as np 
from scipy.optimize import curve_fit

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

def fun(t,a,c):
	return a*t +  c

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]

temperature, cp = read_file() # calling the function
\"\"\"
for the cubical curve fit
\"\"\"
popt,pcov = curve_fit(func,temperature,cp)
fit_cp = func(np.array(temperature),*popt)
\"\"\"
for the linear curve fit
\"\"\"
popt,pcov = curve_fit(fun,temperature,cp)
fit_cp_1 = fun(np.array(temperature),*popt)


plt.plot(temperature,cp,color=\'blue\',linewidth=3)
plt.plot(temperature,fit_cp,color = \'red\',linewidth=3)
plt.plot(temperature,fit_cp_1,color = \'black\',linewidth = 3)
plt.legend([\'acutal data\',\'cubical curve fit\',\'linear curve fit\'])
plt.xlabel(\'temperature[k]\')
plt.ylabel(\'Cp\')
plt.show() 


Result:

Conclusion:

The cubical curve fit is approximately close to the original data set when compared to the linear curve 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.