Challenge

Week 5 - Curve fitting

AIM - To explain the parameters used to measure the fitness characteristics for curves.

OBJECTIVE -

  • Write code to fit a linear and cubic polynomial for the Cp data.
  • Plot the linear and cubic fit curves along with the raw data points.

RELATED THEORY :-

A proper curve is the process of creating a curve, or mathematical operations, that is closest to the actual data chain. With the right curve, we can build an effective relationship between recognized data values ​​and parameter values, etc. It works very well in the mathematical representation of other natural processes.

1.What does popt and pcov mean?

" popt " is an array that stores the values of the coefficients that are being passed in a given function .

" pcov " is an two dimensional array that stores the values of the estimated covarience of popt i.e the coefficients .

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

The above command is used for passing the vales of temperature as a numpy array .

3.What does the * in *popt mean?

" * " in *popt helps us to refer to the coefficients in a given equation.

Python Code

Linear

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 a*t+b

 

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

 

 

temperature,cp = read_file()

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

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

 

                                                       #Plotting the curves

plt.plot(temperature,cp,color="blue",linewidth=3)

plt.plot(temperature,fit_cp,color="red",linewidth=3)

plt.legend(['actual data','curve fit'])

plt.xlabel('Temperature[K]')

plt.ylabel('Cp')

plt.show()

 

Cubic

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 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]

 

 

temperature,cp = read_file()

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

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

 
       
                                              #Plotting the curves

plt.plot(temperature,cp,color="blue",linewidth=3)

plt.plot(temperature,fit_cp,color="red",linewidth=3)

plt.legend(['actual data','curve fit'])

plt.xlabel('Temperature[K]')

plt.ylabel('Cp')

plt.show()

 

Results

For linear

For cubic

Explanation of Results

Importing statistics into calculations, importing numpy to create a temperature list, importing matplotlib.pyplot for setting the graph graph of temperature and specific temperatures, and from the scipy module for importing curves is suitable to make the curve even.

The Linear equation and cubic equation are defined in practice so that they are termed later in the larger system while making the right curve.

With the thermodynamic data available to us we use the "open" command to open the next file with the letter "r" to enter read mode. Since there is a lot of data in our file, we use a loop to read all the lines and separate the details with a comma "," in the data lines using the split function.

Special heat and humidity are stored in the same separate components, using a numpy module

Curve fit is made by returning two arrows "popt" and "pcov", popt at the appropriate parameter values ​​so that the total residual f (xdata, * popt) - ydata is reduced.

Where the word “pcov” means limited translation of popt. Diagonals provide variations in the measurement scale.

In passing a command popton in the same curve we use it as a * popt, that is because the * pointer is a tool and displays all the scenes in that list.

Predicted cp is calculated as fit_cp for linear and cubic function.

The graph is sorted using the predicted cp of polynomals, and compared with the original data set.

Conclusion

By using high-order polynomials we reduce the error, as we can see from the graph above that the cubic fit fit better than the equal line.

As we can see from the above system of separating all the data from others it would be a curve rather than better to use a single polynomial of the whole matter in all the data.

And with smaller temperatures we can see that low-order polynomials are appropriate, and as data size and values ​​increase polynomial sequence should increase to obtain a good balance.

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.