Project

Curve Fitting

popt:- popt gives the optimum value of constant(a,b,c,d... which depend upon the order of approximation if it linear then 2 constant ,quadratic then 3 constant, cubic then there are 4 constant) the values obtained are such that sum of squared error are minimized

pcov:-It gives covariance 2D array of the parameter of popt and used to calculate standard deviation error of popt parameter

np.array(temperature):-It will form the temperature array using numpy

#curve fitting project
#by Abhishek Parmar
import math
import matplotlib.pyplot as plt 
from scipy.optimize import curve_fit
import numpy as np
def fun1(t1,a,b):
	return a*t1 + b
def fun2(t2,a,b,c,d):
	return a*pow(t2,3) + b*pow(t2,2) + c*t2 + d 
def read_file():
	temperature=[]
	cp=[]
	for line in open(\'a\',\'r\') :
		values=line.split(\',\')
		temperature.append(float(values[0]))
		cp.append(float(values[1]))
	return(temperature,cp)
temperature,cp=read_file()
popt,pcov=curve_fit(fun1,temperature,cp)

fit_cp=fun1(np.array(temperature),*popt)
plt.figure(1)
plt.plot(temperature,cp,color=\'blue\',linewidth=3)
plt.plot(temperature,fit_cp,color=\'red\',linewidth=3)
plt.legend([\'Actual data\',\'Curve fit data\'])
plt.xlabel(\'Temperature\')
plt.ylabel(\'Cp\')
plt.show()
popt,pcov=curve_fit(fun2,temperature,cp)
fit_cp1=fun2(np.array(temperature),*popt)
plt.figure(2)
plt.plot(temperature,cp,color=\'blue\',linewidth=3)
plt.plot(temperature,fit_cp1,color=\'red\',linewidth=3)
plt.legend([\'Actual data\',\'Curve fit data\'])
plt.xlabel(\'Temperature\')
plt.ylabel(\'Cp\')
plt.show()

\"\"

\"\"

Seperate code for 4th and 5th order approximation is shown below

#curve fitting project 4th and 5th order approximation
#by Abhishek Parmar
import math
import matplotlib.pyplot as plt 
from scipy.optimize import curve_fit
import numpy as np
def fun1(t1,a,b,c,d,e):
	return a*pow(t1,4) + b*pow(t1,3) + c*pow(t1,2) + d*t1 + e
def fun2(t2,a,b,c,d,e,f):
	return a*pow(t2,5) + b*pow(t2,4) + c*pow(t2,3) + d*pow(t2,2) + e*t2 + f 
def read_file():
	temperature=[]
	cp=[]
	for line in open(\'a\',\'r\') :
		values=line.split(\',\')
		temperature.append(float(values[0]))
		cp.append(float(values[1]))
	return(temperature,cp)
temperature,cp=read_file()
popt,pcov=curve_fit(fun1,temperature,cp)

fit_cp=fun1(np.array(temperature),*popt)
plt.figure(1)
plt.title(\'4th order approximation of cp and temperature\')
plt.plot(temperature,cp,color=\'blue\',linewidth=3)
plt.plot(temperature,fit_cp,color=\'red\',linewidth=3)
plt.legend([\'Actual data\',\'Curve fit data\'])
plt.xlabel(\'Temperature\')
plt.ylabel(\'Cp\')
plt.show()
popt,pcov=curve_fit(fun2,temperature,cp)
fit_cp1=fun2(np.array(temperature),*popt)
plt.figure(2)
plt.title(\'5th order approximation of cp and temperature\')
plt.plot(temperature,cp,color=\'blue\',linewidth=3)
plt.plot(temperature,fit_cp1,color=\'red\',linewidth=3)
plt.legend([\'Actual data\',\'Curve fit data\'])
plt.xlabel(\'Temperature\')
plt.ylabel(\'Cp\')
plt.show()

Result are shown below

\"\"

\"\"

5th order approximation give better result as it exactly matches with original data

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.