Challenge

Week 5 - Curve fitting

1. a. popt contains the variable(a,b,c,d),it also contains the best fit parameters for 
      the variables ( a,b,c,d)
   b. pcov variable contains the covariance matrix, which indicates the uncertainties 
      and correlations between parameters. 



2. This function stores the values of 'temperature' in the form of an array and passes 
   to the function 'numpy' 

3. *in * popt stores the values ​​of the constant parameters (a, b, c, d) of array'popt', 
   that are used in the plot


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




#linear ploynomial
def func ( 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]


# Main program

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


#plot
plt.savefig('plot1')
plt.plot(temperature,cp, color = "blue",linewidth = 3)
plt.plot(temperature,fit_cp, color = "red",linewidth = 3)
plt.legend(['actual size', 'curve_fit'])
plt.xlabel('Temperature [k]')
plt.ylabel('Cp')
plt.show()




# cubic polynomial
def func(t,a,b,c,d):
	return a*pow(t,3) + b*pow(t,2) + 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]


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


#plot
plt.savefig('plot2')
plt.plot(temperature,cp, color = "blue", linewidth = 3)
plt. plot(temperature, fit_cp, color = "red" ,linewidth = 3)
plt.legend(['actual size','curve_fit'])
plt. xlabel('Temperature [k]')
plt.ylabel('Cp')
plt.show()

explain = 

1.  By using linear curve fit, the plot is not so satisfying because the original curve of observed data looks cubic in nature.
There is a lot of variation between the original curve and linear curve so the results are bad by using a linear polynomial equation.
2. By using cubic curve fit, the plot is satisfying because the original curve of given data looks cubic in nature.
Hence there is a minimum variation between the original curve and the cubic curve so the results are good by using a cubic polynomial equation.



5. To make a perfect fit, we must reduce errors because a perfect fit means the curve should fit the original curve without showing error.​​


6.
# linear curve fit
error =  cp - fit_cp
MS_error = mean(pow(error,2))
SSE = sum(pow(error,2))
SSR = 0

for i in cp:
	SSR = SSR + pow(i - mean(cp),2)

(SST) = (SSR + SSE)
(R1_square) = (SSR/SST) 
print(R1_square)

result = 0.9301266760274183 ('Bad fit')


# cubic curve fit


error =  cp - fit_cp
MS_error = mean(pow(error,2))
SSE = sum(pow(error,2))
SSR = 0

for i in cp:
	SSR = SSR + pow(i - mean(cp),2)

(SST) = (SSR + SSE)
(R1_square) = (SSR/SST) 
print(R1_square)
 

result = 0.9967366311575357 ('Good 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.