Project

CURVE FITTING BY PYTHON

OBJECTIVE: Write code to fit a linear and cubic polynomial for the data provided

Explanation :

curve fitting lets you describe how the data changes mathematically once you have curve-fit.

with the help of curve fit, we can do an additional operation which might not be possible in the initial data set

eg. Population data, we cannot say what will be the population after 10 years but we can predict it, with a change in population with time

by curve fit, we can assume that the function is a linear, quadratic, cubic, or polynomial

so for a linear polymerization, the graph plotted is :

In this plot, the original dataset values differ from the curve fit as it is linear polymerization values (predicted values)

the curve fit doesn\'t match to the original dataset

when it is a cubic AND liner polymerization the CODE and graph plotted are:

What does popt and pcov mean?

popt : It gives an array with the optimal values for the parameters so that the sum of the squared residuals of data is minimized

pcov : It is a 2d array with the estimated covariance of the parameters in popt.it can be used to calculate the standard deviation errors of popt.

What does np.array(temperature) do?

The function which stores the values of temperature in the form of an array and passes it to the function numpy.

What does the * in *popt mean?

It stores the values of the constant parameters in the array popt.

* is used to call the arguments .instead of calling each coefficient separately,*popt can be used to call all the coefficient in one call

PROCEDURE :

Write the program by giving the initial data which is \'data\'

load the data file and in data it has two colors temperature and specific heat.

split them as temp and cp

to predict the curve according to the data we need an equation of curve to be given ie. ax^2 + b*x +c

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

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

#reading thermodynamics data file
def read_files():
	temperature =[]
	cp = []
	for line in open(\'ae\',\'r\'):
		values = line.split(\',\')
		temperature.append(float(values[0]))
		cp.append(float(values[1]))
	return [temperature ,cp]



# MAIN PROGRAM 
temperature,cp =read_files()
popt, pcov =curve_fit(func,temperature,cp)
fit_cp =func(np.array(temperature),*popt)

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.show()



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

#reading thermodynamics data file
def read_files():
	temperature =[]
	cp = []
	for line in open(\'ae\',\'r\'):
		values = line.split(\',\')
		temperature.append(float(values[0]))
		cp.append(float(values[1]))
	return [temperature ,cp]



# MAIN PROGRAM 
temperature,cp =read_files()
popt, pcov =curve_fit(func,temperature,cp)
fit_cp =func(np.array(temperature),*popt)


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.show()



#curve fit function
def func(t,a,b,c,d,e,f,g,h,i):
	return a*pow(t,8) +b*pow(t,7) +c*pow(t,6)+d*pow(t,5)+e*pow(t,4)+f*pow(t,3)+g*pow(t,2)+h*t+i

#reading thermodynamics data file
def read_files():
	temperature =[]
	cp = []
	for line in open(\'ae\',\'r\'):
		values = line.split(\',\')
		temperature.append(float(values[0]))
		cp.append(float(values[1]))
	return [temperature ,cp]



# MAIN PROGRAM 
temperature,cp =read_files()
popt, pcov =curve_fit(func,temperature,cp)
fit_cp =func(np.array(temperature),*popt)


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.show()


PROGRAM FOR LINEAR, CUBIC AND OCTAVE POLYNOMIAL VALUE

FIG.1 LINEAR POLYNOMIAL VALUES CURVE WITH ORIGINAL DATA

In the fig.1 we can see the curve fit does not match the original database the error between them is more.

FIG.2 CUBIC POLYNOMIAL VALUES CURVE WITH ORIGINAL DATA

In the fig.2 we can see the curve fit try to match with the original database but at some points, it does not match but compare to the liner polynomial value it is good.

FIG.3 OCTAVE POLYNOMIAL VALUES CURVE WITH ORIGINAL DATA

In the above program, we have to change the polynomial value to 8.

the curve we get is much good that than the linear and cubic values curve we get

therefore as we increase the polynomial values we get a perfect fit which matches the original data values

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

We can make the curve fit perfect by changing the values of the polynomial

as we change the values from the liner to octave the fit corresponding to the original data change

so at the 8 polynomial we get a perfect fit to original data

How to get the best fit?

A perfect fit can be the best fit but the best fit cannot be a perfect fit

best-fit means by these we can get close to values and get the best fit

The method is to calculate the root mean square values, R-square, the sum of square due to error, and adjusted R-square

R- SQUARE -This quantity that measures how successful the fit is in explaining the variation of the data

it ranges from the values of 0 to 1

where 0 - worst

1 - best fit

RMSE = This statistic is also known as they fit standard error and the standard error of the regression .it is an estimate of the standard deviation of the random component in the data

these two methods are best to find the best fit of the curve

for calculation of RMSE AND R- square the pdf is provided: https://projects.skill-lync.com/projects/Curve-Fitting-and-criterion-to-choose-best-fit-41860

CONCLUSION :

  • CREATED CURVE FIT ACCORDING TO THE LINEAR AND CUBIC VALUES
  • FOUND BEST FIT, PERFECT FIT
  • METHODS TO CALCLULE BEST FIT
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.