Challenge

Week 5 - Curve fitting

1)What does popt and pcov mean?

popt- An array of values for the parameters which minimizes the polynomials and gives out residual values

Pcov -An array of Estimated covarience of popt perameters.

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

it gives out a array of numbers which we feed inside.

3.What does the * in *popt mean?

It passes the values of all constants and co.effs of a polynomial defined inside a function as agruments.

4-a)program code for linear polynomials:

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

#def fun1(t,a,b,c,d):
	#return (a*t*t*t)+(b*t*t)+(c*t)+d

def fun2(t,a,b):
	return (a*t)+b

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(fun2,temperature,cp)
fit_cp=fun2(np.array(temperature),*popt)
plt.plot(temperature,cp,color='b',linewidth=3)
plt.plot(temperature,fit_cp,color='r',linewidth=3)
plt.xlabel("Temperature")
plt.ylabel("Cp")
plt.show()

output plot- linear polynomial curve fit:

4-b)program code for cubic polynomials:

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

def fun1(t,a,b,c,d):
	return (a*t*t*t)+(b*t*t)+(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()
popt,pcov=curve_fit(fun1,temperature,cp)
fit_cp=fun1(np.array(temperature),*popt)
plt.plot(temperature,cp,color='b',linewidth=3)
plt.plot(temperature,fit_cp,color='r',linewidth=3)
plt.xlabel("Temperature")
plt.ylabel("Cp")
plt.show()

output plot- cubic polynomial curve fit:

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

1)Adjust the co.eff values to get perfect fits.

2)Optomize through error function.

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.