Project

curve fit using python

Objective:

To find the best fit for the curve which is plotted using data having two parameters, temperature and specific heat at constant pressure

Curve Fitting is used to find the "best fit" line or curve for a series of data points. Most of the time, the curve fit will produce an equation that can be used to find points anywhere along the curve, equation can be linear, polynomial logarithmic or exponential

here we are solving with a polynomial equation

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

#curve fit function
def func(t,a,b,c,d):    #here equation is function of 't',and 'a,b,c,d arecoeficients
    return a*pow(t,3)+b*pow(t,2)+c*t+d

#reading data_file
                      #file contains two columns namely temperature and specific heat 
def read_file():      #at constant pressure i.e., cp
    temperature=[]
    cp=[]
    for line in open('data','r'):                       #reading each line of file
        temperature.append(float(line.split(',')[0]))   #storing the vlues of temperature in temperature araray
        cp.append(float(line.split(',')[1]))            #storing the vlues of cp in araray cp
    return [temperature,cp]

#main program
temperature,cp=read_file()

popt,pcov=curve_fit(func,temperature,cp)
print(popt)
print(pcov)
fit_cp=func(np.array(temperature),*popt)

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

popt,pcov=curve_fit(func,temperature,cp)

It assumes that function 'func' is a function of t with some parameters to adjust and (possibly) an uncertainty sigma (offset).

It returns an array, popt, which contains the optimized parameters,

here, in this case, popt is an array containing the value of the coefficients "a,b,c,d"

pcov is a 2d-array, which you can think of as a matrix. The diagonal contains the variance of the popt, such that we can extract the one standard deviation errors by taking the square root of those elements

This is done to fit the curve more accurately one more thing we can do to fit the curve more appropriately is by using a higher order equation like cubic equations instead of quadratic equation

fit_cp=func(np.array(temperature),*popt)

-we are using this star(*)in *popt, to store all the values of coefficients in popt array , if we do not use this * then it will take only the value of first coefficient

and passing temperature as numpy array will pass the temperature as one dimensional array

Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.