Project

Curve fitting

Aim: Write a code to fit a linear and cubic polynomial for the CP data and plot curve fit of linear and cubic polynomials. To calculate the goodness of fit using different parameters.

Theory: Curve fitting is the process of constructing a curve or mathematical function, that has the best fit to the series of data points. It examines the relationship between the independent variables and dependent variables, with the goal of defining a "best fit" modal of the relationship.

Code: Code to fit a linear and cubic polynomial for the given cp data.

clear all
close all
clc

%program for the linear curve fit
%preparing data

cp_data = load('data');
temp = cp_data(:,1);                                %temperature 
cp = cp_data(:,2);                                  %Specific Heat

%linear polynomial curve fit

figure(1);
co_eff = polyfit(temp,cp,1);                        %co_efficients
pred_cpl = polyval(co_eff,temp);

%plotting curve

plot(temp,cp,'linewidth',2.5,'color','b');
hold on
plot(temp,pred_cpl,'linewidth',2,'color','r');

mean_cp = mean(cp);
n = length(temp);
sse = 0;
ssr = 0;
k = 2
for i = 1:n;
    SSR1 = ssr+(pred_cpl(i)-mean_cp)^2              %SSR Sum of Squares of Regression
    SSE1 = sse+(cp(i)-pred_cpl(i))^2                %SSE Sum of Squares due to errors
end

SST1 = SSR1+SSE1;
R_sq_lin = (SSR1/SST1)^0.5                          %R-Square(2)

R_adj_lin = (1-((1-R_sq_lin)*(n-1)/(n-k-1)))^0.5    %Adjusted R-Square(3)
RMSE_lin = (SSE1/n)^0.5                             %Root mean squared error(4) 

xlabel('temperature [k]');
ylabel('specific Heat [J/mol-K]');
title('curve fit linear polynomial');
legend('original dataset','predicted curve','location','Northwest');

%cubic polynomial curve fit

figure(2)
co_eff = polyfit(temp,cp,3);
pred_cpc = polyval(co_eff,temp);

%plotting curve

plot(temp,cp,'linewidth',2.5,'color','b');
hold on
plot(temp,pred_cpc,'linewidth',2,'color','r');

mean_cp = mean(cp);
n = length(temp);
sse = 0;
ssr = 0;
k = 2;
for i = 1:n;
    SSR3 = ssr+(pred_cpc(i)-mean_cp)^2
    SSE3 = sse+(cp(i)-pred_cpc(i))^2
end

SST3 = SSR3+SSE3; 
R_sq_cub = (SSR3/SST3)^0.5                        

R_adj_cub = (1-((1-R_sq_cub)*(n-1)/(n-k-1)))^0.5  
RMSE_cub = (SSE3/n)^0.5                           


xlabel('temperature [k]');
ylabel('specific Heat [J/mol-K]');
title('curve fit cubic polynomial');
legend('original dataset','predicted curve','location','Northwest');

Explanation: There are four quantities to measure the goodness of fit.

  1. The Sum of Squares due to errors (SSE)
  2. R-Square
  3. Adjusted
  4. R-square Root Mean Squared Error (RMSE)

Parameters used here are Temperature and specific heat data. For the goodness of fit, quantity R-Square is used which is the square of the correlation between the response values and the predicted response values. As per Layman's terms, higher the value of R-Square the better the fit lower the value of R-Square lousier the fit. Range of R-Square is from 0-1.

R^2` = (SSR/SST) Where SSR - Sum of Square of the Regression.

SSR = ∑i=1(f(x(i))−Mean)^2 #here f(x) = predicted CP
Mean = mean_cp
SSE = ∑i=1(Y(i)−f(x(i)))^2 y(i) = CP

SST = SSR+SSE #SSE- Sum of Squares due to errors
#SST- Sum of Squares of Total

The adjusted R-squared is a modified version of R-squared that has been adjusted for the number of predictors in the model. The adjusted R-squared increases only if the new term improves the model more than would be expected by chance. It decreases when a predictor improves the model by less than expected by chance. The adjusted R-squared can be negative, but it’s usually not. It is always lower than the R-squared.

R^2 adj = 1-[(1-R^2)(n-1)/(n-k-1)] #here n = length(temp)
k = no. independent variables

Root Mean Squared Error(RMSE) This statistic is also known as the fit standard error and the standard error of the regression. It is an estimate of the standard deviation of the random component in the data and is defined as

RMSE = √∑ni=1(Y(i)−f(x(i)))2n
=√SSE/n

In code R^2 of the linear and cubic polynomial is represented as R_sq_lin & R_sq_cubAdjusted R^2 as R_adj_lin & R_adj_cub. SSE as SSE1 and SSE3.

  • In the code, I loaded the data given by the command #load(data);
  • data with the first column 'temperature' used command #(:,1);
  • The second column 'Specific heat' used command (:,2);
  • I used command #polyfit to compute the least squares polynomial for a given set of data.
  • I used command #polyval to evaluate a polynomial for a given set of x values.
  • plot the temperature and original specific heat and compare it with predicted specific heat data.
  • we find the curve seems to be fitted by simply increasing the degree of the polynomial but it is not possible in all cases.
  • An error is displayed in the command window saying "Equation is badly conditioned.
  • Remove repeated data points or try centering and scaling".
  • To remove the error and give goodness of fit quantities shown above or splitting method is used.
  • After using centering and scaling and splitting algorithm error is not displayed even the degree of the polynomial is increased.

Result Plot: plotting of linear and cubic fit curves along with the raw data points.

Values of R-Square and Adjusted R-Square.

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.