Challenge

Week 5 - Curve fitting

1. popt and pcov: The curve_fit function returns two arrays. The first one, popt is a one-dimensional array that contains the coefficients of the determined function from curve_fit. The second array, pcov, is a 2-dimensional array, or a matrix, that contains theestimated covariance of the popt.

Covariance is the variance between the two variables. Thus, popt 2D array contains the covariances of each value (coefficient) in the popt list with the other values in the list that the curve_fit algorithm calculates. The scipy.optimize calculates

these with the least squares. Now, the diagonal of this matrix gives the covariance of a coefficient with itself, which is simply the variance of the coefficient.

And the (ij) and (i,i) values are the same and give the covariance between the two (ij)th or(j,i)th coefficients.

2.np.arrray(temperature) is a function use to take value of array in which temperature data is save

3. *popt

In python, the star operator is used to multiply a number, list or tuples. Also, if the operator is used singly before a list or tuple, it allows a variable number of arguments to pass from calling.

4.

OBJECT : CURVE FITTING OF THE GIVEN DATA AND OBTAINING BEST FIT, PERFECT FIT AND CUBIC FIT .

here we obtain curve related to linear fit ,polynomyalfit and cubic fit of given data file . in order to optimize the curve fit i use inserting data in pieces so that output is optimise.

Here i use curve_fit function of scipy library that use in curve fitting operation,

1.Linear fit:

import numpy as np
import matplotlib.pyplot as plt
from  scipy.optimize import curve_fit
   #defining function for curve fit
def func(t,a,b,c):
   	return  b*t +c
 #reading the data file
 
def read_file():
 	temperature =[ ]
 	cp =[ ]
 	for line in open('data','r'):
 		values = line .split(',')
 		temperature.append(float(values[0]))
 		cp.append(float(values[1]))
 	return [temperature,cp]
#perorming the curve fit operation
temperature,cp=read_file( )
popt,pcov=curve_fit(func,temperature,cp)
fit_cp1=func(np.array(temperature),*popt)
plt.plot(temperature,cp,color="blue", linewidth=10)
plt.plot(temperature,fit_cp1,color="red",linewidth=3)
plt.legend(['actual data','peidcted'])
plt.xlabel('temperature in KELVIN')
plt.ylabel('cp')
plt.grid ()
plt.title('linear curve fit')
plt.show(

plot of the curve:

ERROR CALCULATED:

R_square =

0.9249


RMSE =

25.9991

2.cubic fit

import numpy as np
import matplotlib.pyplot as plt
from  scipy.optimize import curve_fit
   #defining function for curve fit
def func(t,a,b,c,d):
   	return a*pow(t,3) +  b*pow(t,2) +c*t +d
 #reading the data file
 
def read_file():
 	temperature =[ ]
 	cp =[ ]
 	for line in open('data','r'):
 		values = line .split(',')
 		temperature.append(float(values[0]))
 		cp.append(float(values[1]))
 	return [temperature,cp]
#perorming the curve fit operation
temperature,cp=read_file( )
popt,pcov=curve_fit(func,temperature,cp)
fit_cp1=func(np.array(temperature),*popt)
plt.plot(temperature,cp,color="blue", linewidth=10)
plt.plot(temperature,fit_cp1,color="red",linewidth=3)
plt.legend(['actual data','peidcted'])
plt.xlabel('temperature in KELVIN')
plt.ylabel('cp')
plt.grid ()
plt.title('cubic curve fit')
plt.show( )

plot of the curve:

ERROR CALCULATION:

R_square =

0.9967


RMSE =

5.4277

5.in order to obtain pergect fit:

PIECE WISE FIT:

Here i splitted data in 5 pieces and perform curvefitting.

import numpy as np
import matplotlib.pyplot as plt
from  scipy.optimize import curve_fit
   #defining func1tion for curve fit
def func1(t,a,b,c):
   	return a*pow(t,2) +  b*t +c
 #reading the data file
 
def read_file():
 	temperature =[ ]
 	cp =[ ]
 	for line in open('data','r'):
 		values = line .split(',')
 		temperature.append(float(values[0]))
 		cp.append(float(values[1]))
 	return [temperature,cp]
#perorming the curve fit operation
temperature,cp=read_file( )
#first fit
t1=temperature[:700]
cp1=cp[:700]
popt,pcov=curve_fit(func1,t1,cp1)
fit_cp1=func1(np.array(t1),*popt)
#second  fit
t2=temperature[700:1400]
cp2=cp[700:1400]
popt,pcov=curve_fit(func1,t2,cp2)
fit_cp2=func1(np.array(t2),*popt)
#third  fit
t3=temperature[1400:2100]
cp3=cp[1400:2100]
popt,pcov=curve_fit(func1,t3,cp3)
fit_cp3=func1(np.array(t3),*popt)
#fourth fit
t4=temperature[2100:2800]
cp4=cp[2100:2800]
popt,pcov=curve_fit(func1,t4,cp4)
fit_cp4=func1(np.array(t4),*popt)
#fifth fit
t5=temperature[2800:3500]
cp5=cp[2800:3500]
popt,pcov=curve_fit(func1,t5,cp5)
fit_cp5=func1(np.array(t5),*popt)
""""ploting the operation """
plt.plot(temperature,cp,color="blue", linewidth=10)
plt.plot(t1,fit_cp1,color="red",linewidth=3)
plt.plot(t2,fit_cp2,color="green",linewidth=3)
plt.plot(t3,fit_cp3,color="yellow",linewidth=3)
plt.plot(t4,fit_cp4,color="black",linewidth=3)
plt.plot(t5,fit_cp5,color="red",linewidth=3)
plt.legend(['actual data','fit1','fit2','fit3','fit4','fit5'])
plt.xlabel('temperature in KELVIN')
plt.ylabel('cp')
plt.grid ()
plt.title('perfect fit')
plt.show( )

 			

OUTPUT PLOT:

CONCLUSION:


Thus we obtain various fitted curve with respect to given temperature and cp data for OXYGEN;

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.