Project

CURVE FITTING USING PYTHON

Aim:

To write code to fit a linear and cubic polynomial for the Cp data

Description:

CURVE FITTING:

Curve fitting is the process of constructing a curve, or mathematical functions, which possess the closest proximity to the real series of data. By curve fitting, we can mathematically construct the functional relationship between the observed dataset and parameter values, etc. It is highly effective in the mathematical modeling of some natural processes.

Procedure:

  • import the modules that are required
  • Then open the data file by using the open command
  • Split the values of the part and store it in an array
  • After importing that first import a linear function as an example as ax+b
  • Then plot the values as a linear fit
  • After that copy the code from function to plotting then add a variable extra and form a cubic equation ax^2+bx+c
  • Then compare the plots and see which suits more perfectly.

Code:

#program to perform curve fit
#programmed by Hariprasad

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

#defining the program for linear fit

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

#array

temperature=[]
c_p=[]

#opening the data file from my folder

a=open('data','r')

#using for loop for storing the values in the empty array

for line in a:
	values=line.split(',')
	temperature.append(float(values[0]))
	c_p.append(float(values[1]))
popt,pcov=curve_fit(linear,temperature,c_p)
predicted_cp1=linear(np.array(temperature),*popt)

#Plotting linear fit 
plt.plot(temperature,c_p,linewidth=3,color='b',label=r'Original data')
plt.plot(temperature,predicted_cp1,linewidth=3,color='r',label=r'Linear Fit')
plt.title('Specific Heat vs Temperature,(Linear Fit)')
plt.xlabel('Temperature',color='red')
plt.ylabel('Specific Heat',color='red')
plt.legend(loc='best')
plt.show()

#defining third-order fit(cubic polynomial fit)

def poly3(t,a,b,c,d):
	return a*pow(t,3)+b*pow(t,2)+c*t+d
popt,pcov=curve_fit(poly3,temperature,c_p)
predicted_cp3=poly3(np.array(temperature),*popt)
 #plotting for cubic fit

plt.figure(2)
plt.plot(temperature,c_p,color='b',linewidth=3,label=r'Original Data')
plt.plot(temperature,predicted_cp3,color='g',linewidth=3,label=r'cubic fit')
plt.legend(loc='best')
plt.title('Specific Heat vs Temperature,(Cubic Fit)')
plt.xlabel('Temperature',color='red')
plt.ylabel('Specific Heat',color='red')
plt.show()

#plotting for linear vs cubic fit vs polynomial fit

plt.figure(3)
plt.plot(temperature,c_p,color='b',linewidth=3,label=r'Original Data')
plt.plot(temperature,predicted_cp3,color='g',linewidth=3,label=r'cubic fit')
plt.plot(temperature,predicted_cp1,linewidth=3,color='r',label=r'Linear Fit')
plt.legend(loc='best')
plt.title('linear fit vs cubic fit vs polynomial fit')
plt.xlabel('Temperature',color='red')
plt.ylabel('Specific Heat',color='red')
plt.show()

Plot:

Linear fit:

Cubic fit:

Linear vs cubic fit:

Observation:

From these plots, the cubic fit is very much good as compared to linear fit

But going into a higher order of polynomials the curve becomes very much good

The following code is attached with the above code and checked, the fit is far far better than the cubic fit,

def poly4(t,a,b,c,d,e):
	return a*pow(t,4)+b*pow(t,3)+c*pow(t,2)+d*t+e
popt,pcov=curve_fit(poly4,temperature,c_p)
predicted_cp4=poly4(np.array(temperature),*popt)


plt.figure(3)
plt.plot(temperature,c_p,color='b',linewidth=3,label=r'Original Data')
plt.plot(temperature,predicted_cp4,color='y',linewidth=3,label=r'polynomial fit')
plt.legend(loc='best')
plt.title('Specific Heat vs Temperature,(polynomial fit)')
plt.xlabel('Temperature',color='red')
plt.ylabel('Specific Heat',color='red')
plt.show()

Plot:

This fit is much better than cubic fit

Ongoing to higher polynomials like x^10 and above that curve fit becomes better.

Result: The fit of the curve is good for cubic fit and the linear fit somewhat seems to be unfit.

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.