Project

Curve Fitting

1. The definition of popt and pcov:

  • popt:- It is the optimal value of the parameters so that the sum of the square residuals is minimized.
  • pcov:- The pcov variable contains the covariance matrix, which indicates the uncertainties and correlations between parameters. This is mostly useful when the data has uncertainties.

2. np.array(temperature):- Using this we can put the values of temperature into an array one by one using numpy module.

3. *in *popt:- This denotes the coefficient of polynomials.

OBJECTIVE:-

  • Write a program to perform curve cutting using different order of polynomials. In this case linear and cubic polynomial must be taken into accord.

CODING:-

1. Below is the code which I wrote in case of linear polynomial:

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


# CASE 1

# Function of Curve Fitting
def func(t,a,b,c):
	return a*pow(t,2)+b*t+c

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

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

# Plotting of the graph
plt.figure()
plt.plot(temperature, cp, color=\"blue\", linewidth=\"3\")
plt.plot(temperature, fit_cp, color=\"red\", linewidth=\"3\")
plt.xlabel(\'Temperature [K]\')
plt.ylabel(\'Cp\')
plt.legend([\'Actual Data\', \'Curve Fit\'])
plt.savefig(\'Figure 1\')
plt.show()

The resulting graph was as follows:

\"\"

2. Below is the program which I wrote in case of cubic polynomial:

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


# CASE 2

# Function of Curve Fitting
def func(t,a,b,c,d):
	return a*pow(t,3)+b*pow(t,2)+c*t+d

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

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

# Plotting of the graph
plt.figure()
plt.plot(temperature, cp, color=\"blue\", linewidth=\"3\")
plt.plot(temperature, fit_cp, color=\"red\", linewidth=\"3\")
plt.xlabel(\'Temperature [K]\')
plt.ylabel(\'Cp\')
plt.legend([\'Actual Data\', \'Curve Fit\'])
plt.savefig(\'Figure 2\')
plt.show()

The resulting graph is as follows:

\"\"

Comparison:- On comparing the result of both, the cubic fit is more comparable to linear fits. Thus as the order of polynomial is increased, the curve fits good.

WAYS TO IMPROVE CURVE FIT:-

  • By increasing the polynomial value.
  • If the polynomial is a lot higher, then splitting it into small parts.

CONCLUSION:-

  1. Thus, we can finally conclude that cubic fit is more better than the linear fit, which in turn proves the fact that more the value of polunomial, the more better the curve will fit.
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.