All Courses
All Courses
Courses by Software
Courses by Semester
Courses by Domain
Tool-focused Courses
Machine learning
POPULAR COURSES
Success Stories
AIM: To write a program to fit a linear and cubic polynomial for the specific heat data set then calculate the goodness of fit using different parameters and different ways to improve the fit in MATLAB THEORY: Curve fitting is the process of constructing a curve or mathematical function that best fits the data points,…
Shubhranshu Mishra
updated on 03 Jul 2020
AIM:
To write a program to fit a linear and cubic polynomial for the specific heat data set then calculate the goodness of fit using different parameters and different ways to improve the fit in MATLAB
THEORY:
Curve fitting is the process of constructing a curve or mathematical function that best fits the data points, possibly subjected to constraints. It helps us in understanding the type of relationship between the set of data and helps in predicting the unknown values.
The Goodness of fit of statistical data determines how well it fits a set of observations. Measures of Goodness of fit typically summarize the discrepancy between observed values and the values expected under the model in question. There are four methods to quantify the goodness of fit.
They are:
The Sum of Squares due to Error (SSE) – This statistic measures the total deviation of the response values from the fit to the response values. It is also called the summed square of residuals and is usually labeled as SSE.
SSE=∑ni=1wi(yi−yˆi)2SSE=∑i=1nwi(yi-ŷi)2
R-square – This statistic measures how successful the fit is in explaining the variation of the data. It can also be understood as the square of the correlation between the response values and the predicted response values. It is defined as the ratio of the sum of squares of the regression (SSR) and the total sum of squares (SST).
SSR is defined as SSR=∑ni=1wi(yˆi−y̅)2SSR=∑i=1nwi(ŷi-y̅)2
SST is also called the sum of squares about the mean. SST=∑ni=1wi(yi−y̅)2SST=∑i=1nwi(yi-y̅)2
R – square R2=SSRSSTR2=SSRSST
Its value range is in between 0 and 1
Adjusted R-square – This statistic uses the R – square statistic defined above, and adjusts it based on the residual degrees of freedom. It is generally the best indicator of the fir quality when you compare two models that are nested – that is, a series of models each of which adds additional coefficients to the previous model.
Adjusted R-square =1−[(1−R2)(n−1)n−k−1]=1-[(1-R2)(n-1)n-k-1]
where, n – number of data points and k – number of 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.
RMSE =√(SSEn)=√(SSEn) where, n – total number of data points
Using these four quantities MATLAB effectively deduces the goodness of fit, typically R – square is greater than 0.95 the fit is assumed to be good.
PROCEDURE:
FOR LINEAR, AND CUBIC FIT
FOR IMPROVING THE FIT – The fit is divided into 3 parts so that it can fit properly, if needed it can be divided in more parts to get more precise result
CODE:
FOR LINEAR, AND CUBIC FIT
% curve fitting
clear
close all
clc
% preparing the data
cp_data = load('data'); %loads the data and then saves it in the form of array
temperature = cp_data(:,1); % stores first column as temperature
cp = cp_data(:,2); % stores second column as cp
% curve fit for 1 degree polynomial
coeffs1 = polyfit(temperature,cp,1); % calculates the value of coefficients of 1 degree polynomial equation
predicted_cp1 = polyval(coeffs1,temperature); % calculates the value of cp based on the value of coefficient and temperature
% curve fit for 3 degree polynomial
coeffs3 = polyfit(temperature,cp,3); % calculates the value of coefficients of 3 degree polynomial equation
predicted_cp3 = polyval(coeffs3,temperature); % calculates the value of cp based on the value of coefficient and temperature
% comparing my curve fit for 1 degree polynomial with original data
figure(1) % to open a new figure window
plot(temperature,cp,'linewidth',3) % plotting original data
hold on % to plot another set of data in the same graph
plot(temperature,predicted_cp1,'linewidth',3,'color','r') % plotting the 1 degree polynomial data
xlabel('Temperature [K]') % labelling x-axis
ylabel('Specific Heat [KJ/Kmol-K]') % labelling y-axis
legend('Original data','Curve fit') % providing legends to differentiate the data
title('1 Degree Polynomial') % giving a title
hold off
% comparing my curve fit for 3 degree polynomial with original data
figure(3) % to open a new figure window
plot(temperature,cp,'linewidth',3) % plotting original data
hold on % to plot another set of data in the same graph
plot(temperature,predicted_cp3,'linewidth',3,'color','r') % plotting the 3 degree polynomial data
xlabel('Temperature [K]') % labelling x-axis
ylabel('Specific Heat [KJ/Kmol-K]') % labelling y-axis
legend('Original data','Curve fit') % providing legends to differentiate the data
title('3 Degree Polynomial') % giving a title
hold off
% calculating the SSE, SSR, SST, R_sq, R_sq_adj, RMSE for 1 dgree polynomial
error1 = predicted_cp1 - cp;
n = length(cp) % number of data
k = 2 % number of independent variables
%mean
M = mean(cp)
% calculating SSE1
sse1 = (error1).^2;
SSE1 = sum(sse1)
% calculating SSR1
ssr1 = (predicted_cp1 - M).^2;
SSR1 = sum(ssr1);
% calculating SST1
SST1 = SSE1 + SSR1;
% calculating R_sq1
R_sq1 = SSR1/SST1
% calculating R_sq_adj1
R_sq_adj1 = 1-(((1-R_sq1)*(n-1))/(n-k-1))
% calculating RMSE1
RMSE1 = (SSE1/n)^0.5
% calculating the SSR, SST, R_sq, R_sq_adj, RMSE for 3 dgree polynomial
error3 = predicted_cp3 - cp;
n = length(cp)
k = 3 % number of independent variables
%mean
M = mean(cp)
% calculating SSE3
sse3 = (error3).^2;
SSE3 = sum(sse3)
% calculating SSR3
ssr3 = (predicted_cp3 - M).^2;
SSR3 = sum(ssr3);
% calculating SST3
SST3 = SSE3 + SSR3;
% calculating R_sq3
R_sq3 = SSR3/SST3
% calculating R_sq_adj3
R_sq_adj3 = 1-(((1-R_sq3)*(n-1))/(n-k-1))
% calculating RMSE3
RMSE3 = (SSE3/n)^0.5
FOR IMPROVING THE FIT
% curve fitting - improving the fit by using centering and scaling
clear
close all
clc
% preparing the data
cp_data = load('data');
temperature = cp_data(:,1); % first column as the temperature
cp = cp_data(:,2); % second column as the cp
temperature1 = temperature(1:800); % temperature is divided in 1 to 800
predicted_cp1 = cp(1:800); % cp is divided in 1 to 800
temperature2 = temperature(801:1600); % temperature is divided in 801 to 1600
predicted_cp2 = cp(801:1600); % cp is divided in 801 to 1600
temperature3 = temperature(1601:2400); % temperature is divided in 1601 to 2400
predicted_cp3 = cp(1601:2400); % cp is divided in 1601 to 2400
temperature4 = temperature(2401:3200); % temperature is divided in 2401 to 3200
predicted_cp4 = cp(2401:3200); % cp is divided in 2401 to 3200
% curve fit for 1 to 800 using centering and scaling
[coeffs1,~,mu] = polyfit(temperature1,predicted_cp1,5); % calculates the value of coefficients of 5 degree polynomial equation
cp1 = polyval(coeffs1,temperature1,[],mu); % calculates the value of cp1 based on the value of coefficient1 and temperature1
% curve fit for 801 to 1600 using centering and scaling
[coeffs2,~,mu] = polyfit(temperature2,predicted_cp2,5); % calculates the value of coefficients of 5 degree polynomial equation
cp2 = polyval(coeffs2,temperature2,[],mu); % calculates the value of cp2 based on the value of coefficient2 and temperature2
% curve fit for 1601 to 2400 using centering and scaling
[coeffs3,~,mu] = polyfit(temperature3,predicted_cp3,5); % calculates the value of coefficients of 5 degree polynomial equation
cp3 = polyval(coeffs3,temperature3,[],mu); % calculates the value of cp3 based on the value of coefficient3 and temperature3
% curve fit for 2401 to 3200 using centering and scaling
[coeffs4,~,mu] = polyfit(temperature4,predicted_cp4,5); % calculates the value of coefficients of 5 degree polynomial equation
cp4 = polyval(coeffs4,temperature4,[],mu); % calculates the value of cp4 based on the value of coefficient4 and temperature4
% plotting
plot(temperature,cp,'linewidth',3,'o') % original data
hold on
plot(temperature1,cp1,'linewidth',3,'color','r') % data from 1 to 800
plot(temperature2,cp2,'linewidth',3,'color','g') % data from 801 to 1600
plot(temperature3,cp3,'linewidth',3,'color','y') % data from 1601 to 2400
plot(temperature4,cp4,'linewidth',3,'color','m') % data from 2401 to 3200
axis([0 3600 900 1350]) % setting the axiz limits
hold off
xlabel('Temperature [K]') % labelling x-axis
ylabel('Specific Heat [KJ/Kmol-K]') % labelling y-axis
title('Curve Fitting of Specific Heat vs Temperature') % giving a title to the plot
legend('Original Data','Data for 1-800','Data for 801-1600','Data for 1601-2400','Data for 2401-3200')
grid on
RESULTS:
FOR LINEAR, AND CUBIC FIT
USING THE CURVE FITTING TOOLBOX IN MATLAB TO VALIDATE THE FITNESS CHARACTERISTICS
FOR IMPROVING THE FIT
CONCLUSIONS:
Thus we can see that R – square parameter for the linear polynomial equation is 0.9249 whereas for the cubic polynomial equation it is 0.9967 which means the degree of the polynomial increases the curve fitting also increases. For R – square the higher the number the better the fit, the lower the number the lousier the fit, its value ranges from 0 to 1. By using centering and scaling in “polyfit” command polynomial equation of the 5th degree can be plotted without getting any warnings.
Leave a comment
Thanks for choosing to leave a comment. Please keep in mind that all the comments are moderated as per our comment policy, and your email will not be published for privacy reasons. Please leave a personal & meaningful conversation.
Other comments...
Frequency Analysis of a rotating shaft (Finite Element Analysis using SolidWorks)
Aim- The aim of this project is to perform a frequency analysis on a rotating shaft, from there we need to determine the critical frequencies and the mode shapes. 5 Mode Shapes were simulated and analyzed. Introduction:- Frequency is the number of occurrences of a repeating event per unit of time. The formula…
06 Jul 2020 03:57 PM IST
Project - Rankine cycle Simulator (MATLAB)
AIM: To create a basic 'RANKINE CYCLE SIMULATOR'. THEORY: The Rankine cycle is the fundamental operating cycle of all power plants where an operating fluid is continuously evaporated and condensed. The selection of operating fluid depends mainly on the available temperature range. The above figure shows us the basic rankine…
03 Jul 2020 10:43 AM IST
Curve fitting (MATLAB)
AIM: To write a program to fit a linear and cubic polynomial for the specific heat data set then calculate the goodness of fit using different parameters and different ways to improve the fit in MATLAB THEORY: Curve fitting is the process of constructing a curve or mathematical function that best fits the data points,…
03 Jul 2020 10:24 AM IST
Solving second order ODEs (MATLAB)
Aim: To solves the ODE which represents the equation of motion of a simple pendulum with damping. Objective: To write a program that solves the following ODE which represents the equation of motion of a simple pendulum with damping and create an animated video of output obtains by solving this ODE. Theory:…
03 Jul 2020 10:20 AM IST
Related Courses
Skill-Lync offers industry relevant advanced engineering courses for engineering students by partnering with industry experts.
© 2025 Skill-Lync Inc. All Rights Reserved.