Challenge

Week 5 - Curve fitting

REPORT ON CURVE FIT USING PYTHON

Created by Shahbaaz Khan Deoda

On 20 may 2020

AIM: - Write a code to obtain linear and cubical cur fit.

EQUATION USED FOR FITS: -

1. for linear fit: - a.t + b

2. for cubical fit: - a.t2 + b.t + c

a, b, c are the coefficients of the equations.

OBJECTIVE: - 1. To code the curve fit.

  1. to obtain the curve fit.

TERMINOLOGY: -

  1. What does popt and pcov mean?

Ans. popt = array of cofficients

Pcov = 2d array of covariance of popt

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

Ans. it takes the value of temprature from the data file to do mathematical opration like square and multiplication

  1. What does the * in *popt mean?

Ans. * used for take all value of popt in consideration

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

Ans. for different ranges different polynomial fits are used

CODE FOR LINEAR 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():
	temprature = []
	cp = []
	for line in open('data','r'):
		values = line.split(',')
		temprature.append(float(values[0]))
		cp.append(float(values[1]))
	return [temprature, cp]
#main programe
temprature, cp = read_file()
popt, pcov = curve_fit(func, temprature, cp)

fit_cp=func(np.array(temprature),*popt)

# curve fit plot
plt.plot(temprature,cp,color="blue",linewidth=3)
plt.plot(temprature,fit_cp,color="red",linewidth=3)
plt.legend(['actual data','curve fit'])
plt.xlabel('Temprature[K]')
plt.ylabel('Cp[kJ/kg.K]')
plt.title('linear curve fit')
plt.show()

PLOT FOR LINEAR CURVE FIT: -

CODE FOR CUBICAL 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):
	return pow(a,3)*t + b*t + c


# reading thermodynamic data file
def read_file():
	temprature = []
	cp = []
	for line in open('data','r'):
		values = line.split(',')
		temprature.append(float(values[0]))
		cp.append(float(values[1]))
	return [temprature, cp]
#main programe
temprature, cp = read_file()
popt, pcov = curve_fit(func, temprature, cp)

fit_cp=func(np.array(temprature),*popt)

# curve fit plot
plt.plot(temprature,cp,color="blue",linewidth=3)
plt.plot(temprature,fit_cp,color="red",linewidth=3)
plt.legend(['actual data','curve fit'])
plt.xlabel('Temprature[K]')
plt.ylabel('Cp[kJ/kg.K]')
plt.title('cubical curve fit')
plt.show()

PLOT FOR CUBICAL FIT: -

CONCLUSION: -we have succesfully code the program for linear and cubical fit. And also evaluate the curve for linear and cubical 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.