Project

CURVE FIT USING PYTHON

CURVE FIT

1.OBJECTIVE:

  • To do a curve fitting for a specific heat v/s temperature data.

2.OBSERVATIONS:

  • popt,pcov = cuve_fit(function,independent variable,dependent variable)
  • where Popt gives the coefficients for a given order of function
  • pcov gives estimated covariance of popt, it is used to estimate the standard deviation errors of a given parameters
  • \'temperature\' is a list containing a sequence of temperature values, hence it gives an entire sequence of temperature values to the function, which is not correct,program throws an error.
  • NP.array(temperature), where \'temperature\' list converted into an array, it gives individual temperature values separately to the function, such that function is executed for each temperature values, and function gives output values equal to the no of temperature values.
  • For example, Popt is an array contains 3 coefficients for a quadratic function when the function is called it has to gives each coefficient, whereas \' Popt\' gives 3 coefficients as single coefficients to the function which is not correct, hence \'*Popt\' is used which gives each coefficient of an array to the function.\'*\' is an element by element operator.

3.PYTHON PROGRAM:

  • At first, modules like \'matplotlib.pyplot\', \'numpy\' and \'curve_fit(from \'scipy.integrate\' module)\' modules are imported.
  • File name termed as \'data\' contains temperature and specific heat values in 2 separate columns.
  • These values are read, split, converted and stored into temperature and specific heat lists.
  • Next function is defined for curve fit according to the degree(linear, quad, cubic).
  • \'Curve_fit\' function is called, it takes function, independent variable, and dependent variable as input and gives co-efficient of that function and also co-variance of coefficients.
  • For the obtained coefficients, new dependent variable is calculated for each value of independent variable using a function
  • The values of actual data and newly obtained data are plotted to see how best the curve_fit is obtained. Best fit means how best the obtained values mimic the actual data.

4.Linear and Cubic fit:

CODE

import matplotlib.pyplot as PLT
import numpy as NP
from scipy.optimize import curve_fit

temperature = []
specific_heat = []

for k in open(\'data\',\'r\'):
	line = k.split(\',\')
	temperature.append(float(line[0]))
	specific_heat.append(float(line[1]))

def linear_function(temperature,a,b):
	return temperature*a+b

def cubic_function(temperature,a,b,c,d,):
	return pow(temperature,3)*a+pow(temperature,2)*b+temperature*c+d

coefficients,coeff_coverience = curve_fit(linear_function,temperature,specific_heat)
fit_cp = linear_function(NP.array(temperature), *coefficients)

coefficients3,coeff_coverience3 = curve_fit(cubic_function,temperature,specific_heat)
fit_cp3 = cubic_function(NP.array(temperature), *coefficients3)

PLT.plot(temperature,specific_heat,color=\'red\',label=\'Actual curve\')
PLT.plot(temperature,fit_cp,color=\'green\',label = \'linear fit\')
PLT.plot(temperature,fit_cp3,color=\'blue\',label = \'cubic fit\')
PLT.xlabel(\'TEMPERATURE\')
PLT.ylabel(\'Specific Heat\')
PLT.legend(loc=\'best\')
PLT.show()

RESULT

  • From the graph, it is evident that the cubic fit gives better curve_fit than the linear fit
  • Hence, increasing the degree of a polynomial of a function gives a better curve fit that it mimics the actual curve best but not perfect.

\"Linear

5.PERFECT FIT:

  • Increasing the degree of polynomials above optimum deviates curve from actual values.
  • The perfect fit means the minimum deviation of obtained values from the actual values (or) how much obtained values are nearer to the actual values.
  • Therefore, the best way to get the perfect curve fit is by splitting the curve and applying a combination of lower degree polynomial function and higher degree polynomial functions to get curve fit at each splits.

CODE

import matplotlib.pyplot as PLT
import numpy as NP
from scipy.optimize import curve_fit

temperature = []
specific_heat = []

for k in open(\'data\',\'r\'):
	line = k.split(\',\')
	temperature.append(float(line[0]))
	specific_heat.append(float(line[1]))

temperature1 = temperature[0:800]
temperature2 = temperature[800:1600]
temperature3 = temperature[1600:2400]
temperature4 = temperature[2400:3200]

specific_heat1 = specific_heat[0:800]
specific_heat2 = specific_heat[800:1600]
specific_heat3 = specific_heat[1600:2400]
specific_heat4 = specific_heat[2400:3200]

def quad_function(temperature,a,b,c):
	return pow(temperature,2)*a+temperature*b+c

def cubic_function(temperature,a,b,c,d,):
	return pow(temperature,3)*a+pow(temperature,2)*b+temperature*c+d

coeff1,coeff_coverience1 = curve_fit(cubic_function,temperature1,specific_heat1)
fit_cp1 = cubic_function(NP.array(temperature1), *coeff1)

coeff2,coeff_coverience2 = curve_fit(cubic_function,temperature2,specific_heat2)
fit_cp2 = cubic_function(NP.array(temperature2), *coeff2)

coeff3,coeff_coverience3 = curve_fit(quad_function,temperature3,specific_heat3)
fit_cp3 = quad_function(NP.array(temperature3), *coeff3)

coeff4,coeff_coverience4 = curve_fit(quad_function,temperature4,specific_heat4)
fit_cp4 = quad_function(NP.array(temperature4), *coeff4)


PLT.plot(temperature,specific_heat,color=\'black\',label=\'Actual curve\',linewidth=3)
PLT.plot(temperature1,fit_cp1,color=\'green\',label = \'cubic fit\')
PLT.plot(temperature2,fit_cp2,color=\'blue\',label = \'cubic fit\')
PLT.plot(temperature3,fit_cp3,color=\'red\',label = \'quad fit\')
PLT.plot(temperature4,fit_cp4,color=\'yellow\',label = \'quad fit\')
PLT.xlabel(\'TEMPERATURE\')
PLT.ylabel(\'Specific Heat\')
PLT.legend(loc=\'best\')
PLT.show()

	

RESULT

  • From the graph it can be seen that by splitting the curve and applying a combination of lower degree polynomial function and higher degree polynomial functions to get curve fit at each splits gives perfect fit.

\"\"

ERRORS

  • Minor errors were encountered during stages of code writing, it is rectified during those stages only.

THANK YOU

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.