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 MATLAB code to fit a linear and cubic polynomial for the cp data. OBJECTIVES : 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. Write a code to show splitwise method. Explain the parameters used to measure…
Abhinav Mahto
updated on 03 Apr 2022
AIM :
To write a MATLAB code to fit a linear and cubic polynomial for the cp data.
OBJECTIVES :
THEORY :
Curve fitting is one of the most powerful and most widely used analysis tools in Origin. It examines the relationship between one or more predictors (independent variables) and a response variable (dependent variable), intending to define a 'best fit' model of the relationship. Origin provides tools for linear, polynomial, and nonlinear curve fitting along with validation and goodness of fit tests.
Goodness of Fit
The term goodness of fit refers to a statistical test that determines how well sample data fits a distribution from a population with a normal distribution. It establishes the discrepancy between the observed values and those expected of the model in a normal distribution case. There are multiple methods to determine the goodness of fit, which include
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 labelled as SSE.
SSE=N∑i=1(yi−ˆyi)2
A value closer to 0 indicates that the model has a smaller random error component and that the fit will be more useful for prediction.
R-Square :-
R-square is the square of the correlation between the response values and the predicted response values. It is also called the square of the multiple correlation coefficient and the coefficient of multiple determination.
R-square 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=N∑i=1(ˆyi−¯yi)2
SST is also called the sum of squares about the mean and is defined as
SST=N∑i=1(yi−¯yi)2
where SST = SSR + SSE. Given these definitions, R-square is expressed as
R-square =SSRSST=1−SSESST
R-square can take on any value between 0 and 1, with a value closer to 1 indicating that a greater proportion of variance is accounted for by the model.
Adjusted R-Square :-
This statistic uses the R-square statistic defined above and adjusts it based on the residual degrees of freedom. The residual degrees of freedom are defined as the number of response values n minus the number of fitted coefficients m estimated from the response values.
v=n−m
v indicates the number of independent pieces of information involving the n">nn1−SSE(n−1)SST(v) data points that are required to calculate the sum of squares.
adjusted R-square =
The adjusted R-square statistic can take on any value less than or equal to 1, with a value closer to 1 indicating a better fit. Negative values can occur when the model contains terms that do not help to predict the response.
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=√SSEv
where v indicates the number of independent pieces of information.
PROGRAM :
Main Program
% Curve Fitting Program on MATLAB
clear all
close all
clc
% Step I : Inputs
cp_data = load("data");
temperature = cp_data(:,1);
cp = cp_data(:,2);
% Curve Fit
% Degree of Polynomial is 1
% cp = a*T + b
co_effs1 = polyfit(temperature,cp,1);
predicted_cp1 = polyval(co_effs1,temperature);
% Degree of Polynomial is 3
% cp = a*T^3 + b*T^2 + c*T + d
co_effs2 = polyfit(temperature,cp,3);
predicted_cp2 = polyval(co_effs2,temperature);
% Linear Fit
figure(1)
plot(temperature,cp,'LineWidth', 3);
hold on
plot(temperature,predicted_cp1,'LineWidth',3,'Color','k')
xlabel('Temperature [ K ]')
ylabel('Specific Heat [ KJ/Kmol-K ]')
legend('Original Data Set','Linear Fit')
title('Linear Fit')
grid on
hold off
% Cubic Curve Fit
figure(2)
plot(temperature,cp,'LineWidth', 3);
hold on
plot(temperature,predicted_cp2,'LineWidth',3,'Color','r')
xlabel('Temperature [ K ]')
ylabel('Specific Heat [ KJ/Kmol-K ]')
legend('Original Data Set','Cubic Curve Fit')
title('Cubic Curve Fit')
grid on
hold off
% Linear Fit vs Cubic Curve Fit
figure(3)
plot(temperature,cp,'LineWidth', 3);
hold on
plot(temperature,predicted_cp1,'LineWidth',3,'Color','k')
plot(temperature,predicted_cp2,'LineWidth',3,'Color','r')
xlabel('Temperature [ K ]')
ylabel('Specific Heat [ KJ/Kmol-K ]')
legend('Original Data Set','Linear Fit','Cubic Curve Fit')
title('Linear Fit vs Cubic Curve Fit')
grid on
hold off
% Check Goodness of Fit for both Linear & Cubic Curve Fit
% For Linear Fit
% Calculating Sum of Squares due to Errors (SSE)
SSE_Linear = sum((cp - predicted_cp1).^2);
% Square of Regression (SSR)
SSR_Linear = sum((predicted_cp1 - mean(cp)).^2);
% Total Sum of Squares (SST)
SST_Linear = SSR_Linear + SSE_Linear;
% R-square
Rsquare_Linear = (SSR_Linear/SST_Linear);
% Adjusted R-square
Adjusted_Rsquare_Linear = 1 - ((SSE_Linear.*(length(cp)+2-1))/(SST_Linear.*length(cp)));
% Root Mean Squared Error (RMSE)
RMSE_Linear = sqrt(SSE_Linear/length(cp));
% For Cubic Fit
% Calculating Sum of Squares due to Errors (SSE)
SSE_Cubic = sum((cp - predicted_cp2).^2);
% Square of Regression (SSR)
SSR_Cubic = sum((predicted_cp2 - mean(cp)).^2);
% Total Sum of Squares (SST)
SST_Cubic = SSR_Cubic + SSE_Cubic;
% R-square
Rsquare_Cubic = (SSR_Cubic/SST_Cubic);
% Adjusted R-square
Adjusted_Rsquare_Cubic = 1 - ((SSE_Cubic.*(length(cp)+4-1))/(SST_Cubic.*length(cp)));
% Root Mean Squared Error (RMSE)
RMSE_Cubic = sqrt(SSE_Cubic/length(cp));
RESULTS :
1. Linear Fit
2. Cubic Curve Fit
Best Fit is observed as we keep on increasing the Degree of Polynomial
3. Linear vs Cubic Curve Fit
% MATLAB Program for Splitwise Curve Fitting
clear all
close all
clc
% Step I : Inputs
cp_data = load("data");
temperature = cp_data(:,1);
cp = cp_data(:,2);
% Step II : Splitting Data into 4 Parts
% Part 1
temperature1 = cp_data(1:800,1);
cp1 = cp_data(1:800,2);
% Part 2
temperature2 = cp_data(801:1600,1);
cp2 = cp_data(801:1600,2);
% Part 3
temperature3 = cp_data(1601:2400,1);
cp3 = cp_data(1601:2400,2);
% Part 4
temperature4 = cp_data(2401:3200,1);
cp4 = cp_data(2401:3200,2);
% Step III : Consider Cubic Polynomial and calulate predicted value of cp
% cp = a*T^3 + b*T^2 + c*T + d
co_effs1 = polyfit(temperature1,cp1,3);
predicted_cp1 = polyval(co_effs1,temperature1);
co_effs2 = polyfit(temperature2,cp2,3);
predicted_cp2 = polyval(co_effs2,temperature2);
co_effs3 = polyfit(temperature3,cp3,3);
predicted_cp3 = polyval(co_effs3,temperature3);
co_effs4 = polyfit(temperature4,cp4,3);
predicted_cp4 = polyval(co_effs4,temperature4);
% Step IV : Plotting Cubic Fit
plot(temperature,cp,'LineWidth',3,'Color','k');
hold on
plot(temperature1,predicted_cp1,'LineWidth',3,'Color','r')
plot(temperature2,predicted_cp2,'LineWidth',3,'Color','g')
plot(temperature3,predicted_cp3,'LineWidth',3,'Color','y')
plot(temperature4,predicted_cp4,'LineWidth',3,'Color','c')
hold off
xlabel('Temperature [ K ]')
ylabel('Specific Heat [ KJ/Kmol-K ]')
legend('Original Data Set','Cubic Curve Fit')
title('Cubic Curve Fit using Splitwise Method')
grid on
RESULTS :
CONCLUSION :
From the above outputs and graphs for both the programs (curve fit and splitwise curve fit), one can conclude that if we increase the order of the equation we will achieve a good fit to the curve. However, if we continue increasing the order of the equation, then at one point we will attain disorder equations, which means points are overlapping, which is called overfit as explained above. For this, the split wise method can be used, in which we can divide the curve into sets and calculate parameters for each set, later combining the sets to get the full curve, which is a good fit.
Q&A :
1. How to make a curve fit perfectly?
A perfect fit means that the curve should match the original curve without any errors as far as that particular polynomial degree is concerned.
2. How to get the best fit?
We can get the best fit by using
To compare the accuracy of the fit, we can evaluate the goodness parameter. When the improvement in the goodness of fit becomes stagnant, the best fit curve is determined.
3. What could be done to improve the cubic fit?
We can improve the cubic fit by splitting the curve into multiple parts and using a different cubic polynomial for each set of temperatures and specific heat values. This reduces errors and provides the best cubic fit.
REFERENCES :
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...
MATLAB Project based on NASA's Thermodynamic Data
AIM : To write a MATLAB Program that helps Parsing NASA's thermodynamic data. OBJECTIVES : To write a function that extracts the 14 coefficients and calculates Specific Heat, Enthalpy & Entropy. Plot the Cp, Enthalpy and Entropy for each species. To calculate the molecular weight of each species. THEORY : Parsing…
03 Apr 2022 01:57 PM IST
MATLAB Project based on Rankine Cycle Simulator
AIM : To create a MATLAB Program that calculate the state points of the Rankine Cycle. OBJECTIVES : To create a Rankine Cycle Simulator using MATLAB. To determine its state points including (i) Net work output & (ii) Back work ratio. Plot its T-S and h-S diagram from the given data. Explain its working. THEORY…
03 Apr 2022 01:56 PM IST
MATLAB Program based on Curve fitting
AIM : To write a MATLAB code to fit a linear and cubic polynomial for the cp data. OBJECTIVES : 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. Write a code to show splitwise method. Explain the parameters used to measure…
03 Apr 2022 01:47 PM IST
MATLAB Program to calculate Drag Force against a Cyclist
AIM: To create a Matlab program to calculate drag force against a cyclist. OBJECTIVES: To understand the flow of air while cycling and calculate the drag force which is acting opposite to it with respect to velocity and drag coefficient. Plot graph of Velocity vs Drag force & Drag Co-efficient vs Drag…
03 Apr 2022 01:43 PM 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.