Project

project on curve fit

Program:

# Program to demonstrate Curve Fit by increasing the degree of the polynomials

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

# Quartic Curve fit Function 
def quartic_poly(t, a, b, c, d, e):
	A = a * pow(t, 4) + b * pow(t, 3) + c * pow(t, 2) + d * t + e
	return A

# Cubic Curve Fit Function
def cubic_poly(t, q, w, y, r):
	B = q * pow(t, 3) + w * pow(t, 2) + y * t + r
	return B

# Linear Curve Function
def linear_poly(t, g, h):
	C = g * t + h
	return C	

# Reading thermodynamic data file function
def read_file():
	cp = []
	temperature = []
	for line in open(\'data\', \'r\'):
		values = line.split(\',\')
		cp.append(float(values[1]))
		temperature.append(float(values[0]))
	return [temperature, cp]	

# Plotting the data function
def perform_curve_fit(temperature, cp, fit_cp1, fit_cp2, fit_cp3):
	plt.figure(1)
	plt.plot(temperature, cp, \'c\', linewidth = 2)
	plt.plot(temperature, fit_cp2, \'--k\')
	plt.plot(temperature, fit_cp3, \':r\')
	plt.legend([\'Actual Data\', \'Cubic Curve Fit\', \'Linear Curve Fit\'], loc = \'best\')
	plt.xlabel(\'Temperature [K]\', fontsize = 12)
	plt.ylabel(\'Cp\', fontsize = 12)
	plt.title(\'Performing Linear & Cubic Curve Fits\', fontsize = 15, color = \'m\')
	plt.savefig(\'cubic_linear.png\')
	plt.show()

	plt.figure(2)
	plt.plot(temperature, cp, \'c\', linewidth = 2)
	plt.plot(temperature, fit_cp1, \'--k\')
	plt.legend([\'Actual Data\', \'Quartic Curve Fit\'], loc = \'best\')
	plt.xlabel(\'Temperature [K]\', fontsize = 12)
	plt.ylabel(\'Cp\', fontsize = 12)
	plt.title(\'Performing Quaritc Curve Fit\', fontsize = 15, color = \'b\')
	plt.savefig(\'quartic.png\')
	plt.show()

######################   Driver Script    #############################

# Storing the coefficients of polynomials in popt arrays 
# after reading the thermodynamic properties from data file
temperature, cp = read_file()
popt1, pcov1 = curve_fit(quartic_poly, temperature, cp)		
popt2, pcov2 = curve_fit(cubic_poly, temperature, cp)		
popt3, pcov3 = curve_fit(linear_poly, temperature, cp)		

# Generating numpy array for the temperature values and 
# storing fit values of Cp using different polynomials
temp = np.array(temperature)			
fit_cp1 = quartic_poly(temp, *popt1)	
fit_cp2 = cubic_poly(temp, *popt2)
fit_cp3 = linear_poly(temp, *popt3)

# Plots
perform_curve_fit(temperature, cp, fit_cp1, fit_cp2, fit_cp3)

Results and Conclusions:

Plot obtained:






The coefficients of the quartic polynomial are

-1.0212010449881611e-11 9.665168208992808e-08 -0.0003367919770872752 0.5827183837218107 781.0667208575651
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.