Curve Fitting in MATLAB
Problem Statement:-
- 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.
- Explain the parameters used to measure the fitness characteristics for both the curves.
Answer following questions:-
- How to make a curve fit perfectly?
- How to get the best fit?
- What could be done to improve the cubic fit?
Theory :-
Curve Fitting :-
Curve fitting is the process of specifying the model that provides the best fit to the specific curves in your dataset. Curve fitting construct a curve or mathematical function that has best fit to a series of data points. Curve fitting is the way we model or represent a data spread by assigning a best fit function (curve) along the entire range. Ideally, it will capture the trend in the data and allow us to make predictions of how the data series will behave in the future.
Linear and Polynomial Curve Fitting :-
- Linear curve fitting, or linear regression when the data is fit to a straight line. Although there might be some curve to your data, a straight line provides a reasonable enough fit to make predictions.
- Mainly for curve fitting the function used is in the form of `y=f(x)`.
- A polynomial function is known as a function which is in form of `y = a_1x^n + a_2x^{n-1} +. . . + a_nx+a_{n+1}`. Where `a_1,a_2,. . .,a_n` are known as constants.
- The maximum power if x is known as degree of polynomial.
- For example first degree polynomial equation is in the form of
`f = ax + b`
Which represent a slope of line. First degree polynomial is also known as linear curve fitting.
- Second degree polynomial equation is in the form of
`f = ax^2 + bx +c`
This is exactly simple curve to three points.
- Third degree polynomial equation is in the form of
`f = ax^3 + bx^2 + cx + d`
This will exactly fit four points
In the figure Red line show first degree polynomial, green line show second degree, orange line show third degree and blue show fourth degree polynomial.
Evaluating the Goodness of Fit :-
After fitting the data with one or more models, the goodness of fit should be evaluate. A visual examination of the fitted curve displayed in the Curve Fitting Tool should be your first step.
Residuals :-
The residuals from a fitted model are defined as the differences between the response data and the fit to the response data at each predictor value.
Residual = data – fit
Goodness of Fit Parameters :-
Mainly four parameters are use for statistics analysis of goodness of fit.
- The sum of squares due to error (SSE)
- R-square
- Adjusted R-square
- Root mean squared error (RMSE)
The sum of squares due to error (SSE) :-
Sum of squares (SS) is a statistical tool that is used to identify the dispersion of data as well as how well the data can fit the model in regression analysis. The sum of squares got its name because it is calculated by finding the sum of the squared differences. The sum of squares is one of the most important outputs in regression analysis. The general rule is that a smaller sum of squares indicates a better model, as there is less variation in the data.
Types of Sum of Squares
1. Total sum of squares
`SST = sum_{i=1}^n(y_i -bar y)^2`
2. Regression sum of squares
`SSR = sum_{i=1}^n(hat y_i -bar y)^2`
3. Residual sum of squares
`SSE = sum_{i=1}^n(y_i -hat y_i)^2`
Where `y_i =`Observed values
`hat y_i =` Value estimated by the regression line
`bar y =` Mean value of sample
R-square
R-squared (R2) is a statistical measure that represents the proportion of the variance for a dependent variable that's explained by an independent variable or variables in a regression model. R-squared explains to what extent the variance of one variable explains the variance of the second variable. So, if the R2 of a model is 0.50, then approximately half of the observed variation can be explained by the model's inputs.
`R^2 = frac{SSR}{SST}`
`SST = SSR + SSE`
Adjusted R-square
Adjusted R-square measures the proportion of variation explained by only those independent variables that really helps in explaining the dependent variable. It penalizes you for adding independent variable that do not help in predicting the dependent variable. Adjusted R-Squared can be calculated mathematically in terms of sum of squares. The only difference between R-square and Adjusted R-square equation is degree of freedom.
`R_{adj}^2 = 1 - [frac{(1-R^2)(n-1)}{n-k-1}]`
Root mean squared error (RMSE)
Root Mean Square Error (RMSE) is the standard deviation of the residuals (prediction errors). RMSE is a measure of how spread out these residuals is. In other words, it tells you how concentrated the data is around the line of best fit. Root mean square error is commonly used in climatology, forecasting, and regression analysis to verify experimental results.
`RMSE = sqrt{frac{SSE}{n}}`
A RMSE value closer to 0 indicates a better fit.
Here in this solution I had take RMSE parameter for goodness of fit.
MATLAB CODE : -
For calculate goodness of fit I created a function which calculate value of RMSE.
1. Function code :-
function [RMSE] = goodfit_cal(cp,predicted_cp)
% Calculating error
error = cp - predicted_cp;
% Total observed sample
n = length(cp);
% Calculating SSE value
SSE = 0;
SUM = 0;
for i=1:n % According to formula
SSE = SSE + error(i)^2;
SUM = SUM + cp(i);
end
% Calculating mean of data
mean = SUM/2;
% Calculating SSR
SSR = 0;
for j= 1: length(predicted_cp)
SSR = SSR + (predicted_cp(i) - mean)^2;
end
% Calculating SST
SST = SSR + SSE;
% Calculating R^2
R_sq = SSR/SST;
% Calculating RMSE
RMSE = (SSE/n)^0.5;
end
2. Program Code :-
% Program for Curve fitting
clear all % Clear the Workspace
close all % Clear all figures and plot
clc % Clear the Command Window
% Preparing the data
cp_data = load('data'); % Load cp data
temperature = cp_data(:,1); % Calling of first column of Data file which contain temperature
cp = cp_data(:,2); % Calling of second column of Data file which contain value of cp
% Curve fit for linear curve
% polyfit command use to generate constant for polynomial
% This command take value of x and y and degree of polynomial
co_effs1 = polyfit(temperature,cp,1);
% polyval function is use to evalute the value of y by taking value of constants and x
predicted_cp1 = polyval(co_effs1, temperature);
% Give command to generate figure in full screen
figure('units','normalized','outerposition',[0 0 1 1])
% Compare my curve fit with the original curve data
% Generate sub plot
subplot(3,3,1)
plot(temperature, cp, 'linewidth',1.5) % Plot original curve
hold on
plot(temperature, predicted_cp1,'linewidth',1.5,'color','r') % Plot predicted linear curve
% Labelling and title
xlabel('Temperature [K]')
ylabel('Specific Heat [Kj/Kmol-K]')
legend('original dataset', 'Linear Curve fit','location','southeast')
title('Linear curve fit with original data Curve')
% Curve fit for second degree polynomial
% polyfit command use to generate constant for polynomial
% This command take value of x and y and degree of polynomial
co_effs2 = polyfit(temperature,cp,2);
% polyval function is use to evalute the value of y by taking value of constants and x
predicted_cp2 = polyval(co_effs2, temperature);
% Generate sub plot
subplot(3,3,2)
% Compare my curve fit with the original curve data
plot(temperature, cp, 'linewidth',1.5) % Plot original curve
hold on
plot(temperature, predicted_cp2,'linewidth',1.5,'color','r') % Plot predicted second degree curve
% Labelling and title
xlabel('Temperature [K]')
ylabel('Specific Heat [Kj/Kmol-K]')
legend('original dataset', '2nd Degree Curve fit','location','southeast')
title('2nd Degree curve fit with original data')
% Curve fit for third degree polynomial
% polyfit command use to generate constant for polynomial
% This command take value of x and y and degree of polynomial
co_effs3 = polyfit(temperature,cp,3);
% polyval function is use to evalute the value of y by taking value of constants and x
predicted_cp3 = polyval(co_effs3, temperature);
% Generate sub plot
subplot(3,3,3)
% Compare my curve fit with the original curve data
plot(temperature, cp, 'linewidth',1.5) % Plot original curve
hold on
plot(temperature, predicted_cp3,'linewidth',1.5,'color','r') % Plot predicted curve
% Labelling and title
xlabel('Temperature [K]')
ylabel('Specific Heat [Kj/Kmol-K]')
legend('original dataset', '3rd Degree Curve fit','location','southeast')
title('3rd Degree curve fit with original data Curve')
% Curve fit for fourth degree polynomial
% polyfit command use to generate constant for polynomial
% This command take value of x and y and degree of polynomial
co_effs4 = polyfit(temperature,cp,4);
% polyval function is use to evalute the value of y by taking value of constants and x
predicted_cp4 = polyval(co_effs4, temperature);
% Compare my curve fit with the original curve data
% Generate sub plot
subplot(3,3,4)
plot(temperature, cp, 'linewidth',1.5) % Plot original curve
hold on
plot(temperature, predicted_cp4,'linewidth',1.5,'color','r') % Plot predicted curve
% Labelling and title
xlabel('Temperature [K]')
ylabel('Specific Heat [Kj/Kmol-K]')
legend('original dataset', '4th Degree Curve fit','location','southeast')
title('4th Degree curve fit with original data Curve')
% Curve fit for fifth degree polynomial
% polyfit command use to generate constant for polynomial
% This command take value of x and y and degree of polynomial
co_effs5 = polyfit(temperature,cp,5);
% polyval function is use to evalute the value of y by taking value of constants and x
predicted_cp5 = polyval(co_effs5, temperature);
% Compare my curve fit with the original curve data
% Generate sub plot
subplot(3,3,5)
plot(temperature, cp, 'linewidth',1.5) % Plot original curve
hold on
plot(temperature, predicted_cp5,'linewidth',1.5,'color','r') % Plot predicted curve
% Labelling and title
xlabel('Temperature [K]')
ylabel('Specific Heat [Kj/Kmol-K]')
legend('original dataset', '5th Degree Curve fit','location','southeast')
title('5th Degree curve fit with original data Curve')
% Curve fit for sixth degree polynomial
% polyfit command use to generate constant for polynomial
% This command take value of x and y and degree of polynomial
co_effs6 = polyfit(temperature,cp,6);
% polyval function is use to evalute the value of y by taking value of constants and x
predicted_cp6 = polyval(co_effs6, temperature);
% Compare my curve fit with the original curve data
% Generate sub plot
subplot(3,3,6)
plot(temperature, cp, 'linewidth',1.5) % Plot original curve
hold on
plot(temperature, predicted_cp6,'linewidth',1.5,'color','r') % Plot predicted curve
% Labelling and title
xlabel('Temperature [K]')
ylabel('Specific Heat [Kj/Kmol-K]')
legend('original dataset', '6th Degree Curve fit','location','southeast')
title('6th Degree curve fit with original data Curve')
% Curve fit for seventh degree polynomial
% polyfit command use to generate constant for polynomial
% This command take value of x and y and degree of polynomial
co_effs7 = polyfit(temperature,cp,7);
% polyval function is use to evalute the value of y by taking value of constants and x
predicted_cp7 = polyval(co_effs7, temperature);
% Compare my curve fit with the original curve data
% Generate sub plot
subplot(3,3,7)
plot(temperature, cp, 'linewidth',1.5) % Plot original curve
hold on
plot(temperature, predicted_cp7,'linewidth',1.5,'color','r') % Plot predicted curve
% Labelling and title
xlabel('Temperature [K]')
ylabel('Specific Heat [Kj/Kmol-K]')
legend('original dataset', '7th Degree Curve fit','location','southeast')
title('7th Degree curve fit with original data Curve')
% Curve fit for eight degree polynomial
% polyfit command use to generate constant for polynomial
% This command take value of x and y and degree of polynomial
co_effs8 = polyfit(temperature,cp,8);
% polyval function is use to evalute the value of y by taking value of constants and x
predicted_cp8 = polyval(co_effs8, temperature);
% Compare my curve fit with the original curve data
% Generate sub plot
subplot(3,3,8)
plot(temperature, cp, 'linewidth',1.5) % Plot original curve
hold on
plot(temperature, predicted_cp8,'linewidth',1.5,'color','r') % Plot predicted curve
% Labelling and title
xlabel('Temperature [K]')
ylabel('Specific Heat [Kj/Kmol-K]')
legend('original dataset', '8th Degree Curve fit','location','southeast')
title('8th Degree curve fit with original data Curve')
% Curve fit for ninth degree polynomial
% polyfit command use to generate constant for polynomial
% This command take value of x and y and degree of polynomial
co_effs9 = polyfit(temperature,cp,9);
% polyval function is use to evalute the value of y by taking value of constants and x
predicted_cp9 = polyval(co_effs9, temperature);
% Compare my curve fit with the original curve data
% Generate sub plot
subplot(3,3,9)
plot(temperature, cp, 'linewidth',1.5) % Plot original curve
hold on
plot(temperature, predicted_cp9,'linewidth',1.5,'color','r') % Plot predicted curve
% Labelling and title
xlabel('Temperature [K]')
ylabel('Specific Heat [Kj/Kmol-K]')
legend('original dataset', '9th Degree Curve fit','location','southeast')
title('9th Degree curve fit with original data Curve')
% Goodfit checking by R- square and RMSE values
[RMSE1] = goodfit_cal(cp,predicted_cp1);
[RMSE2] = goodfit_cal(cp,predicted_cp2);
[RMSE3] = goodfit_cal(cp,predicted_cp3);
[RMSE4] = goodfit_cal(cp,predicted_cp4);
[RMSE5] = goodfit_cal(cp,predicted_cp5);
[RMSE6] = goodfit_cal(cp,predicted_cp6);
[RMSE7] = goodfit_cal(cp,predicted_cp7);
[RMSE8] = goodfit_cal(cp,predicted_cp8);
[RMSE9] = goodfit_cal(cp,predicted_cp9);
RMSE = [1 RMSE1;2 RMSE2;3 RMSE3;4 RMSE4;5 RMSE5;6 RMSE6;7 RMSE7;8 RMSE8;9 RMSE9]
Result :-
After run the program two solutions are displayed one as a figure and second as a array.
1. Figure result :
2. Array Result : first column is degree of polynomial and second is corresponding RMSE value
Conclusion :-
From this result it is concluded that with increase of degree of polynomial we get more best fit because it is known that RMSE value near to zero provide best fit. It is shown that with increase of polynomial degree, the value of RMSE get decrease. So the polynomial degree is work for getting more best fit.
Answers of questions :
1. How to make a curve fit perfectly?
Perfect fit means, the curve should fit the original curve without showing any errors in that perticular degree of polynomial. Perfect fit can always be a best fit but best fit can not be a perfect fit. Perfect fit can be achieve by interpolation. By interpolation we get very minimized errors which lead to zero errors and we get perfect fit.
2. How to get the best fit?
By increasing the degree of polynomial we can get best fit. After that by use of parametric analysis we can get a best fit.
3. What could be done to improve the cubic fit?
Cubic fit can be improve by parametric study and by sppliting the fit curve into multiple polynomial. By this we get minimum error for particular part and by which improvement in cubic curve get place.
References :-
- Skill lync video lecture.
- Data of temperature and cp provided by skill lync.
- https://en.wikipedia.org/wiki/Curve_fitting
- https://www.statisticshowto.com/curve-fitting/
- https://statisticsbyjim.com/regression/curve-fitting-linear-nonlinear-regression/
- https://corporatefinanceinstitute.com/resources/knowledge/other/sum-of-squares/