Project

To Perform Curve Fitting using PYTHON

CURVE FITTING

Objective : To write a PYTHON program in order to perform Curve Fitting

Curve fitting is the process of constructing a curve, or mathematical function, that has the best fit to a series of data points, possibly subject to constraints. Curve fitting can involve either interpolations, where an exact fit to the data is required, or smoothing, in which a "smooth" function is constructed that approximately fits the original data. This can also help in visualising the level of uncertanity present in the curve. Fitted curves can be used as an aid for data visualization, to infer values of a function where no data are available, and to summarize the relationships among two or more variables.

Curve fitting serves an important tool in analysing the forecast of any trends from the past data Eg; Population growth.

Polynomials, Quadratics, Cubic, Straight Lines are the most convenient Equations to perform Curve fitting.

  • Straight line
  • Quadratic Equation
  • Cubic Equation

How to make a curve fit perfectly ?

  • A perfect Curve fit refers to that curve which mathematically represents the original data set, independent of the degree of polynomial equation.
  • A perfect Curve fit takes in consideration of most of the points in the data set, and the perfectness of the fit depends on minimization of the error.

How to get the Best fit ?

  • The best fit Comes with no error/zero error after considering the values of entire dataset.
  • The best fit line can be calculated by minimizing the distance between the actual output and the predicted output. There are several methods to do this like sum of squared errors, sum of absolute errors, root mean squared error, etc.
  • difference between the predicted output and the actual output should be least.

Example:

The above figure contains 4 values in its dataset and a curve fitting is performed,

  • Blue line appears to be best fit comparatively than the Red line because, the summation of error ie; the distance between the data points and the fittted blue line is less compared to Red line..
  • The curve which gives the minimum possible error is termed as the best fit.

One such method of arriving to a best fit by minimizing error is Least Square Approach Method

Once the curves are fitted to an dataset, the goodness of the can be evaluated using following techniques,

  1. Sum of Squares due to Errors (SSE)
  2. R-Square
  3. Adjusted R-Square
  4. Root Mean Squared Error (RMS)
  • Sum of Square due to Error

SSE is simply the summation of the Standard Deviations. Lower the SSE, the fit is considered to be Best.

Finding an error in SSE can be related to the example shown in Least Square Approach.

  • R-Square

It is the square of the corelation between the response values and the predicted response values. Higher the value of R-Square better will be the fit.

Where,

SSR = Sum of Squares of Regression

SST = SSR + SSE

R-Square value ranges from 0 to 1. R-Square value 1 indicated that the fit has considered across 100% variation on the dataset from the Mean Average.

R-Squared is a statistical measure of fit that indicates how much variation of a dependent variable is explained by the independent variable(s) in a regression model.

  • Adjusted R-Square

R-Squared only works as intended in a simple linear regression model with one explanatory variable. With a multiple regression made up of several independent variables, the R-Squared must be adjusted. The adjusted R-squared compares the descriptive power of regression models that include diverse numbers of predictors. Adjusted R-Square considers adding variables which are completrly independent, but still related to system in infinitely small manner.

  • Root Mean Squared Error

RMS is the standard deviation of the residuals (prediction errors). Residuals are a measure of how far from the regression line data points are; RMSE is a measure of how spread out these residuals are. In other words, it tells you how concentrated the data is around the line of best fit.

"n" refers to number of points in dataset.

The data used in this program contains 3200 values of Specific Heat of Oxygen at different Temperature.

Goodness of the Fits are evaluated in this program by considering R-Square and RMS vales.

Info

  • What does popt & pcov mean ?
  • They are the Return values of an scipy.optimize module to perform curve fit in pyton.

popt is an array

gives Optimal values for the parameters so that the sum of the squared residuals of f(xdata, *popt) - ydata is minimized

pcov is an 2d array

The estimated covariance of popt. The diagonals provide the variance of the parameter estimate.

  • What does np.array(temperature) do ?
  • np.array is numpy array which has grid of values, all of the same type, and is indexed by a tuple of nonnegative integers. Here it stores the elements present in temperature column.
  • What does the * in *popt mean?
  • popt contains the value of coefficients of equation and gives access to use these variables to find new cp to perform curve fit.

Python Code :

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

n=3200 # number of values in the data set

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

# to read thermodynamic data file
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]



# Quadratic fit
temperature,cp=read_file()
popt,pcov=curve_fit(fun,temperature,cp)
fit_cp=fun(np.array(temperature),*popt)

plt.figure(1)
plt.plot(temperature,cp,color='blue',linewidth=3)
plt.plot(temperature,fit_cp,color='red',linewidth=3)
plt.legend(['Actual Data','Curve Fit'])
plt.xlabel('Temperature [k]')
plt.ylabel('Cp')
plt.title('Quadratic Fit')

mean_cp=statistics.mean(cp)
error_1=cp-fit_cp
sse_1=sum(error_1**2)
x1=[cp-mean_cp for cp in cp]
y1=np.square(x1)
ssr_1=sum(y1)
sst_1=sse_1+ssr_1
rsquare_1=ssr_1/sst_1
rms_1=math.sqrt(sse_1/n)

print("the rsquare value of quadratic fit is",rsquare_1)
print("the RMS value of quadratic fit is",rms_1)

#cubic fit
def fun(t,a,b,c,d):
	return a*pow(t,3)+b*pow(t,2)+c*t+d

temperature,cp=read_file()
popt,pcov=curve_fit(fun,temperature,cp)
fit_cp=fun(np.array(temperature),*popt)

plt.figure(2)
plt.plot(temperature,cp,color='blue',linewidth=3)
plt.plot(temperature,fit_cp,color='red',linewidth=3)
plt.legend(['Actual Data','Curve Fit'])
plt.xlabel('Temperature [k]')
plt.ylabel('Cp')
plt.title('Cubic Fit')

error_2=cp-fit_cp
sse_2=sum(error_2**2)
x2=[cp-mean_cp for cp in cp]
y2=np.square(x2)
ssr_2=sum(y2)
sst_2=sse_2+ssr_2
rsquare_2=ssr_2/sst_2
rms_2=math.sqrt(sse_2/n)

print("the rsquare value of cubic fit is",rsquare_2)
print("the RMS value of cubic fit is",rms_2)


# Linear fit
def fun(t,a,b):
	return a*t+b

temperature,cp=read_file()
popt,pcov=curve_fit(fun,temperature,cp)
fit_cp=fun(np.array(temperature),*popt)

plt.figure(3)
plt.plot(temperature,cp,color='blue',linewidth=3)
plt.plot(temperature,fit_cp,color='red',linewidth=3)
plt.legend(['Actual Data','Curve Fit'])
plt.xlabel('Temperature [k]')
plt.ylabel('Cp')
plt.title('Linear Fit')

error_3=cp-fit_cp
sse_3=sum(error_3**2)
x3=[cp-mean_cp for cp in cp]
y3=np.square(x3)
ssr_3=sum(y3)
sst_3=sse_3+ssr_3
rsquare_3=ssr_3/sst_3
rms_3=math.sqrt(sse_3/n)

print("the rsquare value of linear fit is",rsquare_3)
print("the RMS value of linear fit is",rms_3)


#cubic fit
def fun(t,a,b,c,d,e,f):
	return a*pow(t,5)+b*pow(t,4)+c*pow(t,3)+d*pow(t,2)+e*t+f

temperature,cp=read_file()
popt,pcov=curve_fit(fun,temperature,cp)
fit_cp=fun(np.array(temperature),*popt)

plt.figure(4)
plt.plot(temperature,cp,color='blue',linewidth=3)
plt.plot(temperature,fit_cp,color='red',linewidth=3)
plt.legend(['Actual Data','Curve Fit'])
plt.xlabel('Temperature [k]')
plt.ylabel('Cp')
plt.title('5th Order Polynomial Fit')

error_4=cp-fit_cp
sse_4=sum(error_4**2)
x4=[cp-mean_cp for cp in cp]
y4=np.square(x4)
ssr_4=sum(y4)
sst_4=sse_4+ssr_4
rsquare_4=ssr_4/sst_4
rms_4=math.sqrt(sse_4/n)

print("the rsquare value of polynomial fit is",rsquare_4)
print("the RMS value of polynomial fit is",rms_4)

plt.show()


#To improve the curve fit
# splitting the data set
temp_1=temperature[1:500]
cp_1=cp[1:500]

temp_2=temperature[501:2000]
cp_2=cp[501:2000]

temp_3=temperature[2001:3200]
cp_3=cp[2001:3200]

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

temperature,cp=read_file()
popt,pcov=curve_fit(fun,temp_1,cp_1)
fit_cp1=fun(np.array(temp_1),*popt)

popt,pcov=curve_fit(fun,temp_2,cp_2)
fit_cp2=fun(np.array(temp_2),*popt)

popt,pcov=curve_fit(fun,temp_3,cp_3)
fit_cp3=fun(np.array(temp_3),*popt)

plt.figure(5)
plt.plot(temperature,cp,color='blue',linewidth=3)
plt.plot(temp_1,fit_cp1,color='red',linewidth=2)
plt.plot(temp_2,fit_cp2,color='red',linewidth=2)
plt.plot(temp_3,fit_cp3,color='red',linewidth=2)
plt.legend(['Actual Data','Curve Fit'])
plt.xlabel('Temperature [k]')
plt.ylabel('Cp')
plt.title('Optimized Curve Fit')
plt.show()
  • All the necessary libraries are imported using import comand
  • data file here contains 3200 rows, 2 colums. 1st column is temperature and 2nd column is cp values.
  • A function read_file is called in which an empty array is defined for temperature and cp
  • for every line in data file, it is splitted and respective values are added into arrays using append comand.
  • %curve fit is performed similarly to linear, quadratic, cubic and nth order polynomial.
  • A function is defined for each fit which stores the value according to the formula.
  • temperature & cp are called from read_file function.
  • popt,pcov are the return values which are used to perform curve fit.
  • A new fit_cp is evaluated using the obtained popt value.
  • With the obtained values curve fit are performed using the plot comand.
  • Errors are evaluated with RMS and rsquare tools.

Results

Discussion

From the above results it can be clarified that by increasing the order of polynomial, a best fit can be obtained.

The Best fit should have R-Square value ranging from 0.95 to 1, where in this case Cubic fit (R-Square =0.99) is the Best fit.

By increasing the order of Polynomial equation, the rsquare approaches towards 1, which shows the fitness of the curves, which can be observed in 5th order Polynomial.

To improve the Curve Fit by splitting data set,

The possible solution to improve the Cubic Fit is to split the existing data set in to 2 or more sets, according to feasiblity.

Once the data set is split, use popt and pcov comands to each individual datasets and then plot the curves.

On performing split operation on cubic fit, it has managed to trace the original data set.

References

___________________******************END********************_____________________

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.