Challenge

Week 5 - Curve fitting

1) PCOV is used to calculate the standard deviation and Popt is used to calculate coefficients and store them.

2) np.array(temperature) is an 1D arrays which contains the values of temperature

3) star symbol represents the address.

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

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

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]

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

temperature,cp=read_file()
popt,pcov=curve_fit(func1,temperature,cp)
fit_cp1=func1(np.array(temperature),*popt)

plt.plot(temperature,cp,color="blue")
plt.plot(temperature,fit_cp,color="red")
plt.legend(['Actual data','curve fit'])
plt.xlabel('Tempearture[K]')
plt.ylabel('cp')
plt.title('Linear fit')
plt.show()

plt.plot(temperature,cp,color="blue")
plt.plot(temperature,fit_cp1,color="red")
plt.legend(['Actual data','curve fit'])
plt.xlabel('Tempearture[K]')
plt.ylabel('cp')
plt.title('Cubic fit')
plt.show()
plt.show()

4) In order to make curve fit perfect we need to choose higher order polynomial which gives Rsquare value equals to one.

5) The emperical equation obtained by cubic curve fit is

y = -1E-17x3 + 2E-14x2 + 0.1739x + 888.01

and R² = 1

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.