Week 5 - Curve fitting
- Aim:- To write codes to perform curve fitting.
- Objectives of the project:
- Write code to fit a linear and cubic polynomial for the Cp data.
2.Plot the linear and cubic fit curves along with the raw data point.
3.Explain the parameters used to measure the fitness characteristics for the curves
Solution:
Answers of questions
popt: array stores the values of coefficients that are given in a function
1.pcov :two dimensional array that stores the values of estimated covariance of the coefficients i.e popt
2.np.array(temperature) used to pass the values of temperature as np array
3.* in * popt helps to refer coefficient of given equation
- Governing equations:-
Method used to solve this problem is polynominal curve fitting
1.Linear curve fitting (linear regression)-f(x)=ax+b">f(x)=ax+bf(x)=ax+b
2.Quadratic curve fitting-f(x)=ax2+bx+c">f(x)=ax^2+bx+cf(x)=ax^2+bx+c
3.Cubic curve fitting-f(x)=ax3+bx2+cx+d">f(x)=ax^3+bx^2+cx+df(x)=ax3+bx2+cx+d
4.4th order polynominal-f(x)=ax4+bx3+cx2+dx+f">f(x)=ax^4+bx^3+cx^2+dx+df(x)=ax4+bx3+cx2+dx+f
Incaseofourprob≤mcalca̲tiondo≠→calcua<ebestfitforspecificheat(cp)consider∈gtemperature.">I
The goal is to identify the coefficients ‘a’ and ‘b’ …..’f’ such that f(x) ‘fits’ the data well.
To find the the goodness of fit method of least square is used. Errors calculation helps to find best fits.
`Notation used:
Error=e,
Sum of squares of error=sse, sum of square of regression=ssr,
total sum of squares=sst e =(cp-predicted_cp1);
sse =sum(e.^2);
ssr=sum((cp-mean(cp)).^2);
sst =(sse +ssr);
r_square=(ssr/sst);
root mean square error= rmse=sqrt(sse/n); where, ‘n’ defines no. of datapoints`
4.Body of the content:
Introduction:
Steps in code:
- First the file of data named 'curve_it_data' get loaded in the programme
- The 1st column of data specifies Temperature,temp (k) and 2nd column specifies Specific heat, cp-(Kj/kmol-k)
- Program is written in two parts such as: part 1 is for linear curve fit and part 2 Cubic curve fit.
- After running the code coefficients are get calculated for each type of fit and Plots are get plotted for Curve fit .
Window screenshot:# -*- coding: utf-8 -*- """ Created on Tue Jul 14 17:15:54 2020 @author: ASUS """ ''' Program for linear curve fit''' import numpy as np from scipy.optimize import curve_fit import matplotlib.pyplot as plt def function(t,a,b): ## defining function return a*t+b ## function for linear fit def read_file(): temp=[] ### empty arrays to store values of temperature cp=[] for line in open('curve_fit_data','r'): ## ro read each line in data values=line.split(',') ## splitting data in line using comma temp.append(float(values[0])) ## temperature values assigned to first column cp.append(float(values[1])) ## cp values assigned to first column return[temp,cp] temp,cp=read_file() ##calling function read-file and store value in temp and cp popt,pcov=curve_fit(function,temp,cp) fit_cp=function(np.array(temp),*popt) plt.plot(temp,cp,'r') plt.plot(temp,fit_cp,'b') plt.legend(['actual data', 'curve fit data']) plt.xlabel('temp') plt.ylabel('cp') plt.show() """Program for cubic curve fit""" def function_2(t,a,b,c,d): return a*pow(t,3)+b*pow(t,2)+c*t+d ## function for cubic fit temp,cp=read_file() ##calling function read-file and store value in temp and cp popt,pcov=curve_fit(function_2,temp,cp) fit_cp_2=function_2(np.array(temp),*popt) plt.plot(temp,cp,'g') plt.plot(temp,fit_cp_2,'y') plt.legend(['actual data', 'curve fit data_2']) plt.xlabel('temp') plt.ylabel('cp_2') plt.show()
linear fit graph
Cubic fit
From the graphs it is concluded cubic fit is good than linear fit.