## Curve Fitting using Python

OBJECTIVE – To write a program to perform curve fitting.

Curve Fitting is the process of constructing a curve or mathematical functions, which possess the closest proximity to the real series of data. By curve fitting, we can mathematically construct the functional relationship between the observed dataset and parameter values, etc. It is highly effective in the mathematical modelling of some natural processes.

1. Popt – It is the optimum value of the polynomial coefficients.

Pcov -  It is the covariance of the optimum value.It is a 2d array.

1. array(temperature) – It passes the temperature value to the function.
2. *in*popt- Instead of giving values for a,b,c we can pass the popt which contains the values of a,b and c,’*’ is used for referring that opt contains the values of a,b and c.

4.Program to perform curve fit of linear and cubic for the given data:

Explanation-

• Two functions are defined for the linear and cubic polynomial and the respective equations are returned.
• Using another function the file which contains the data is used.
• ‘for’ loop is used for opening and reading the file. The file is split with a comma.
• Empty arrays are created for Cp and temperature.
• Using ‘append’ command the values of the Cp and the temperature from the file are stored in the respective arrays.
• Then print the temperature and the Cp values.
• Using the curve fit option. The two were created for both the linear and cubic function.
• Plot the temperature with the original Cp,fit Cp for linear and cubic function.
• The plot will show how the linear and cubic function fits the original data.
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit

#defining the linear function
def lin_fun(t,a,b):
return a*t + b

#defining the cubic function
def cub_fun(t,a,b,c,d):
return a*pow(t,3)+b*pow(t,2)+c*t+d

#obtaining data from the file and storing in arrays
def file():
temp =[]
Cp = []
for line in open('data', 'r'):
values = line.split(',')
temp.append(float(values[0]))
Cp.append(float(values[1]))

return [temp, Cp]

#Fitting the curve
temp,Cp = file()
popt, pcov = curve_fit(lin_fun, temp, Cp)
lin_Cp = lin_fun(np.array(temp), *popt)

popt, pcov = curve_fit(cub_fun, temp, Cp)
cub_Cp = cub_fun(np.array(temp), *popt)

#plotting
plt.figure()
plt.plot(temp,Cp ,'r',linewidth = 3)
plt.plot(temp,lin_Cp,'b',linewidth = 3)
plt.legend(['Actual data','Linear fit data'])
plt.savefig('lin.png')

plt.figure()
plt.plot(temp,Cp,'r',linewidth =3)
plt.plot(temp,cub_Cp,'b',linewidth = 3)
plt.legend(['Actual data','Cubic fit data'])
plt.savefig('cub.png')


OUTPUT-

The graph clearly shows that cubic polynomial fits perfect with the data than the linear polynomial. So, increasing the order of the polynomial will result in a perfect fit.

### Pipe Flow simulation using Solidworks Habishek Umapathy · 2019-10-23 12:06:35

AIM – To run a pipe flow simulation with an inlet Reynolds number of 100, 1000 and 10000. For each of these cases do the following Place the line probes at 95%, 90%, 85%of the pipe length. Compare the normalized velocity profile at each of these locations. N Read more

### Data Analysis using Python Habishek Umapathy · 2019-08-13 15:39:11

OBJECTIVE – We have to write the script which should take column numbers as the input and plot the respective columns as separate images. The plot labels should be extracted from the file. If there is a request for a plot between column 1(crank angle) and column 8 Read more

### Breaking Ice with Air cushion Vehicle - Find minimum pressure with Newton-Raphson method Habishek Umapathy · 2019-07-29 18:24:30

The Newton Raphson method is an iterative technique that is used to find out the roots of a real valued function. It uses the idea that a continuous and differentiable function can be approximated by a straight-line tangent to it. In this case Newton Raphson method is Read more

### Simulation of Transient behaviour of a Simple Pendulum Habishek Umapathy · 2019-07-15 19:12:38

OBJECTIVE – To write a program to simulate the transient behaviour of a simple pendulum and to create an animation of its motion.   PROCEDURE – In Engineering, ODE is used to describe the transient behavior of a system. A simple example is a pendulum Read more

### Otto Cycle using Python Habishek Umapathy · 2019-07-08 04:43:31

OBJECTIVE - To write a code for P_V diagram of an Otto cycle and calculate thermal efficiency

PROCEDURE –

ASSUMPTIONS

• Initially state variables p1,t1,t3 and gamma a Read more

### Parsing NASA Thermodynamic file Using MATLAB Habishek Umapathy · 2019-06-12 15:23:18

OBJECTIVE - 1. Write a function that extracts the 14 co-efficient and calculates the enthalpy, entropy and specific heats for all the species in the data file. Calculate the molecular weight of each species and display it in the command window. Plot the Cp, Ent Read more

### Genetic Algorithm Using MATLAB Habishek Umapathy · 2019-06-06 16:17:05

OBJECTIVE - Write a code in MATLAB to optimise the stalagmite function and find the global maxima of the function. PROCEDURE- The genetic algorithm is a method for solving both constrained and unconstrained optimization problems that is based on natural sele Read more

### Curve Fitting Using MATLAB Habishek Umapathy · 2019-06-04 07:59:31

OBJECTIVE – To write code to fit a linear and cubic polynomial for the Cp data and plot the linear and cubic fit curves along with the raw data points.   PROCEDURE – Curve Fitting is the process of constructing a curve or mathematical functions, whic Read more