Curve fitting for Linear and Cubic Polynomials
Objective - To write program to obtain a curvefit for Linear and Cubic polynomials.
Explanation - Curve fitting is the process of constructing a curve or a mathematical function that has the best fit to a series of data points, possibly subject to constraints.
Curve fitting can involve either interpolation, where an exact fit to the data is required, or smoothing, in which a "smooth" function is constructed that approximately fit the data.
Steps Involved -
- Importing the data from a given specified file.
- Considering & designating dataset values for the columns to be created.
- Selecting the equation to be solved (linear or cubic).
- Considering the formulas the are to applied to the result.
- Plotting the curve for given data as well as to create curve for the predicted data with necessary labels & titles.
- Finally finding the value of R^2 and determining the best fit.
- R^2 value ranges from 0 to 1 . Which indicates best fit.
Perfect fit means, the curve should fit the original curve without showing any errors (such as centering and scaling erros) in that perticular degree of polynomial.
Best fit means, minimizing the geometric distance from the given point to the fitting curve, by the least square sense.
Goodness of a fit can be mesured by 4 quantities, which tells how well the quantities are representing the datapoints.
1. Sum of square due to errror(SSE)
2. R-Square
3. Adjusted R-square
4. Root mean square error(RMSE)
1. Formula to calculate Sum of square due to error(SSE),
2. Formula to Calculate R-Square,
where,
3. Formula to calculate Adjusted R-Square,
4. Formula to calculate Root mean Square Error,
Below are the programs for Linear and Cubic Polynomial Curvefit
% Curvefit for a Linear Polynomial, temperature v/s specific heat
clear all
close all
clc
% dataset to solve linear equation
cp_data = load('data'); % taking consideration a dataset from a file
temperature= cp_data(:,1); % considering all the values of temperature in column 1
cp = cp_data(:,2); % considering all the values of specific heat in column 2
% curvefit for linear equation
% considering the equation to solve as y = Ax+C;
co_eff = polyfit(temperature,cp,1); % considering the degree of the above linear
equation
predicted_cp = polyval(co_eff,temperature); % polyval solves the eqn
p1x+p2X+p3x+...+pnx
% residual values
cp_resi = cp - predicted_cp; % formula to calculate cp_resi
SSresi = sum(cp_resi.^2); % formula to calculate Sum of Square of residual
values
SStotal = (length(cp)-1) * var(cp); % formula to calculate Sum of Square of total
result = 1 - SSresi/SStotal; % R^2 value is founded here
result_adj = 1 - SSresi/SStotal * (length(cp)-1)/(length(cp)-length(co_eff));
% R^2 adj value is founded here
% plotting
plot(temperature,cp,'linewidth',3) % actual plot of the data set
hold on
plot(temperature,predicted_cp,'linewidth',3,'color','r') % predicted plot of the data
set
xlabel('temperature') % label along x-axis
ylabel('Specific_heat')
grid on
legend('original dataset',' linear curvefit') % differentiating with given data curve
& linear curve
title('Linear Polynomial')
Plot of Linear Polynomial with Temperature v/s Specific heat
R^2 value = 0.9249 as the value is near to 1 its a best fit.
% Curvefit for a Cubic Polynomial, temprature v/s Specific Heat
clear all
close all
clc
% dataset to solve linear equation
cp_data = load('data'); % taking consideration a dataset from a file
temperature= cp_data(:,1); % considering all the values of temperature in column 1
cp = cp_data(:,2); % considering all the values of temperature in column 2
% curvefit for cubic equation
% to solve the cubic equation y = Ax^3+Bx^2+Cx+D
co_eff = polyfit(temperature,cp,3); % considering the degree of the above linear
equation
predicted_cp = polyval(co_eff,temperature); % polyval solves the eqn
p1x+p2X+p3x+...+pnx
% residual values
cp_resi = cp - predicted_cp; % formula to calculate cp_resi
SSresi = sum(cp_resi.^2); % formula to calculate Sum of Square of residual values
SStotal = (length(cp)-1) * var(cp); % formula to calculate Sum of Square of total
result = 1 - SSresi/SStotal; % R^2 % R^2 value is founded here
result_adj = 1 - SSresi/SStotal * (length(cp)-1)/(length(cp)-length(co_eff));
% R^2 adj value is founded here
% plotting
plot(temperature,cp,'linewidth',3) % actual plot of the data set
hold on
plot(temperature,predicted_cp,'linewidth',3,'color','r') % predicted plot of the data
xlabel('temperature') % label along x-axis
ylabel('Specific_heat') % label along y -axis
grid on
legend('original dataset',' cubic curvefit') % differentiating with given data curve &
linear curve
title('Cubic Polynomial')
Plot for Cubic Polynomial with Tempearture v/s Specific heat
R^2 value is 0.9967 which is better than that of linear curvefit.
There is one more method where we can obtain the best fit for the qiven dataset that is by using Splitwise method.
Here the total number of dataset are broken down to individual parts of datasets and the best fit is obtained. Where it prevents the error which shows the polynomial is badly conditioned when there is huge dataset.
% Curvefit by splitwise method, temperature v/s Specific heat
close all
clear all
clc
% input
cp_data = load('data'); % taking consideration a dataset from a file
temperature= cp_data(:,1); % considering all the values of temperature in column 1
cp = cp_data(:,2); % considering all the values of specific heat in column 2
% Splitting the dataset
%range1(1-320)
temperature1= cp_data(1:320,1); %
cp1 = cp_data(1:320,2);
%range2(320-640)
temperature2 = cp_data(321:640,1);
cp2 = cp_data(321:640,2);
%range3(640-960)
temperature3 = cp_data(641:960,1);
cp3 = cp_data(641:960,2);
%range4(960-1280)
temperature4 = cp_data(961:1280,1);
cp4 = cp_data(961:1280,2);
%range5(1280-1600)
temperature5 = cp_data(1281:1600,1);
cp5 = cp_data(1281:1600,2);
%range6(1600-1920)
temperature6 = cp_data(1601:1920,1);
cp6 = cp_data(1601:1920,2);
%range7(1920-2240)
temperature7 = cp_data(1921:2240,1);
cp7 = cp_data(1921:2240,2);
%range8(2240-2560)
temperature8 = cp_data(2241:2560,1);
cp8 = cp_data(2241:2560,2);
%range9(2560-2280)
temperature9 = cp_data(2561:2880,1);
cp9 = cp_data(2561:2880,2);
%range10(2280-3200)
temperature10= cp_data(2881:3200,1);
cp10 = cp_data(2881:3200,2);
% curvefit
% curve fit for original dataset
co_eff = polyfit(temperature,cp,1);
predicted_cp = polyval(co_eff,temperature);
% curvefit for range1
co_eff1 = polyfit(temperature1,cp1,1);
predicted_cp1 = polyval(co_eff1,temperature1);
%curvefit for range2
co_eff2 = polyfit(temperature2,cp2,1);
predicted_cp2 = polyval(co_eff2,temperature2);
%curvefit for range3
co_eff3 = polyfit(temperature3,cp3,1);
predicted_cp3 = polyval(co_eff3,temperature3);
%curvefit for range4
co_eff4 = polyfit(temperature4,cp4,1);
predicted_cp4 = polyval(co_eff4,temperature4);
%curvefit for range5
co_eff5 = polyfit(temperature5,cp5,1);
predicted_cp5 = polyval(co_eff5,temperature5);
%curvefit for range6
co_eff6 = polyfit(temperature6,cp6,1);
predicted_cp6 = polyval(co_eff6,temperature6);
%curvefit for range7
co_eff7 = polyfit(temperature7,cp7,1);
predicted_cp7 = polyval(co_eff7,temperature7);
%curvefit for range8
co_eff8 = polyfit(temperature8,cp8,1);
predicted_cp8 = polyval(co_eff8,temperature8);
%curvefit for range9
co_eff9 = polyfit(temperature9,cp9,1);
predicted_cp9 = polyval(co_eff9,temperature9);
%curvefit for range10
co_eff10 = polyfit(temperature10,cp10,1);
predicted_cp10 = polyval(co_eff10,temperature10);
% plotting
% plot for original dataset
plot(temperature,cp,'linewidth',3)
hold on
plot(temperature,predicted_cp,'linewidth',3,'color','r')
plot(temperature1,predicted_cp1,'linewidth',3,'color','g') % plot for curvefit of range1
plot(temperature2,predicted_cp2,'linewidth',3,'color','b') % plot for curvefit of range2
plot(temperature3,predicted_cp3,'linewidth',3,'color','r') % plot for curvefit of range3
plot(temperature4,predicted_cp4,'linewidth',3,'color','g') % plot for curvefit of range4
plot(temperature5,predicted_cp5,'linewidth',3,'color','b') % plot for curvefit of range5
plot(temperature6,predicted_cp6,'linewidth',3,'color','r') % plot for curvefit of range6
plot(temperature7,predicted_cp7,'linewidth',3,'color','g') % plot for curvefit of range7
plot(temperature8,predicted_cp8,'linewidth',3,'color','b') % plot for curvefit of range8
plot(temperature9,predicted_cp9,'linewidth',3,'color','r') % plot for curvefit of range9
plot(temperature10,predicted_cp10,'linewidth',3,'color','g') % plot for curvefit of range10
xlabel('temperature') % label along x-axis
ylabel('Specific heat') % label along y-axis
legend('original dataset','splitwise curvefit')
grid on
title('Splitwise')
Plot for splitwise method with temperature v/s specific heat
Inference -
1. Curve can be made to fit perfectly by increasing the degree of polynomial and also by using splitwise method.
2. Best fit can be obtained by using different formulas like R-square,R-adjust, RMSE whic mimimizes the error.