Challenge

Week 5 - Curve fitting

               %Write a program to evaluate the Linear and Cubic polynomial and find 
                              the better fit for the dataset.


The major technique to illustrate the behaviour of two quantities and observe the relation can be easily done on the X-Y Plots. One can easily plot the one to one quantities with the help of the interpolation method.
   But many times there could be non-linear, unstable points where plotting linear graphs can be difficult even with the help of the interpolation method.
    Hence these complex problems can be solved with the help of the 'CURVE FITTING' method.

So, what is this Curve Fitting Method?????
    Curve fitting is a technique to construct a curve with mathematical functions which also helps us to find the best curve fo the dataset. With the best curve, we could predict future data with accuracy. It is also useful for future predictions with the current dataset.


1. What do popt and pcov mean? 
= 'pcov'is the 2d array that contains  the estimated 'Covariance' of the parameters 'popt'

2. What does np.array(temperature) do?
= It is used to create numpy array and pass the temperature values.

3. What does the * in *popt mean?
= It is used to expand the list of values stored in the array 'popt'

4. Write code to fit a linear and cubic polynomial for the Cp data. Explain if your results are good or bad. 
= Linear plot was not up to the mark as it mismatched with the actual data. The result of the cubic fit was better and it matched with actual data. But there are more polynomials which can match better than linear and cubic.


------------------------------------------------------------------------------------
import math                                 %Importing the libraries for performing 
                                            %mathematical function, plotting graphs and 
                                            %implementing the Curve_fit module.
import matplotlib.pyplot as plt 
import numpy as np 
from scipy.optimize import curve_fit

#curve fit function

#For Linear Equation

def func(t,a,b,c,):                        %defining the function 'func' to predict the 
                                           %linear fit. t,a,b,c are passed as argument 
                                           %to the function 
  return a*pow(t,2) + b*t + c

#For Cubic equation                        %defining the function 'fun1' to predict the 
                                           %cubic fit. t,a,b,c,d are passed as argument 
                                           %to the function

def fun1(t,a,b,c,d):
	return a*pow(t,3) + b*pow(t,2) + c*t + d

#reading thermodynamic data file            %Define the function 'read_file'to read the 
                                            %data from the file 'data'
def read_file():
	temperature = []                    %defining the empty array 'temperature' and 
                                            %'cp' to store the values from the the file 
                                            %data.
	cp = []

	for line in open('data','r'):      %Using a loop for reading and opening the 
                                           %file with the command 'line' and 'open' 
                                           %command. We used 'line. split' command to 
                                           %space the line between two entities. Append 
                                           %the temperature and cp values to store in 
                                           % of the empty arrays created.
		
                 values = line.split(',')
		temperature.append(float(values[0]))
		cp.append(float(values[1]))
	return[temperature,cp]

#Main program for Linear equation           %use the curve_fit function to perform 
                                            %curve fitting for linear and cubic 
                                            %equation using 
                                            %actual data.Read file 
                                            %with the read file function.
                                            %np.array is used to create numpy array and 
                                            %temperature is passed
temperature, cp  = read_file()
popt, pcov = curve_fit(func,temperature,cp)
fit_cp = func(np.array(temperature), *popt)

#Main program for Cubic equation
temperature, cp  = read_file()
popt1, pcov1 = curve_fit(fun1,temperature,cp)
fit_cp1 = fun1(np.array(temperature), *popt1)

plt.figure(1)                                   %plotting the figure(1) with the help 
                                                %of plot command for the actual and new 
                                                %fitted data. Legend and labels are 
                                                 %added to the plot for better 
                                                 %representation.
plt.plot(temperature,cp, color= "blue", linewidth =3)
plt.plot(temperature,fit_cp,color="red", linewidth= 3)
plt.legend(['Actual data','Curve fit for linear'])
plt.xlabel('Temp [k]')
plt.ylabel('Cp')

plt.figure(2)

plt.plot(temperature,cp, color= "blue", linewidth =3)
plt.plot(temperature,fit_cp1,color="red", linewidth= 3)
plt.legend(['Actual data','Curve fit for Cubic equation'])
plt.xlabel('Temp [k]')
plt.ylabel('Cp')



plt.show()
--------------------------------------------------------------------------------------
%%%%Conclusion and Future Scope
 
    We have successfully demonstrated the linear and cubic fit curve with the help of code. There are many methods that can be used to make the curve fit properly. One of the methods was to increase the degree of the polynomial but there is the error as badly conditioned equations as we plotted.
To overcome this, if we used the splitting method we can easily plot the best fit curve.
       In the future, this data could be of huge size, more columns, and rows. We could use coding but can be hectic for large data and error can be introduced. Hence, a some toolkit can be a boon for such data. 
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.