SciPy Curve Fitting

OBJECTIVE: To curve fit given data using linear and cubic polynomial.

  1. For curve fitting, we imported curve_fit from scipy library.
  2. Defined two function, func and func1 which are used for fitting curve.
  3. Defined another function read_file to read temperature and cp values.
  4. Values were split using (,) and values were append in array temperature and cp.
  5. Under curve_fit, three parameters was passed,x valuesof graph, yvalues of graph and polynomial function to be used for curve fitting.
  6. fit_cp conatins the y(x)()cp values which will be used for curve fit. X values are temperature values.
  7. popt is the coefficient of polynomial funvtion used for curve fitting.
  8. we are passing temperature as numpy array and also coefficient of function which are obtained by *popt in main polynomial function.
  9. After executing, we can see curve fit for two polynomial.

                                      curve fit using linear polynomial

                                   curve fit using cubic polynomial

Finally to know whether curve fit is good or not, errors were calculated:

  Using these errors SSR,SSE,SST, R^2 and RMSE, we can easily deduce the fit is good or not.

If `R^2`is greater than 0.95, the fit is assumed to be good.

From output and also from graph we can see cubic ploynomial is better than linear polynomial.


#curve fitting
#Luv Kumar
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
#curvefit function linear
def func(t,a,b):
	return a*t + b

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

#read the thermodynamic datafile
def read_file():
	temperature = []
	cp = []
	for line in open('data','r'):
		values = line.split(',')


#main program
temperature,cp = read_file()
popt,pcov = curve_fit(func,temperature,cp)
popt1,pcov1 = curve_fit (func1,temperature,cp)
fit_cp = func(np.array(temperature), *popt)
fitcp1= func1(np.array(temperature), *popt1)
plt.legend(['actual data','curve fit'])
plt.legend(['actual data','curve fit'])
SSE = []
for i in range(0,len(cp)):


mean1 =(np.mean(cp))

for i in range(0,len(cp)):


print("linear fit",RSQUARE)

sse1 = []
for i in range(0,len(cp)):

ssesum1= sum(sse1)
mean2 = (np.mean(cp))
ssr2 = []
for i in range(0,len(cp)):

ssr2sum= sum(ssr2)
rsquare = ssr2sum/sst
print('cubic fit',rsquare)

Projects by LUV KUMAR

OBJECTIVE: To simulate the side crash analysis of the given neon model. STEPS: UNIT SYSTEM: During whole analysis, Kg KN mm ms was followed. INTERFACES: Deleted already defined interfaces and created Type 7 global interface. The properties for the Type 7 interface a Read more

LUV KUMAR · 2019-10-31 04:05:45

OBJECTIVE: To perform a frontal crash simulation using Radioss as a solver. STEPS: View components: In the NEON FRONT model there are 75 components. ASSIGNING MATERIAL ID, PROPERTY ID, AND CARD IMAGE: For rigid bodies, card image is none and prop id, mat id is uns Read more

OBJECTIVE: Studying Interfaces and boundary condition in Hypercrash, Hypermesh and meshing bumper assembly MESHING OF BUMPER ASSEMBLY: Topological error like faces not connected was removed using extend option under surface edit option. Auto mesh under 2D option wa Read more

OBJECTIVE: Through this challenge, we will study different material laws in Radioss and will select the best material law for the given scenario by simulating and plotting graph. STEPS CASE1: Changing the model name from FAILURE_JOHNSON_0000 to Law2_epsmax_failure.R Read more

OBJECTIVE: To compare the results with base simulation and improved shell elements properties by performing energy, mass error checks, plotting graph in HyperGraph and animating in HyperView. SIMULATION 1: With default Shell Properties. Imported Element_Formulati Read more

OBJECTIVE: To clean the geometry, extract mid surface, mesh the extracted surface and assign thickness to given models. LINK TO HYPERMESH FILES   PROCEDURE: Model was imported and to see Read more

OBJECTIVE: Surface mesh and volume mesh generation on given housing and arm bracket model. LINK TO HYPER MESH RADIOSS FILES: 3D MESHING: 3D Elements should be used when all dimensions are compara Read more

LUV KUMAR · 2019-09-13 07:11:16

OBJECTIVE: To find minimum pressure using Newton-Raphson method PROGRAMMING EXPLAINED: Two functions are defined. f and fprime. These function are defined to calculate next iteration value using NR method. To calculate pressure values for different thickness, for a Read more

File Parsing in Python
LUV KUMAR · 2019-09-13 06:25:32

OBJECTIVE: To demonstate file parsing using Python. when we run the program, first thing appears is  to enter the filename,if we speicify correct name then we can see the rest output otherwise a message will show file not found. To store different properties li Read more


The End