Curve Fitting of Linear and Cubic Polynomial Equation using Python

Objective : 

    The main aim of the task is to perform a curve fit for linear and cubic polynomials equation and to find which curve fits the best to the actual data curve.

Explanation :

           The main aim of the curve fit is to show accurate result depending upon the dependent and independent parameters which are Temperature and specific heat capacity(cp) and to compare it with the actual data curve.

         The solution to the above problem can be solved using.

  • Temperature and cp values are known.
  • Import numpy, matplotlib.pyplot , curve_fit from scipy.optimize.
  • The function is defined representing the polynomial function, whether it can be a linear or cubic polynomial equation or any degree of polynomials.
  • Now open the file which contains the data of temperature and cp values using the open command and the file which is to be imported must be in the current python location.
  • Now to create a p_fit (cp values) which changes corresponding to the polynomial equations.
  • Thus the values are stored using the split command in the different empty array such as cp and temperature.
  • Now append all the values of ct and temperature in ct array and temperature array.
  • P_fit = func(np.array(temperature),*popt)
  • The above formula helps to find the p_fit which helps to show the actual curve fit with given data points. this is for the linear equation.
  • Same for the cubic equation, p_fit1=function(np.array(temperature)*popt1)

While performing the above function the constant values such as a,b,c,d get stored in popt, whereas Popc is the covariance which determines the data are directly proportional or inversely proportional to each other.covariance refers to the measure of the directional relationship between two random variables.

np.array(temperature) determines the whole temperature values that are stored in a temperature array

The popt contains all the factors such as (a,b,c,d) which influence the curve. 

  • Thus the graph is plotted between the temperature and pfit1(linear)
  • Another graph is plotted between the temperature and pfit2(cubic)
  • The value of r^2 is also calculated for the given points.
  • The value of r^2 closet to the value of 100, that good the curve is.
  • It is done for both the linear curve as well as the cubic curve.











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

def func(t,a,b):
	F = a*t+b
	return F

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

def store_data():
	temperature = []
	cp = []
	for digit in open('data','r'):
		values = digit.split(',')
		temperature.append(float(values [0]))
		cp.append(float(values [1]))

temperature,cp = store_data()
popt,pcov =curve_fit(func,temperature,cp)
p_fit = func(np.array(temperature),*popt)

popt1,pcov1 = curve_fit(function,temperature,cp)
p_fit1 = function(np.array(temperature),*popt1)

plt.plot(temperature,p_fit, color='red',linewidth='3')
plt.plot(temperature,cp, color=  'blue', linewidth='3')

plt.plot(temperature,p_fit1, color='red', linewidth='3')
plt.plot(temperature,cp, color = 'blue' , linewidth='3')
plt.legend(['Actual data fit','Curve fit'])

w=[q] * len(p_fit)

y = cp-p_fit
u =np.square(y)

j = o+t
g = ((t)/(j)) * 100

k =p_fit1 - w
l = np.square(k)
z = sum(l)
x = cp - p_fit1
c = np.square(x)
v = sum(c)
b=v +z
m = 100*(z/b)

The higher the polynomial, the good the fit of the curve is. But it leads other different errors 


  • thus the curve fit for the linear and cubic polynomial equation is executed successfully.
  • The value of r^2 is determined and found that cubic polynomial equation fits the curve more precisely.

Projects by Sachin M

SIDE CRASH OF CHRYSLER NEON The Chrysler Neon is a front-engine, front-wheel-drive compact car introduced in January 1994 for the model year 1995 by Chrysler\'s Dodge and Plymouth divisions in two and four-door body styles over two generations. BIW : BIW (Body in Whi Read more

FRONTAL CRASH OF CHRYSLER NEON The Chrysler Neon is a front-engine, front-wheel-drive compact car introduced in January 1994 for the model year 1995 by Chrysler\'s Dodge and Plymouth divisions in two and four-door body styles over two generations. BIW : BIW (Body in Read more

Objective :   To mesh the given bumper model using Hypermesh and simulate crash tube with different contact interface type using Radioss. Explanation : The given Bumper is meshed under given conditions.                 Read more

1.Objective :    To compare the simulation of different cases by assigning different laws and also observing the corresponding behaviour of the material for the given model. 2.Explanation: Regarding files such as 0000.rad and 0001.rad files are required t Read more

1.Objectives:      To compare the result of the base simulation and improve shell element properties. 2.Explanation :   2.1 Simulation 1 : First, 0000.rad file is imported to the Hypermesh. To get a minimum of 25 animation files, frequency is Read more

Objective:     The aim of this project is to do a geometry clean up for given models and perform 2D mesh using the midplane surface extract for the given parameters for the shell element. Procedure: Import the given model to Hypermesh --- Radioss. The Rad Read more

File Parsing using Python
Sachin M · 2019-09-14 04:42:56

Objective :    The aim of this project is do file parsing using python for different data provided in the file. Explanation:    File Parsing :         Text parsing is a common programming task that splits the given s Read more

Objective:   The main objective the process is to mesh the given hood model using ansa and do the hemming process at last. Hemming:    The hemming process is used in many automation where it helps to join two different surfaces with different material Read more

`f(x) = 5-(x-2)^2 - 2(y-1)^2`        `5 - x^2-4+4x-2y^2-2+4y = 0`        `-x^2-2y^2+4x+4y-1=0`          g(x)=f(x)+λ `g(x) = -x^2-2y^2+4x+4y-1 + λ (x+4y-3)`   `(del g)/(d Read more


The End