Challenge

Week 5 - Curve fitting

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


def func(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()
a = 0
b = 399

fit_cp = []
	
for i in range(0,8):

	c = temperature[a:b+1]
	d = cp[a:b+1]

	popt, pcov = curve_fit(func, c, d)
	z = func(np.array(c), *popt)


	for j in z:

		fit_cp.append(j)



	a = b + 1
	b = b + 400


plt.figure()
plt.plot(temperature, cp, color = 'green')
plt.plot(temperature, fit_cp, color = 'red')
plt.legend(['Actual data','curve fit'])
plt.xlabel('temperature [k]')
plt.ylabel('cp')
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.

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.