Project

curve fitting using python

1. What does popt and pcov mean?

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

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

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


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()

popt:The output is an array with the values for the parameters used above so that the sum of the squared residuals of data is minimized.

pcov:2nd array with the estimated covariance of the parameters in popt. Used to calculate the the standard deviation errors of popt.

2. What does np.array(temperature) do?

This function stores the values of temperature in the form of array and passes it to the function numpy.

3. What does the * in *popt mean?

It stores the values of the constant parameters(a,b,c) in the array popt

in order to get better curve fit we need to increase the order of the equation in order to get precise curve fir

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.