Project

Linear Cubic Function Curve Fit

1. What does popt and pcov mean?

Ans:

The curve fit function returns two items, which we are popt and pcov.

lets assume func = ax + b

1) popt stands for optimal paramaters,

The popt argument are the best-fit paramters for coefficient(a,b) of a function,

the values of popt are not exact because there are only a limited number of random samples,

2) pcov stand for covariance parameter

The pcov variable contains the covariance matrix, which indicates the uncertainties and correlations between parameters. This is mostly useful when the data has uncertainties.

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

we are calling a temperature array to insert it in function = a*t^2 + b*t +c

it as an array of temperature used to calculate curve fit.

as we have popt & temperature array we can calculate fit for cp using func = a*t^2 + b*t +c

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

# Curve fit function
def func1(t, a, b): # for Linear curve fit
	return a*t + b

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

# Reading Thermodynamics data file
def read_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]

# Main Program

temp, cp = read_file()
# curve fit for Linear Function
popt, pcov = curve_fit(func1, temp, cp)
fit_cp_1 = func1(np.array(temp),*popt)

# curve fit for Cubic Function
popt, pcov = curve_fit(func3, temp, cp)
fit_cp_3 = func3(np.array(temp),*popt)

# plotting 
plt.subplot(211)
plt.title(\'Curve Fit For linear Function\')
plt.plot(temp, cp, color = \"blue\", linewidth = 3)
plt.plot(temp, fit_cp_1, color = \"red\", linewidth = 3)
plt.legend([\'Actual data\', \'curve fit\'])
plt.xlabel(\'Temperature [K]\')
plt.ylabel(\'CP\')

plt.subplot(212)
plt.title(\'Curve Fit For Cubic Function\')
plt.plot(temp, cp, color = \"blue\", linewidth = 3)
plt.plot(temp, fit_cp_3, color = \"red\", linewidth = 3)
plt.legend([\'Actual data\', \'curve fit\'])
plt.xlabel(\'Temperature [K]\')
plt.ylabel(\'CP\')
plt.subplots_adjust(hspace= 0.75)
plt.show()

Coding for curve fit

step 1) import numpy, matplotlib.pyplot, scipy.optimize import curve_fit

step 2) Curve fit function

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

cubic curve fit function

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

step 3) now we need to load provided data as below

def read_file():


temp = [] , is a empty temperature array
cp =[], is a empty cp array

loading file
for line in open(\'data\',\'r\'):

extracting data separated by \"comma\"
values = line.split(\',\')

writing temp cp varaibles in array, where first column is temp and 2nd column is cp
temp.append(float(values[0]))
cp.append(float(values[1]))

return [temp, cp]

step 4)

Main Program

program for linear curve fit

reading temp & cp from data file

temp, cp = read_file()


curve fit for Linear Function

here inbuilt function in python is used as below

popt, pcov = curve_fit(func1, temp, cp)

first inputs is linear func1 and then temp & cp

outputs are :
fit_cp_1 = func1(np.array(temp),*popt)

similar for cubic function

curve fit for Cubic Function
popt, pcov = curve_fit(func3, temp, cp)
fit_cp_3 = func3(np.array(temp),*popt)


step 5) Ploting

here I combine two plot linear and cubic polynomial in 1

it can be done by using \"subplot\"

Syntax \"subplot(r,c,n)

r = number of row for plot

c = number of column

n = active plots.


1) linear curve fit ploting

subplot(211)

# plotting
plt.subplot(211)

title
plt.title(\'Curve Fit For linear Function\')

original data
plt.plot(temp, cp, color = \"blue\", linewidth = 3)

curve fir
plt.plot(temp, fit_cp_1, color = \"red\", linewidth = 3)

legends
plt.legend([\'Actual data\', \'curve fit\'])

labeling
plt.xlabel(\'Temperature [K]\')
plt.ylabel(\'CP\')

similar for cubic function

plt.subplot(212)
plt.title(\'Curve Fit For Cubic Function\')
plt.plot(temp, cp, color = \"blue\", linewidth = 3)
plt.plot(temp, fit_cp_3, color = \"red\", linewidth = 3)
plt.legend([\'Actual data\', \'curve fit\'])

plt.xlabel(\'Temperature [K]\')
plt.ylabel(\'CP\')

to adjust space between plots to avoid overlapping of legends
plt.subplots_adjust(hspace= 0.75)
plt.show()

step 6) Results

Linear curve fit is too bad as it is not at all matching with original data can be concluded after seeing plots for linear function

where as cubic curve fit is better fit than linear as it is mostly matching with original data.

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

to make curve fit perfect we acn do

1) increase degree of a polynomial function can be concluded from above example.

2) we can divide temperature range and apply different ploynomial function(deg 1,2,3)

for particular range of temperature.

fi
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.