Linear and Cubic Polynomial Curve Fitting by PYTHON programming
Aim:-
To write a code for the fitting of a linear and cubic polynomial by PYTHON programming
Objectives:-
The main objective of this project is to perform curve fitting using PYTHON programming
Brief Theory:-
Curve Fitting:- It is nothing but a method of making a curve (mathematical function) which gives the best fit to a given data(points).
Methods to make a perfect curve fit:-
- Squares sum method
- R square and adjusted R square method
- Root Mean Square Method
Method of getting the best fit:- A fit is called the best fit when there will be no error for taking into consideration.
The four quantities to make perfect fit helps to measure the goodness of the fit and this helps t get the best fit also(Best fit and the perfect fit are not similar).
Improvement of cubic fit:- It depends upon how we are using the higher-order polynomial equations
POPT:-It is an array used stored the values of coefficients to pass in a given function.
PCOV:-It is a two-dimensional array used to store the values of the estimated covariance of the coefficients.
np.array (temperature):- This command is used to pass the temperature values as a numpy array.
*in*POPT :- This is used to refer to the coefficients in the given equation.
Workflow:-
- Datas are collected to get a curve,
- Inputs values taken and datas are differentiated,
- Fitting of polynomial degree is done
- Plotting of a curve is done
Brief content of this project:-
This project has been done by PYTHON programming. The code has been attached below:-
#Linear and Cubic Polynomial Curve Fitting using PYTHON programming
import math
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
import numpy as np
# Function (Linear fit)
def f_linear(t,a,b):
return a*t + b
#Function (Quadratic fit)
def f_quadratic(t,a,b,c):
return a*pow(t,2) + b*t + c
# Function (Cubic fit)
def f_cubic(t,a,b,c,d):
return a*pow(t,3) + b*pow(t,2) + c*t + d
#Reading the thermodynamic data file
def read_file():
temperature = []
cp = []
#using for loop
for line in open('data','r'):
values = line.split(',')
temperature.append(float(values[0]))
cp.append(float(values[1]))
return [temperature,cp]
#curve fitting code
temperature,cp = read_file()
#Linear curve fitting
popt,pcov = curve_fit(f_linear,temperature,cp)
linear_cp = f_linear(np.array(temperature),*popt)
#Quadratic curve fitting
popt,pcov = curve_fit(f_quadratic,temperature,cp)
quadratic_cp = f_quadratic(np.array(temperature),*popt)
#Cubic curve fitting
popt,pcov = curve_fit(f_cubic,temperature,cp)
cubic_cp = f_cubic(np.array(temperature),*popt)
#Plotting of graph for Linear Curve Fit
plt.figure()
plt.plot(temperature,cp,color = 'red',linewidth =3)
plt.plot(temperature,linear_cp,color = 'blue',linewidth =3)
plt.legend(['Original data','Linear curve fit'])
#using xaxis and y axis
plt.xlabel('Temperature')
plt.ylabel('CP')
#saving figure
plt.savefig('Linear.png')
plt.show()
#Plotting of graph for Quadratic Curve Fit
plt.figure()
plt.plot(temperature,cp,color = 'red',linewidth =3)
plt.plot(temperature,quadratic_cp,color = 'green',linewidth =3)
plt.legend(['Original data','Quadratic curve fit'])
#using xaxis and yaxis
plt.xlabel('Temperature')
plt.ylabel('CP')
#saving figure
plt.savefig('Quadratic.png')
plt.show()
#Plotting of graph for cubic Curve Fit
plt.figure()
plt.plot(temperature,cp,color = 'red',linewidth =3)
plt.plot(temperature,cubic_cp,color = 'black',linewidth =3)
plt.legend(['Original data','Cubic curve fit'])
#using xaxis and yaxis
plt.xlabel('Temperature')
plt.ylabel('CP')
#saving figure
plt.savefig('cubic.png')
plt.show()
Algorithm of the program:-
1)Importing of numpy, matplotlib.pyplot and scipy is done
2)function f_linear and f_cubic for linear and cubic polynomial are defined
3)for a loop every temperature and cp value is used
5)POPT & PCOV are used
6)to plot figures figure command is used
7)Xlabel , Ylabel command is used to define the properties of axis and yaxis
8)plot is saved and shown
Graphs:-
1)Linear polynomial curve fitting
2)Quadratic polynomial curve firing:-
3)Cubic polynomial curve fitting:-
Conclusion:-
Through this whole project, e have learned how to code in the PYTHON to fit curve. We have learned the use of curve fitting i.e how we can predict and see the data visualization where no data are available and finally our aim of this project is fulfilled and this coding is very helpful for industrial works to predict something by a program mathematical model.