Project

Curve fitting in Python

Description

In this code I used numpy module, matplotlib module, scipy module. I created two functions one is for the Linear polynomial and other is for the Cubic polynomial. Then I created a seperate function the read the data file and extracts the temperature and specific heat. Then the curve fit syntax is used to calculate the popt and pcov. The popt gives the values of the constants in the equations or polynomial. The pcov gives the covariance matrix. In this matrix the diagonal represent the variance between the same variable. After that the fit is calculated by passing the values of temperature in numpy array and the constant values in popt. Then the graph is ploted between the temperature and specific heat and the temperature and the fitted specific heat.

1. What does popt and pcov mean?

popt gives the values of the constants in the polynomial.

pcov gives the covariance matrix. If the polynomial has 2 constants then it is 2 x 2 matrix. If the polynomial has 4 constants then pcov has 4 x 4 matrix. The diagonal element represent the variance of same variable.

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

np.array(temperature) passes the temperature values to the function. numpy supports large multidimensional array.

3. What does the *popt mean?

*popt refers to the constant values of the polynomial. It passes the constant values to the function.

4. Write code to fit a linear and cubic polynomial for the Cp data?

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

def f(t,a,b):
	term = a*t + b 
	return term

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

def read_file():
	temperature = []
	specific_heat = []
	for line in open(\'data\',\'r\'):
		values = line.split(\',\')
		x = float(values[0])
		y = float(values[1])
		temperature.append(x)
		specific_heat.append(y)
	return [temperature, specific_heat]

temperature, specific_heat = read_file()
popt, pcov = curve_fit(f, temperature, specific_heat)
fit_cp = f(np.array(temperature), *popt)

popt1, pcov1 = curve_fit(f1, temperature, specific_heat)
fit_cp1 = f1(np.array(temperature), *popt1)

plt.figure(1)
plt.plot(temperature, specific_heat, color = \"blue\", linewidth = 3)
plt.plot(temperature, fit_cp, color = \"red\", linewidth = 3)
plt.title(\"Curve Fit using Linear Polynomial\")
plt.legend([\'Actual Data\',\'Curve fit\'])
plt.xlabel(\'Temperature\')
plt.ylabel(\'Specific heat\')
plt.show()
plt.figure(2)
plt.plot(temperature, specific_heat, color = \"blue\", linewidth = 3)
plt.plot(temperature, fit_cp1, color = \"red\", linewidth = 3)
plt.title(\"Curve Fit using Cubic Polynomial\")
plt.legend([\'Actual Data\',\'Curve fit\'])
plt.xlabel(\'Temperature\')
plt.ylabel(\'Specific heat\')
plt.show()

Results

From the above figure we can see that the linear curve fit does not match to the actual data.

From the above figure we can see that the cubic curve fit tries to match the actual data. In cubic polynomial the results are good compared to the linear polynomial.

5. What needs to be done in order to make the curve fit perfect?

There are two ways to make the curve fit perfect.

1. Increasing the order of the polynomial

2. The temperature range is split into two. The one half follows the one order of polynomial and other half follows the other order of the polynomial.

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.