Challenge

Week 5 - Curve fitting

CURVE FITTING USING PYTHON

AIM

In this challenge, we have to write a program for curve fitting using python.

CURVE FITTING

Curve fitting is the process of constructing a curve or mathematical function, that has the best fit to a series of data points, possibly subject to constraints.

  1. What do popt and pcov mean?

“popt” is an array that store the value of the coefficient that is passed through the given function

“pcov” is a two dimensional array that store the value of estimated covariance of popt i.e coefficient.

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

The above command is used to pass the temperature values as numpy array.

  1. What does the * in *popt mean?

“*” the symbol in *popt is used to refer to the values of coefficient in the given equation.

  1. Write code to fit a linear and cubic polynomial for the Cp data. Explain if your results are good or bad.

The following code is used for the curve fit using the polynomial equation.

PROCEDURE

  • The functions are used to define the linear and cubic polynomial by the arguments.

def quadratic_func(t,a,b,c,d):

return a*t**3 +b*t**2 +c*t+d

def func(t,a,b,):

return a*t +b

  • Then the read file function is used to read the files.
  • Temperature and cp values are assigned as a null array to store the values.
  • Using for loop the values are stored by append command.
  • Then the graph is plotted.
  1. What needs to be done in order to make the curve fit perfectly?

To make the curve fit more perfectly we have to split the curve into different parts and try to fit the curve.

Curve_fit command is used for curve fitting by automatically guessing the arguments or coefficient.

  1. Show empirically how well your curve has been fit.

There are different methods to show our curve is fit now we are using R_square methods to show whether our curve is fit are not.

PROGRAM

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

#def func(t,a,b,c):
#	return a*t**2 +b*t +c
def quadratic_func(t,a,b,c,d):
	return a*t**3 +b*t**2 +c*t+d
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]
temperature,cp = read_file()
popt,pcov = curve_fit(func,temperature,cp)
fit_cp = func(np.array(temperature),*popt)
plt.plot(temperature,fit_cp,color="red",linewidth=1)
#quadratic_fit
popt,pcov = curve_fit(quadratic_func,temperature,cp)
fit_cp = quadratic_func(np.array(temperature),*popt)
#dividing the data into four sections
temperature1 = temperature[0:199]
cp1 = cp[0:199]
popt1, pcov1 = curve_fit(quadratic_func,temperature1,cp1)
fit_cp1 = quadratic_func(np.array(temperature1),*popt1)

temperature2 = temperature[199:1499]
cp2 = cp[199:1499]
popt2, pcov2 = curve_fit(quadratic_func,temperature2,cp2)
fit_cp2 = quadratic_func(np.array(temperature2),*popt2)

temperature3 = temperature[1499:2999]
cp3 = cp[1499:2999]
popt3, pcov3 = curve_fit(quadratic_func,temperature3,cp3)
fit_cp3 = quadratic_func(np.array(temperature3),*popt3)

temperature4 = temperature[2999:3200]
cp4 = cp[2999:3200]
popt4, pcov4 = curve_fit(quadratic_func,temperature4,cp4)
fit_cp4 = quadratic_func(np.array(temperature4),*popt4)

plt.plot(temperature,fit_cp,color="blue",linewidth=2)
plt.plot(temperature1,fit_cp1,color="green",linewidth=1)
plt.plot(temperature2,fit_cp2,color="green",linewidth=1)
plt.plot(temperature3,fit_cp3,color="green",linewidth=1)
plt.plot(temperature4,fit_cp4,color="green",linewidth=1)
plt.legend(['linear curve fit','actual data','quadratic curve fit'])
plt.xlabel('temoerature[k]')
plt.ylabel('cp')
plt.show()

#R-square to find the goodness of fit

def R_square(temperature,cp,fit_cp):
	for i in range (0,len(temperature)):
		Mean = mean(cp)
		SSR = np.sum(pow((fit_cp[i]-Mean),2))
		SSE = np.sum(pow((cp[i]-fit_cp[i]),2))
		SST = SSR + SSE 
		R_2 = SSR/SST
	return R_2



R2_initial = R_square(temperature,cp,fit_cp)
R_1 = R_square(temperature1,cp1,fit_cp1)
R_2 = R_square(temperature2,cp2,fit_cp2)
R_3 = R_square(temperature3,cp3,fit_cp3)
R_4 = R_square(temperature4,cp4,fit_cp4)

R2 = (R_1+R_2+R_3+R_4)/4
print(R2)
#Identification for best fit
if R2 > 0.9:
	print('the curve is good fit with the valur',R2)

OUTOUT

CONCLUSION

The curve fit using python is done by various polynomial equations and the curve fit method.

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.