Project

Curve Fitting

Problem Statement:

Write a program to perform curve fit for linear and cubic polynomial.

Python Coding for linear Polynomial:

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

# defining function for linear polynomial
def func(t, a, b):
	return a*t + b

# defining function to read the data
def read_file():
	temperature = []
	cp = []
	for line in open(\'data.txt\',\'r\'): # reading file saved local disk
		values = line.split(\',\') # split the lines in data in two column from \',\'
		temperature.append(float(values[0])) # appending values of temperature to array
		cp.append(float(values[1]))	# appending cp values to array
	return (temperature,cp)

# calculating constants a,b using curve fit
temperature,cp = read_file()
popt, pcov = curve_fit(func, temperature, cp)
fit_cp = func(np.array(temperature), *popt)

# calculating goodness of fit
n = len(temperature)
tss = np.sum(pow((cp - np.mean(cp)),2))
rss = np.sum(pow((cp - np.array(fit_cp)),2))
r = (1 - (rss/tss))*100

#print values of parameters and covarience matrix
print(\'The value of parametrs a = \' + \'%.3f\' %(popt[0]) + \' & b = \' + \'%.3f\' %(popt[1])) 
print(\'The Co-varience matrix is:\' + \'\\n\' + str(pcov))

#printing errors associated
perr = np.diag(pcov)
perr = np.sqrt(perr)
print(\"The Values of errors are: \" + \'%.5f\'%perr[0] + \' & \' + \'%.3f\'%perr[1] )

#plotting results
plt.plot(temperature, cp, color=\'blue\' , linewidth=2, label=\'Actual Data\')
plt.plot(temperature, fit_cp, color=\'red\', linewidth=2, label=\'Curve fit\')
plt.legend()
plt.xlabel(\'Temperature [K]\')
plt.ylabel(\'Cp\')
plt.savefig(\'Curve_fit_linear.png\')
print(\'The goodness of fit is = \' + \'%.2f\' %r)
plt.show()		

Stepwise procedure:

  1. Program starts with importing modules. Modules needed for this task are math, matplotlib, numpy and scipy to perform curve fitting operations.
  2. After importing modules, next step is to define function \"func(t,a,b)\" for linear polynomial. The equation for linear ploynimial is; Cp = at + b ______i. where, a & b are the constants Cp & t are Specific Heat & temperature resp.
  3. As the observed values of Cp and temperature are provided and stored in txt file locally, we need to import data in file to python. This task is done by defining another function named \"read_file()\". Under this function, blank array for temperature and Cp are considered. For reading file line to line, a FOR LOOP is considered as below; for line in open(\'data.txt\',\'r\'): where, data.txt is file name containing observed data for Cp & t. \'r\' denotes read only command. It tend python to read each line in data.txt file. Each line in file consists of values t & Cp separated \',\'. We need extract these values separately so that we can strore it in respective array. For this purpose line.split(\',\') function is used which splits line from comma(,). Hence we get separate values of Cp & t which are appended in array where values[0] i.e. first column is values of t and values[1] i.e. second column is values of Cp.
  4. After defining functions, its time now to call this function in order to calculate constants a & b and thereby defining relationship between Cp and t. temperature, cp = read_file()
  5. As we have considered linear polynomial to state the relationship, the equation for linear condition is as steted in pt.1. Putting values of Cp & t from data file, thereby multiple equation will be created. Theory behind it is as belows; if (Xi,Yi) are the co-ordinates lying on line, then eq.i becomes; Yi\' = aXi + b and (Yi - Yi\') represents deviation of observed value Yi from calculated value Yi\' For points (X1,Y1), (X2,Y2).............(Xn,Yn) Y1\' = aX1 + b Y2\' = aX2 + b..................Yn\' = aXn + b let, s = (Y1\'- Y1)² + (Y2\'- Y2)²...........................(Yn\'- Yn)² = (aX1 + b - Y1)² + (aX2 + b - Y2)² + .......(aXn + b -Yn)² For s to be minimum, ds/da = 0 & ds/db = 0 ds/db = 2(aX1 + b - Y1) + 2(aX2 + b - Y2) + ...........2(aXn + b -Yn) by solving we get, aΣX + nb = ΣY __________________________(i) Similarly by solving ds/da = 0, we get aΣX² + bΣX = ΣXY _______________________(ii) by solving eqn. (i) & (ii), we get the values of a & b. All the above mentioned calculations are performed under function curve_fit(). The Curve fit function gives us values of para,eters a & b, as well as co-varience matrix.
  6. popt & pcov: The function curve_fit(func, temperature, cp) gives value of parameters i.e. popt and the covarience matrix i.e. pcov. popt: popt is an array of best estimates for the paramters values i.e a & b in linear regression, each entry matches the order in function defination. pcov: As the name suggests pcov is co-varience matrix i.e. error matrix for the fit parameters. From that we can calculate values of standard deviation of parameters. Standard deviation is nothing but deviation of parameters from its mean value. This deviation can be on positive or negative side, hence its necessary to consider its square values, which is referred as Co-varience. It is denoted by σ². Co-varience matrix in curve fitting shows uncertainty and interdependence of each parameters. The diagonal element of matrix are co-varience or square of standard deviation of fit parameters while non-diagonal elements of matrix shows relationship between the parameters. Lesser are the values of errors, more the slope of fit curve matched with curve obtained from observed data.
  7. Once we get values of parameters a & b, next step is to find out the values of cp_fit, the modified value of cp which will lead to optimize the curve so that it tries to pass through maximum co-ordinates of observed data & depicts trend of observed values more correctly. Following function is used to calculate the fit_cp. fit_cp = func(np.array(temperature) , *popt) To calculate the values of fit_cp, function has to go through each value of temperature and optimised value of a & b i.e. popt. Function of np.array(temperature): As we haved saved the vaues of temperature in an array, temperaure[], to consider each and every value of temperature to calculate fit_cp, function needs to go through each value of temperature stored in array and then calculate value of fit_cp for that particular temperature. np.array(temperature) provides that facility to function to go through each value of temperature in array and calculate manipulated cp or fit_cp. Function of \'*\' in *popt: The * in *popt unpacks the popt array so that two optimized values of parameters becomes second & third argument of functin calculating fit_cp. Unpacking an array allows function to consider values of a & b so that fit_cp can be calculated for each valu of temperature.
  8. After calculating all values, next objective is to check how well the curve obtained from numerical calculation matches with actual curve i.e. goodness of fit. For this method chi-square method is referred. A chi-squre is percentage value of how well the experimental curve matches with actual curve. As per this method; tss = Σ (Y - Ymean)² i.e. in this case it is Σ (cp - cp(mean))² rss = Σ (Y - Y\')² i.e. in this case it is Σ (cp - fit_cp)² And the value of chi sqaure = r = 1 - (rss/tss) * 100% Higher is the percentage of r, closer is the obtained curve matches with actual curve.
  9. After calulating all the values, it is necessary to print all the results so as to note the obseravtions of numerical calculations.
  10. Goodness of fit can also be observed from values of errors i.e standard deviation. Lesser is the value of errors, more the fit is curve. Values of the errors can be obtained by taking square root of diagonal element of pcov matrix or co-varience matrix. Though this method is not as perfect as chi-square but can be used to observe goodness of fit of curve in quick time.
  11. Next step is to plot the actual curve using calues of temprature & cp and Curve Fit using values of temperature & cp_fit.

Results:

  1. Output window prints the values of paramaters a & b, the co-varience matrix, the values of errors and goodness of fit(chi-square). The value of parameters, i.e. popt is; a = 0.099 & b = 995.202. The goodness of fit_curve to actual curve is 92.49%.
  2. Plot of tempereture v/s Cp showing comparisom of actual v/s fit_curve. Above plot shows the red line not matches the actual curve closely and the error values are much larger in these cases.

Python Coding for Cubic Polynomial:

Python Coding:

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

# defining function for cubic polynomial
def func(t, a, b, c, d):
	return (a*pow(t,3) + b*pow(t,2) + c*t + d)

# defining function to read the data
def read_file():
	temperature = []
	cp = []
	for line in open(\'data.txt\', \'r\'):  # reading file saved local disk
		values = line.split(\',\') # split the lines in data in two column from \',\'
		temperature.append(float(values[0])) # appending values of temperature to array
		cp.append(float(values[1]))	 # appending cp values to array
	return(temperature, cp)
		
# calculating constants a,b using curve fit
temperature, cp = read_file()
popt, pcov = curve_fit(func, temperature, cp)
fit_cp = func(np.array(temperature), *popt)

n = len(temperature)
tss = np.sum(pow((cp - np.mean(cp)),2))
rss = np.sum(pow((cp - np.array(fit_cp)),2))
r = (1 - (rss/tss))*100

#printing values of parameters and Co-varience
print(\'The value of a = \' + \'%.8f\' %popt[0] + \' b = \' + \'%.8f\' %popt[1] + \' c = \' + \'%.8f\' %popt[2] + \' d = \' + \'%.8f\' %popt[3])
print(\'The Co-varience matrix is: \\n\' + str(pcov))

perr = np.diag(pcov)
perr = np.sqrt(perr)
print(\'The values of error are: \' ,\'\\n\', perr)

#plotting results
plt.plot(temperature, cp, \'blue\', linewidth = 2)
plt.plot(temperature, fit_cp, \'red\', linewidth = 2)
plt.legend([\'Actual Data\',\'Curve Fit\'])
plt.xlabel(\'Temperature [K]\')
plt.ylabel(\'Cp\')
plt.savefig(\'Curve_fit_cubic.png\')
print(\'The goodness of fit is = \' + \'%.2f\' %r)
plt.show()

Stepwise Procedure:

  1. Procedure for cubic polynomial curev fitting is same as that of linear polynomial. Only difference being the equation of cubic polynomial under consideration; at³+bt²+ct+d Therefore, popt will be an array of values of parameters a, b, c & d. Similarly pcov i.e. co-varience matrix consists of an array of 4x4, where diagonal element consists of square of standard deviation related to parameters a, b, c & d.

Reults:

  1. As in case of linear regression, result of cubic polynomial regression is values of parameters i.e. popt, the covarience matrix or error matrix i.e. pcov, the values of error and percentage of goodness of fit. The result window shows the values of parameters; a,b,c & d. The value of goodness of fit is 99.67%.

2. Plot of temperature v/s Cp: As can be seen from graph curve fit (red curve) nearly matches the actual curve.

Comparison of Linear and Cubic polynomial curve fit:

The comparison paremeters to compare two cases are the values of errors, Goodness of fit as per chi-square method and graph plotted temperature v/s Cp.

Though comparison of error values associated with parameters a, b, c, d is not effective way to compare linear and cubic regression, it can give initial insights of whether the curve form we chosen is reaching near to actual represntation or not. Error values associated with fit parameters are arger in case of linear regression than in case of cubic regression. This indicates that cubic curve is better choice than that of a line.

Second parameter of comparison is goodness of fit values. In this parameter, Cubic curve is clear winner as percentage value of goodness of fit in cubic regression is 99.67% over 92.49% in linear regression.

Thid parameter is graphical representation. As seen from graphs plotted temperature against Cp for both linear as well as cubic polynomial case, curve in later case nearly matches actual curve obtained from observed data than in previous case.

All three parameters suggests that Cubic Polynomial is near accurate representaion of observed data.

Ways to perfect the curve fit:

  1. First way of improving curve fit, which is also observed in above data is to increase the order of polynomial. Results obtained in third order polynomial are far better than first order linear polynomial.
  2. Another way to improve curve fit is to split large data into no of short data. Differential polynomial functions can be considered for no. of short data. This way can lead to fit curve more accurately for short data thereby decreasing error in accumulated results of short data.
  3. Goodness of fit: This is not the direct way to improve the curve fit. It is the quality checking measure after we have consider the polynomial function. It gives values of errors or percentage by which curve fit matches the actual curve. Its a comparison tool which compares result obtained from consideration of curve function and helps in chosing better curve fit. There are different methods for checking Goodness of Fit.
    1. RMS error
    2. Chi-Square(square of error)

Conclusion:

Cubic polynomial fits better to obsereved data than first order polynomial.

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.