Project

CURVE FITTING (PYTHON)

AIM: Program in PYTHON to fit a linear and cubic polynomial for the Cp data.

THEORY:

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 interpolation, where an exact fit to the data is required, or smoothing, in which a "smooth" function is constructed that approximately fits the data.

Goodness of Fit Statistics

Upon finding a curve which satisfies the data points there are 4 quantities which help us to measure goodness of fit criteria or how well the equation is representing the data points. They are listed as: -

  • The sum of squares due to error (SSE)
  • R-square
  • Adjusted R-square
  • Root mean squared error (RMSE)

Sum of Squares Due to Error: This statistic measures the total deviation of the response values from the fit to the response values. It is also called the summed square of residuals and is usually labeled as SSE.

Sum of Squares due to Error: `SSE=∑_(i=1)^n(Y(i)-f(x(i)) )^2`

R-Square: This statistic measures how successful the fit is in explaining the variation of the data. Put another way, R-square is the square of the correlation between the response values and the predicted response values. It is also called the square of the multiple correlation coefficient and the coefficient of multiple determination.

R-square is defined as the ratio of the sum of squares of the regression (SSR) and the total sum of squares (SST).

R-square can take on any value between 0 and 1, with a value closer to 1 indicating a better fit.

R-Square: `R^(2 )= (SSR)/(SST)`

Adjusted R-square: The adjusted R-square statistic is generally the best indicator of the fit quality when you add additional coefficients to your model.

The adjusted R-square statistic can take on any value less than or equal to 1, with a value closer to 1 indicating a better fit.

Adjusted R-Square: `R_a^2=1-[((1-R^2 )(n-1))/(n-k-1)]`

Root Mean Squared Error: This statistic is also known as the fit standard error and the standard error of the regression.

A RMSE value closer to 0 indicates a better fit.

Root Mean Squared Error: `RMSE = √((SSE)/n)`

OBJECTIVE:

  1. To write a code to fit a linear and cubic polynomial for the cp data.
  2. Determine the Goodness of Fit.

INPUT:

Data file( Temperature v/s Cp):

https://drive.google.com/file/d/1vWi9Vj3AdQf0gqUK1u9jwS8zg92BHGR0/view

GOVERNING EQUATION:

SSE:

`SSE=∑_(i=1)^n(Y(i)-f(x(i)) )^2`

SSR:

`SSR=∑_(i=1)^n(f(x(i)) - Mean)^2`

SST:

`SST=SSR+SSE`

R-Square:

`R^(2 )= (SSR)/(SST)`

Adjusted R-Square:

`R_a^2=1-[((1-R^2 )(n-1))/(n-k-1)]`

RMSE:

`RMSE = √((SSE)/n)`

where,

n = no. of data points

k = no. of independent variables.

Theoretical Quetions and answers:

Ques: What does popt and pcov mean?

‘popt’ : It is an array of coefficients of polynomials

popt is a one dimensional array of the best estimates for the parameter values, each entry matches the order in the function definition

pcov is the covariance matrix showing the uncertainty and interdependence of each parameter in popt.

Ques: What does np.array(temperature) do?

np.array(temperature) creates an array of temperature from the values stored in data file using numpy module.

Ques: What does the * in *popt mean?

* refers to the coefficients of the polynomial into consideration.

Ques: What needs to be done in order to make the curve fit perfect?

Perfect fit means, the curve should fit original curve without showing any errors. To make a curve fit perfectly the value of R-square should be close to ‘1’ and the value of RMSE should be minimum.

The following can be done by increasing the order of polynomials.

PYTHON PROGRAM:

"""
  CURVE FITTING
  Program to fit a linear and cubic polynomial
"""

# Impoet libraries
import numpy as np 
import matplotlib.pyplot as plt 
from scipy.optimize import curve_fit

# Curve fit functions
def func_1(t, a, b):
	return a*t + b

def func_2(t, a, b, c):
	return a*pow(t,2) + b*t + c

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

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

# Reading 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]
	
# Main program		
temperature, cp = read_file()

# Linear curve fiiting 
popt, pcov = curve_fit(func_1, temperature, cp)
fit_cp1 = func_1(np.array(temperature), *popt)

plt.figure(1)
plt.plot(temperature, cp, color= 'blue', linewidth = 2)
plt.plot(temperature, fit_cp1, color='red', linewidth = 3)
plt.legend(['Actual data', 'Curve fit'])
plt.title('Linear Curve Fitting')
plt.xlabel('Temperature [K]')
plt.ylabel('Cp')
plt.show()

#Cubic Curve Fitting
popt, pcov = curve_fit(func_3, temperature, cp)
fit_cp3 = func_3(np.array(temperature), *popt)

plt.figure(2)
plt.plot(temperature, cp, color= 'blue', linewidth = 2)
plt.plot(temperature, fit_cp3, color='red', linewidth = 3)
plt.legend(['Actual data', 'Curve fit'])
plt.title('Cubic Curve Fitting')
plt.xlabel('Temperature [K]')
plt.ylabel('Cp')
plt.show()

#Biquadratic Curve Fitting
popt, pcov = curve_fit(func_4, temperature, cp)
fit_cp4 = func_4(np.array(temperature), *popt)

plt.figure(3)
plt.plot(temperature, cp, color= 'blue', linewidth = 2)
plt.plot(temperature, fit_cp4, color='red', linewidth = 2)
plt.legend(['Actual data', 'Curve fit'])
plt.title('Biquadratic Curve Fitting')
plt.xlabel('Temperature [K]')
plt.ylabel('Cp')
plt.show()


# Fitness Characteristics Calculation
s = np.sum(cp)
n = np.size(cp)

# Mean
m = s/n

# For Linear curve fit
SSR_lin = 0
SSE_lin = 0


for i in range(n):
	error = abs ((np.sum((cp[i] - fit_cp1[i]))))
	SSE_lin = SSE_lin + pow(error, 2)
	SSR_lin = SSR_lin + (np.sum(pow((fit_cp1[i] - m), 2)))

SST_lin = SSE_lin + SSR_lin
Rsq_lin = SSR_lin/SST_lin
RMSE_lin = pow((SSE_lin/n), 0.5)

print('SST_lin:', SST_lin)
print('Rsq_lin:', Rsq_lin)
print('RMSE_lin:', RMSE_lin)	

# For Cubic Curve Fit 
SSR_cub = 0
SSE_cub = 0 

for i in range(n):
	error = abs ((np.sum((cp[i] - fit_cp3[i]))))
	SSE_cub = SSE_cub + pow(error, 2)
	SSR_cub = SSR_cub + (np.sum(pow((fit_cp3[i] - m), 2)))

SST_cub = SSE_cub + SSR_cub
Rsq_cub = SSR_cub/SST_cub
RMSE_cub = pow((SSE_cub/n),0.5)

print('SST_cub:', SST_cub)
print('Rsq_cub:', Rsq_cub)
print('RMSE_cub:', RMSE_cub)

# For Cubic Curve Fit 
SSR_biquad = 0
SSE_biquad = 0 

for i in range(n):
	error = abs ((np.sum((cp[i] - fit_cp3[i]))))
	SSE_biquad = SSE_biquad + pow(error, 2)
	SSR_biquad = SSR_biquad + (np.sum(pow((fit_cp3[i] - m), 2)))

SST_biquad = SSE_biquad + SSR_biquad
Rsq_biquad = SSR_biquad/SST_biquad
RMSE_biquad = pow((SSE_biquad/n),0.5)

print('SST_biquad:', SST_biquad)
print('Rsq_biquad:', Rsq_biquad)
print('RMSE_biquad:', RMSE_biquad)

EXPLANATION OF THE CODE:

  • Initially, the necessary libraries are imported, and the functions are specified.
  • Then, for reading the provided data file a function is written and empty arrays for temperature and cp are formed.
  • Using popt and pcov commands the linear curve fit is initiated.
  • Graph for the above is plotted and is compared with the original data.
  • Similarly, to improve the curve fitting, curve fit for the Cubic and Biquadratic polynomials are initiated.
  • Using the goodness of fit calculation best fit is chosen and the specification is provided.
  • The fit parameters SST, RMSE, and R square are calculated and the results are mentioned for the comparison.

PLOTS:

As we can see the Linear fit does not satisfy the purpose.

So, we should try using higher order polynomial.

The above cubic fit shows high-accuracy but for a better fit, we should increased th polynomial order.

The results are highly accurate and the value of RMSE is least for the BIQUADRATIC curve. Also, the value of R square is closest to 1.

RESULTS:

The values of SST, RMSE, and R square are obtained as a output in the command window.

CONCLUSION: Curve fitting of the given data is done by suitable methods and techniques to fit the linear and cubic curve.

And the plot to fit linear and cubic polynomial are made.

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.