## Curve Fitting for Temperature and specific heat values using Python

OBJECTIVE

The key objective of writing this program is to perform curve fitting with the help of a linear and cubic polynomial for the Temp. & Cp data.

#1. What does popt and pcov mean?

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

#3. What does the * in *popt mean?

#4. Write code to fit a linear and cubic polynomial for the Cp data?

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

Explanation

Curve fitting used to find the "best fit" line or curve for a series of data points.
Process of constructing a curve or mathematical functions that gives the closest proximity to the real series of data. It is highly effective in mathematical modeling for some processes.

#1. popt An array of optimal values for the parameters which minimize the sum of squares of residuals pcov-2d array which contains the estimated covariance of popt. The diagonals provide the variance of the parameter estimate. And, the curve fit function returns two values, popt and pcov popt returns the value if co-efficient of the polynomial used and pcos refers to the co-variance of these coefficients.

#2. np.array(temperature) it is an array to store values of different temperature. Basically, it saves all the values of temperature in an array from the sequence, so all the values can be called for each value of cp at every temperature.

#3. *popt is used to refer to the coefficient values from the array. The * is used to call the arguments. Instead of calling each coefficient separately, *popt can be used to call all the coefficient in one instance.

#4. PROCEDURE

1. We have cp vs temp. data file.

2. Open a sublime file save it with a proper name that having .py extension.

3. Import all the necessary modules;

• import matplotlib.pyplot as plt
This module helps us to plot the graph of the given result.
• import math
This module helps us to use mathematical functions like pie, sine, cos, etc.
• import numpy
This module helps us to do mathematical operations on an element of an array.
Whenever we want to define more than one value in a single variable we have to use an array and numpy.linspace is also the same thing it stores value in a range like this; numpy.linspace (lower limit, upper limit, no. of division)
This works acc. to a formula:
Size of division = (upper limit-lower limit)/(no. of division-1)
• From scipy.integrate import odeint
Odeint function which integrates the system of ODE.

3. Use an empty array for storing a value that we get from FOR loop.

4. Append is a command that will continuously append the value of a variable. It keeps on adding the values into an existing array.

5. Define the function: We can call the function whenever and wherever we required.

def func(t,a,b):  here t is a variable and a, b is constant in linear equation.

Create the FOR loop for reading the file which we have in our database by the name of 'Data' which contains temperature and specific heat values. we append those all values into an empty array of name temperature and cp.

6. The main program for the linear function

we call the function  read_file() and here we use popt (function gives the value) & pcov (function gives the square matrix).

fit_cp is a new array to stores the value of new specific heat.
Here *popt helps to make arguments from an array.

Now plot the curve for the linear equation by using plt.plot command and mark labels also for that curve. We can change the color and linewidth of our curve also.

7. Done it the same thing for the cubic equation: define a new function and name them accordingly and make for loop and call that function for work. Plot the curve for the cubic equation and see the difference.

8. Now we find the error in both linear and cubic separately. For that, we take some formulas for our given data. For choosing the best fit.

SSE = the sum of square due to errors

R^2= SSR/SST
R Square measures how successfully the fit is explaining the variation of data. it ranges from 0 to 1.
0 is the worst possible fit for the data set
1 is the best possible fit for the data set

As a result, we give only R^2 & RMSE values by which we can define which curve is best suited for us.

Now plot the graph for the cubic equation and observe what results we get.

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

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

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

# Main program for linear function
popt, pcov = curve_fit(func, temperature, cp)
fit_cp = func(np.array(temperature), *popt)  			# here * helps to make arguments from array

plt.plot(temperature,cp, color='blue' ,linewidth = '3')   		 #ideal curve we get
plt.plot(temperature,fit_cp, color = 'red' , linewidth= '3')	#actual curve we get
plt.legend(['actual data' , 'curve fit'])
plt.xlabel('temperature [k]')
plt.ylabel('cp')
#plt.show()

############
#curve fit cubic funtion

def func3(t,a,b,c,d):			# 3 denotes cubic
return a*pow(t,3)+b*pow(t,2)+c*t+d

#Main program for cubic function
popt3, pcov3 = curve_fit(func3,temperature,cp)
fit_cp3 = func3(np.array(temperature), *popt3)

plt.plot(temperature,cp, color='blue' ,linewidth = '3')
plt.plot(temperature,fit_cp3, color='red' ,linewidth = '3')
plt.legend(['actual data' , 'curve fit'])
plt.xlabel('temperature [k]')
plt.ylabel('cp')
#plt.show()

#print(len(temperature))

#----------ERROR IN LINEAR

##for SSR
j= np.mean(cp)
#print(j)

jarray=[j] * len(fit_cp)
#print(jarray)
sub = (fit_cp - j)
subsq= np.square(sub)
SSR= sum(subsq)
#print(SSR)

##for SSE
sub1= (cp - fit_cp)
sqsub1= np.square(sub1)
SSE= sum(sqsub1)
#print(SSE)

SST = SSR +SSE
#print(SST)

Rsq = SSR /SST
print(Rsq)

RMSE = math.sqrt(SSE/len(fit_cp))
print(RMSE)

#----------ERROR IN Cubic

sub3 = (fit_cp3 - j)
subsq3= np.square(sub3)
SSR3= sum(subsq3)
#print(SSR3)

##for SSE
sub1_3= (cp - fit_cp3)
sqsub1_3= np.square(sub1_3)
SSE3= sum(sqsub1_3)
#print(SSE3)

SST3 = SSR3 +SSE3
#print(SST3)

Rsq3 = SSR3 /SST3
print(Rsq3)

RMSE3 = math.sqrt(SSE3/len(fit_cp3))
print(RMSE3)

#print(pcov)
#print(pcov3)

Curve fit for the Linear polynomial and observe what results in we get.

Curve fit for the cubic polynomial and observe what results in we get.

CONCLUSION

1. As the seen plot of the linear and cubic polynomial, the cubic polynomial is closest to the function curve.

2. #5. In order to make or have a better curve fit we have to use higher-order polynomial which is used to fit closest to the function curve (reduces the deviation from data). Therefore we can say that polynomial of higher degree provides a better curve fit.

3. Here we learn how to plot a graph from the given data file which is in our database.

### Neon Side Crash Analysis on BIW model using Hypermesh RADIOSS Jitesh Sahjwani · 2019-10-31 05:18:23

OBJECTIVE In this challenge, we check and create the properties for a Neon side crash-BIW model and run the simulation & observe results. Question: Neon side crash -BIW Check the unit system and either follow [Mg mm s] or [Kg mm ms]. Create an appropriate int Read more

### Neon frontal crash Analysis on BIW model using Hypermesh RADIOSS Jitesh Sahjwani · 2019-10-31 05:16:57

OBJECTIVE In this challenge, we check and create the properties for a Neon frontal crash-BIW model and run the simulation & observe results. Question: Frontal crash-BIW Check the unit system and either follow[Mg mm s] or [Kg mm ms]. Create an appropriate inte Read more

### Analysis on the automotive Crash box using contact interface Type-7 simulation Type-11 along with Type-7 using Hypercrash and Radioss Jitesh Sahjwani · 2019-10-21 05:15:32

OBJECTIVE In this report, we mesh a Bumper assembly.Perform simulation on Crash tube and compare the Type-7 simulation results, Type-11 along with Type-7 and different kinematic condition simulation results. Create six different cases  Run the crash tube model Read more

### Analysis of failure behavior of a plate with 3 different materials using Hypermesh Radioss Jitesh Sahjwani · 2019-09-23 19:06:35

OBJECTIVE The main objective is to analyze and compare the failure behavior of a plate using different material models LAW-1, LAW-2, LAW-27 & LAW-36 along with Johnson-cook Failure card. For LAW-2, Once use Johnson\'s Failure card along with some certain p Read more

### Comparison of Simulation results and Improve Shell Property of rail model using Hypermesh RADIOSS Jitesh Sahjwani · 2019-09-20 14:57:35

OBJECTIVE Using the crash beam file, Comparison of simulation results of the base setup and improved shell element properties. Questions: 1.Using the crash beam file and change the run time to 55 ms. 2. Change the number of animation steps during simulation to a min Read more

### Crash Worthiness Analysis of Housing and Arm Bracket using Hypermesh Radioss Jitesh Sahjwani · 2019-09-20 04:21:10

Crash Worthiness analysis of housing and arm bracket using Radioss Hypermesh OBJECTIVE The key objective is to create the 2D and 3D meshing for the given models and solve the following questions PROCEDURE & Answers 1. Material property for steel we can ch Read more

### Geometry Cleanup and Mesh on 2D SHELL Elements Using Hypermesh Radioss Jitesh Sahjwani · 2019-09-17 07:48:47

Google drive link for Hypermesh files: https://drive.google.com/open?id=1MDhngswNUUJFrXmWwMTR4jfR0R9T6PKd   OBJECTIVE The key objective is to carry out the geometry cleanup operation & ignore holes less than dia 5mm, extract mid surface and create a 2D mesh Read more

### Data analysis File parsing script using Python Jitesh Sahjwani · 2019-09-14 04:40:04

Google drive link for the \'Engine_data.out\' file: https://drive.google.com/open?id=1lIh6RdUZRex9klgtDhpQ0tmtRTBY11YC   OBJECTIVE The primary aim is to create a data visualizer tool using python and perform certain things by this code is: The scri Read more

### Constraint Minimization of Equation using lagrange's method Jitesh Sahjwani · 2019-09-11 06:59:11

OBJECTIVE The main objective is to Minimize the given equation with respect to the given constraint. Problem: This function f(x,y) = 5-(x-2)^2 -2(y-1)^2 is subject to the following constraint x + 4y = 3 Solution: Make a new function as `g(x,y) = Read more