Project

CURVE FITTING USING PYTHON

1. What does popt and pcov mean?

pcov- it is 2d array which estimated covariance of popt. The diagonals provide the variance of the parameter estimate.

popt - it is an array of Optimal values for the parameters so that the sum of the squared residuals of is minimized.

2. What does np.array(temperature) do?

it passes the temperature as numpy array which then go to the defined function.

3. What does the * in *popt mean?

it shows the values stored in popt.

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 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

After using graphical methods to evaluate the goodness of fit, you should examine the goodness of fit statistics. The Curve Fitting Toolbox supports these goodness of fit statistics for parametric models:

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

FOR CUBIC POLYNOMIAL

# 1 programm to show cubic polynomial curve fitting.

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

#writing curve fitting function
def func(x, a, b, c, d):
    return a * pow(x,3) +b* pow(x,2) + c*x +d

#reading thermodynamics data file
def read_file():
	#creating two array 
	temperature = []
	cp = []

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

	return[temperature,cp]	

 
#Fit for the parameters a, b, c of the function func


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


#plotting


plt.plot(temperature,cp,color=\"blue\",linewidth=4)
plt.plot(temperature,fit_cp,color=\"red\",linewidth=6)
plt.xlabel(\'temperature[k]\')
plt.ylabel(\'cp\') 
plt.show()









 
#2 PROGRAMM ON LINEAR POLYNOMIAL.

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

#writing curve fitting function
def func(x, a, b):
    return a*x+b

#reading thermodynamics data file
def read_file():
	#creating two array 
	temperature = []
	cp = []

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

	return[temperature,cp]	

 
#Fit for the parameters a, b, c of the function func


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


#plotting


plt.plot(temperature,cp,color=\"blue\",linewidth=4)
plt.plot(temperature,fit_cp,color=\"red\",linewidth=6)
plt.xlabel(\'temperature[k]\')
plt.ylabel(\'cp\') 
plt.show()









 

EXPLANATION OF RESULTS:

Here from the plots we find that the curve of higher polynomial fits better as cubic fits better than linear polynomial.

5. What needs to be done in order to make the curve fit perfect?

To make the curve fit perfect we can do following things:

1. break the temperature range into 2 parts and provide polynomial for both the ranges.

2. use higher degree of poynomial for better fitting.

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.