Project

Curve fitting code

Approximation by linear function:

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

# Curve fit function for linear approx.

def func(t,a,b,):
	return a*t + b


# reading thermodynamic 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]


# Main program 

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


plt.plot(temperature, cp, color=\'blue\',linewidth=3)
plt.plot(temperature, fit_cp, color=\'red\',linewidth=3)
plt.legend([\'Actual data\',\'Curve fit\'])
plt.xlabel(\'Temperature [K]\')
plt.ylabel(\'Cp\')
plt.show()

Approximatiom by cubic polynomial :

The only change in the code above is the change of the function :

# Curve fit function by cubic polynomial

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

The higher polynomial by degree the better approximation of the data.

Description:

popt that is an array with optimized values of parameters [ a,b,c,d]

pcov that represent 2d array of the estimated covariance of popt

np.array that creates an array in this case temperature values ada stores it in numpy 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.