Challenge

Week 5 - Curve fitting

AIM: CURVE FITTING:

1.What does popt and pcov mean?

Answer: popt – This is an array which stores the value of the coefficients of the equation.

pcov—This is an 2D array. The estimated covariance of popt. The variance of estimated parameter is given by the diagonals.

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

Answer: Converts the python list of temperature to a numpy array since operation like pow are not allowed in python list but allowed in numpy arrays.

3.What does the *in *popt mean?

Answer: The * in *popt unpacks the popt array so that the two optimized parameters values become the second and third arguments to the function.

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

Answer:

#Programe to fit linear curve:

#This code is written in python
#Programe for linear curve fit
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit

#Curve fit function
def func(t,a,b):
	return a*t+b

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

#Main program
#Extracting the data 
temperature,cp = read_file()

popt,pcov = curve_fit(func,temperature,cp)

#Fitted cp
fit_cp = func(np.array(temperature), *popt)

#Original curve
plt.plot(temperature,cp,color="red",linewidth=3)
#Fitted curve
plt.plot(temperature,fit_cp,color="green",linewidth=3)
#Naming the curves
plt.legend(["Original curve","Fitted curve"])
#Axes are named
plt.xlabel("Temperature (K)")
plt.ylabel("Specific heat (J/K kg)")
#Title of the curve
plt.title("Linear curve fit")
#Saved in current directory
plt.savefig("Linear fit")
plt.show()

OUTPUT:

#Code to fit cubic equation:

#This code is written in python
#Programe for cubic curve fit
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit

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

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

#Main program
#Extracting the data 
temperature,cp = read_file()

popt,pcov = curve_fit(func,temperature,cp)

#Fitted cp
fit_cp = func(np.array(temperature), *popt)

#Original curve
plt.plot(temperature,cp,color="red",linewidth=3)
#Fitted curve
plt.plot(temperature,fit_cp,color="green",linewidth=3)
#Naming the curves
plt.legend(["Original curve","Fitted curve"])
#Axes are named
plt.xlabel("Temperature (K)")
plt.ylabel("Specific heat (J/K kg)")
#Title of the curve
plt.title("Cubic curve fit")
#Saved in current directory
plt.savefig("Cubic fit")
plt.show()

OUTPUT:

ERRORS AND DEBUGGING: I forgot ot give the necessary parameters in the definition of the function.

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

Answer: In order to make a curve fit perfect there should be minimal errors.This can be achieved by using higher order polynomial.In reality a perfect curve is not achievable.

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.