Curve fitting - MATLAB
AIM : To write a code to perform curve fitting
THEORY :
Curve fitting is the process of constructing a curve, or mathematical functions, which possess the closest proximity to the real series of data. By curve fitting, we can mathematically construct the functional relationship between the observed dataset and parameter values, etc.
The accuracy of the curve fit can be improved by increasing the degree of the polynomial. So, a 'Best fit' can be obtained by increasing the degree of the polynomial.
PERFECT FIT : The perfect fit is achieved when the geometric distances from the given points to the fitting curve are zero . This means that the curve fits perfectly on the points . To get a perfect fit we can increase to a order of the polynomials , this method increases the accuracy and reduces errors .
BEST FIt :The best fit is achieved when the geometric distances from the given points to the fitting curve are minimized, in the least squares sense. Finding the best fit reduces to the minimization of the objective function where denotes the fitting curve.
Upon finding a curve which satisfies the data points there are 4 quantities which help us measure the goodness of fit criteria or how well the equation is a representing the datapoints.
- The sum of squares due to error (SSE)
- R-square
- Adjusted R-square
- Root mean squared error (RMSE)
https://projects.skill-lync.com/projects/Curve-Fitting-and-criterion-to-choose-best-fit-41860
Link for data : https://drive.google.com/file/d/1vWi9Vj3AdQf0gqUK1u9jwS8zg92BHGR0/view
CODE STRUCTURE :
The file containing the data is copied to a variable in the Matlab program using 'load' function like this: d = load('data')
- Each individual parameter in 'd' is copied to other variables i.e cp and temp
- A curve is to fit for 'cp' and 'Temperature' values, so a function 'polyfit' is called and assigned to a new variable. The inputs for the function are (X parameter, Y parameter, Degree of the polynomial) in the same order.
- The function 'polyfit' returns the coefficients in descending order of power of the polynomial. Another function 'polyval' is called and assigned to a new variable. This function takes (return values of 'polyfit', X parameter) as input in the same order and returns respective 'Y axis' values i.e., cp values.
- These set of values are used for curve fitting. The same operations are performed to obtain a linear and a cubic curve fit.
Separate plots are made to depict linear and Cubic curve fits against the actual curve.
MATLAB CODE :
1.Linear polynomial
% PROGRAM TO PERFORM CURVE FIT (LINEAR)
clear all
close all
clc
% INPUTS and DATA extraction
d = load('data'); % loading the data from the file "data"
temp = d(:,1); % getting temperature data from column 1
cp = d(:,2); % getting specific heat data from column 2
% using polyfit to obtain the coefficient
coeff = polyfit(temp,cp,1) % for linear polynomial i.e f(T) = aT+b coefficient values a & b are obtained
predicted_cp = polyval(coeff , temp) % for curve fitting we need predicted sepcific heats
% plotting the graphs
plot (temp,cp,'linewidth',3,'color','y');
hold on;
plot(temp, predicted_cp,'linewidth',3);
xlabel('Temperature in K','color','b');
ylabel('Specific heat in KJ/Kmol-K','color','r');
title('Temperature v/s Specific heats', 'color' ,'g');
legend('Original Data' , 'Curve Fit Data','location','northwest');
Figure 1 :
2. Code for cubic polynoimal :
% Cubic Polynomial curve fit
clear all
close all
clc
% Data Extraction from data file
d = load('data'); % loading the data from the file "data"
temp = d(:,1); % getting temperature data from column 1
cp = d(:,2); % getting specific heat data from column 2
% using polyfit to obtain coefficient
coeff = polyfit(temp,cp,3) % for cubic polynomial i.e f(T) = aT^3 + bT^2 + cT + d coefficient values a & b are obtained
predicted_cp = polyval(coeff , temp) % for curve fitting we need predicted sepcific heats
% plotting the Graphs
plot (temp,cp,'linewidth',2);
hold on;
plot(temp, predicted_cp,'linewidth',2);
xlabel('Temperature in K');
ylabel('Specific heat in KJ/Kmol-K');
title('Temperature v/s Specific heats','color','r');
legend('Original Data' , 'Curve Fit Data','location','northwest');
Figure 2 :
The plot obtained here is more accurate than the linear curve fitting.
3.Split_wise_curvefit
% SPLITWISE CURVE FIT
clear all
close all
clc
% Extracting data from file
d = load('data'); % loading the data from the file "data"
temp = d(:,1); % loading temperature data from column 1
cp = d(:,2); % loading specific heat data from column 2
coeff1 = polyfit(temp(1:1600),cp(1:1600),2); % for quadratic polynominal i.e f(T) = aT^2 + bT + c coefficient values a,b & c are obtained
coeff2 = polyfit(temp(1600:3200),cp(1600:3200),3); % for cubic polynomial i.e f(T) = aT^3 + bT^2 + cT + d coefficient values a,b,c & d are obtained
%calculating predicted specific heat values for 2 set of temperature range
predicted_cp1 = polyval(coeff1,temp(1:1600));
predicted_cp2 = polyval(coeff2,temp(1600:3200));
% plotting the graphs
plot(temp,cp,'linewidth',1.5,'color','g')
hold on
plot(temp(1:1600),predicted_cp1,'linewidth',1.5,'color','r')
plot(temp(1600:3200),predicted_cp2,'linewidth',1.5,'color','y')
hold off
xlabel('Temperature in K')
ylabel('Specific Heat in KJ/Kmol-K')
title('Split-wise Curve Fit Data')
legend('Original Data','Curve Fit Data','Split-wise Curve Fit Data','location','northwest'
From the above figures it can be concluded that, The accuracy of the curve fit can be improved by increasing the degree of the polynomial.