Challenge

Week 5 - Curve fitting

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

#read_data
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]

#curve fit function
def func(t,a,b,c):
	return a*pow(t,1)+b*pow(t,b)+c

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


plt.plot(temperature,cp,color='blue')
plt.plot(temperature,fit_cp,color='red')
plt.show()
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.