All Courses
All Courses
Courses by Software
Courses by Semester
Courses by Domain
Tool-focused Courses
Machine learning
POPULAR COURSES
Success Stories
CURVE FIT
AIM :
To prepare a code to fit the linear and the cubic polynomial for the Cp data and to study the goodness of it for Curve Fitting.
THEORY :
Types of Polynomial Functions -
To measure the goodness of fit criteria or how well the equation is a representing the datapoints there are 4 quantities mainly used such as ,
In sum of squared error we subtract the a point of function y = f(x) at x = x(i) from a point y(i) which gives the vertical error and then we take a square of it.
R2=SSRSST
where,
SST is given by the summation of SSR">SSR and SSE.
SST=SSR+SSE
Adjusted R-square disregards the infinitesimally small increments to R-square, and only considers the variables which actually contribute to the output of the system in a drastic way. In short, it is a normalizing variation of R-square which neglects the variation of R-square due to insignificant independent variables.
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=√SSEn
GOVERNING EQUATIONS :
Mathematically, it is defined as,
SSE=n∑i=1(Y(i)−f(x(i)))2
R2=SSRSST
where,
If y=f(x)"> y=f(x) is the fit curve then the SSR is defined as,
SSR=n∑i=1(f(x(i))−Mean)2
Adjusted R-square is given by,
R2adjust=1−[(1−R2)(n−1)n−k−1]
RMSE is defined by the equation,
RMSE=√∑ni=1(Y(i)−f(x(i)))2n=√SSEn
OBJECTIVES OF THE PROJECT:
MAIN PROGRAM CODE -
clear all
close all
clc
% Preparing the data
Cp_data = load('data'); % Loading the data for Cp and Temp...
Temperature = Cp_data(:,1); % Saving the temperature values of all rows from 1st column
Cp = Cp_data(:,2); % Saving the Specific heat (Cp) values of all rows from 1st column
% Curve Fit for Linear polynomial
% Cp = a*T + b
% Cp = a*T^2 + b*T + c
coefficients = polyfit(Temperature,Cp,1); % Using polyfot for the curve fit and to get the coefficients
Predicted_Cp = polyval(coefficients,Temperature); % Using polyval for the curve fit and to get the Predicted_Cp
% Comparing the Curve Fit with Original data
figure(1)
plot(Temperature,Cp,'linewidth',3) % Plotting the Temperature vs Cp figure for original data
hold on
plot(Temperature,Predicted_Cp,'linewidth',3,'color','g') % Plotting the Temperature vs Predicted_Cp for linear polynomial
title('CURVE FITTING for Linear Polynomial')
xlabel('Temperature [k]') % Writing the labels for X axis i.e. Temnperature
ylabel('Specific Heat (Cp) [kJ/Kmol-K]') % Writing the labels for Y axis i.e. Specific heat
legend('Original Dataset',"Curve Fit")
% Curve Fit for Cubic polynomial
% Cp = a*T^3 + b*T^2 + c*T + d
coefficients_cubic = polyfit(Temperature,Cp,3);
Predicted_Cp_cubic = polyval(coefficients_cubic,Temperature);
% Comparing the Curve Fit with Original data
figure(2)
plot(Temperature,Cp,'linewidth',3) % Plotting the Temperature vs Cp figure for original data
hold on
plot(Temperature,Predicted_Cp_cubic,'linewidth',3,'color','r') % Plotting the Temperature vs Predicted_Cp for Cubic polynomial
title('CURVE FITTING for Cubic Polynomial')
xlabel('Temperature [k]') % Writing the labels for X axis i.e. Temnperature
ylabel('Specific Heat (Cp) [kJ/Kmol-K]') % Writing the labels for Y axis i.e. Specific heat
legend('Original Dataset',"Curve Fit")
% Plotting and Comparing the Linear and Cubic fit with orginal data
figure(3)
plot(Temperature,Cp,'linewidth',3,'color','k') % Plotting the Temperature vs Cp figure for original data
hold on
plot(Temperature,Predicted_Cp,'linewidth',2.5,'color','g') % Plotting the Temperature vs Predicted_Cp for Linear polynomial
hold on
plot(Temperature,Predicted_Cp_cubic,'linewidth',2.5,'color','r') % Plotting the Temperature vs Predicted_Cp for Cubic polynomial
title('CURVE FITTING for Linear and Cubic Fit ')
xlabel('Temperature [k]')
ylabel('Specific Heat (Cp) [kJ/Kmol-K]')
legend('Original Dataset','LINEAR Curve fit','CUBIC Curve fit')
GENERATED PROGRAM CODE -
This is the generated code after using center and scale method for goodness of curve fit by using Curve fitting toolbox.
function [fitresult, gof] = createFits(Temperature, Cp)
% CREATEFITS(TEMPERATURE,CP)
% Create fits.
% Data for 'Linear fit' fit:
% X Input : Temperature
% Y Output: Cp
% Data for 'cubic curve fit 2' fit:
% X Input : Temperature
% Y Output: Cp
% Output:
% fitresult : a cell-array of fit objects representing the fits.
% gof : structure array with goodness-of fit info.
% See also FIT, CFIT, SFIT.
% Auto-generated by MATLAB on 08-Jun-2020 20:17:41
%% Initialization.
% Initialize arrays to store fits and goodness-of-fit.
fitresult = cell( 2, 1 );
gof = struct( 'sse', cell( 2, 1 ), ...
'rsquare', [], 'dfe', [], 'adjrsquare', [], 'rmse', [] );
%% Fit: 'Linear fit'.
[xData, yData] = prepareCurveData( Temperature, Cp );
% Set up fittype and options.
ft = fittype( 'poly1' );
% Fit model to data.
[fitresult{1}, gof(1)] = fit( xData, yData, ft, 'Normalize', 'on' );
% Plot fit with data.
figure( 'Name', 'Linear fit' );
h = plot( fitresult{1}, xData, yData );
legend( h, 'Cp vs. Temperature', 'Linear fit', 'Location', 'NorthEast' );
% Label axes
xlabel Temperature
ylabel Cp
grid on
%% Fit: 'cubic curve fit 2'.
[xData, yData] = prepareCurveData( Temperature, Cp );
% Set up fittype and options.
ft = fittype( 'poly3' );
% Fit model to data.
[fitresult{2}, gof(2)] = fit( xData, yData, ft, 'Normalize', 'on' );
% Plot fit with data.
figure( 'Name', 'cubic curve fit 2' );
h = plot( fitresult{2}, xData, yData );
legend( h, 'Cp vs. Temperature', 'cubic curve fit 2', 'Location', 'NorthEast' );
% Label axes
xlabel Temperature
ylabel Cp
grid on
CALCULATION OF GOODNESS OF FIT using CURVE FIT TOOLBOX :
Here, we had calculated for the goodness of fit for linear polynomial function for curve fit using center and scale method in which polynomial has degree 1 and the R-square value is 0.92 which fine but not best as there are some SSE errors present.
Here, we had calculated for the goodness of fit for Cubic polynomial function for curve fit using center and scale method in which polynomial has degree 3 and the R-square value is 0.99 approx.. to 1 which is a best fit and also it has minimum SSE.
STEPWISE EXPLANATION OF THE CODE :
OUTPUTS -
In the above figure we get the output for the plot for linear curve fit compared with respect to Orginal data set in which the accuracy of the curve is low as to reach the demand for the original data as linear curve fit polynomial function has very low degree of 1.
In the above figure we get the output for the plot for cubic curve fit compared with respect to Orginal data set in which the accuracy of the curve is high as to reach the demand for the original data as Cubic curve fit polynomial function has a degree of 3.
In the above figure we get the output for temperature vs Specific heat (Cp) for orginal data, Linear curve fit and the Cubic curve fit. We can compare the linear curve fit, cubic curve fit with respect to orginal data set and know the results graphically that the cubic curve fit is more accurate and the best fit as compared to linear curve fit as the degree of polynomial is high and errors has diminished in the Cubic curve fit.
Output for goodness of fit for linear curve fit Output for goodness of fit for Cubic Curve fit
Here, in the above results we can clearly see that the linear curve fit has R-square value equal to 0.92 and the Cubic curve fit has the R-square value equal to 0.99 . Thus, Cubic curve fit is a best fit as it is more accurate having better characteristics with having high R-square value equal to 0.99 giving a good curve fit respectively.
Errors faced while programming :
In command window error shows about Polynomial is badly conditioned as we increase the degree of polynomial.
Steps taken to overcome the Errors Faced :
We can reduce the degree of polynomial or the best way is to use Center and scale method. So, here we had used center and scale method by using Curve Fit toolbox for the goodness of fit.
Screenshots showing errors in command window -
CONCLUSION -
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...
MBD Simulation on IC Engine Valve Train
AIM : To create models, assembly & perform Motion analysis for the IC engine valve train . OBJECTIVE : To obtain the results for Valve Lift. Calculate and display the plots for contact force between Cam and Push Rod, Pushrod and Rocker Arm, Rocker Arm and Valve. INTRODUCTION : In IC engines, valve…
08 Dec 2020 12:46 PM IST
STATIC ANALYSIS FOR A PLATE WITH HOLES
AIM : To perform the Static Analysis on two different models of a plate with the holes and the comparison of results among the two models. OBJECTIVE : Prepare a static analysis and get the output for stress, strain and displacement for two models of plate with holes. Compare the final results of static analysis for the…
08 Dec 2020 12:46 PM IST
EV battery module Benefits and Specifications
AIM : To study the benefits of using Battery modules and specifications of an individual module specified for an battery pack. INTRODUCTION : A battery is a device that converts chemical energy into electrical energy and vice versa. The composition of an EV battery might vary slightly depending on the types of electric…
08 Dec 2020 12:45 PM IST
Frequency Analysis of a rotating shaft
AIM : To perform frequency analysis for a rotating shaft with disc in solidworks. OBJECTIVE : Conduct a frequency analysis on the rotating shaft with disc. Determine the 5 different mode shapes and display its resonant frequencies. INTRODUCTION : The goal of modal analysis in a model is to determine the natural mode…
08 Dec 2020 12:44 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.