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 that performs curve fitting on the relationship between cp and Temperature and finding the BEST FIT and PERFECT FIT for the following. OBJECTIVE- 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. Title…
Ayush Kumar
updated on 31 Aug 2020
AIM- To write a MATLAB code that performs curve fitting on the relationship between cp and Temperature and finding the BEST FIT and PERFECT FIT for the following.
OBJECTIVE-
THEORY-
Curve fitting is the process of constructing a curve, or mathematical functions, which possess the closest proximity to the real series of data. By curve fitting, we can mathematically construct the functional relationship between the observed dataset and parameter values, etc.
The accuracy of the curve fit can be improved by increasing the degree of the polynomial.
So, a 'Best fit' can be obtained by increasing the degree of the polynomial.
PERFECT FIT : The perfect fit is achieved when the geometric distances from the given points to the fitting curve are zero . This means that the curve fits perfectly on the points .
To get a perfect fit we can increase to a order of the polynomials , this method increases the accuracy and reduces errors .
BEST FIT :The best fit is achieved when the geometric distances from the given points to the fitting curve are minimized, in the least squares sense. Finding the best fit reduces to the minimization of the objective function where denotes the fitting curve.
Upon finding a curve which satisfies the data points there are 4 quantities which help us measure the goodness of fit criteria or how well the equation is a representing the datapoints. There are,
The sum of squares due to error (SSE)
R-square
R-squareAdjusted
R-squareRoot mean squared error (RMSE)
What is the difference between PERFECT FIT and BEST FIT
Best fit simply means that the differences between the actual measured Y values and the Y values predicted by the model equation are minimized. It does not mean a "perfect" fit; in most cases, a least-squares best fit does not go through all the points in the data set.
PERFECT FIT referes to the goodness of FIT,it is determined by the four methods mentioned above.
STRUCTURE OF THE CODE
We are plotting the code for the linear and cubic curve fits
Linear curve fit equation is given by
The cubic curve fit equation is given by
The methods used to get the perfect fit
What that means in layman's terms is that the higher the value of R-square the better the fit, the lower it is the lousier the fit, R-square ranges from a value of 0 to 1
Higher the SSE value lousier the fit and lower the SSE value the better the fit
Lower the RMSE better the Fit
MATLAB CODE
clear all
close all
clc
%Preparing the data
cp_data=load('datacpT'); %We are loading the data for cp[Specific Heat] and Temperature from a file named as 'datacpT'
Temperature=cp_data(:,1);%Provding the first column for Temperature.
cp=cp_data(:,2);%Providing the second column for cp.
figure(1)
plot(Temperature,cp,'linewidth',3);
hold on
%For linear fit
co_eff1=polyfit(Temperature,cp,1);
%Equation will be of the form [cp(predicted)=a*temperature+b]
%Using polyfit we will get the values for a and b
predicted_cp1=polyval(co_eff1,Temperature);
%Calculating the Error value
%Number of Data values
n=3200;
Error_lin = cp-predicted_cp1;
%Calculating the sum of squared errors
SSElin=sum(Error_lin.^2);
%Calculating the SSR
SSRlin=sum((predicted_cp1-mean(cp)).^2);
SSTlin=SSElin +SSRlin;
Rsqrlin=SSRlin/SSTlin;
RMSE_lin=sqrt(SSElin/n);
%We can also calculate the error percentage
error_percentlin=((Error_lin.*100./cp));
fprintf('The maximum error percentage in linear is=');
disp(max(error_percentlin));
% plotting The curve for predicted cp and temperature for linear relation
plot(Temperature,predicted_cp1,'linewidth',3);
xlabel('Temperature');
ylabel('specific Heat');
legend('actual curve','predicted linear curve','location','northwest');
title('curve fitting linear');
%Plotting for cubic fit
%Equation will be of the form
%[cp=a*Temperature^3+b*Temperature^2+c*Temperature+d]
%Using polyfit we will get the values for a,b,c,d
figure(2)
plot(Temperature,cp,'linewidth',3);
hold on;
co_eff4 = polyfit(Temperature,cp,3);
predicted_cp3=polyval(co_eff4,Temperature);
%Number of Data values
n=3200;
Error_cubic = cp-predicted_cp3;
%Calculating the sum of squared errors
SSEcubic=sum(Error_cubic.^2);
%Calculating the SSR
SSRcubic=sum((predicted_cp1-mean(cp)).^2);
SSTcubic=SSEcubic +SSRcubic;
Rsqrcubic=SSRcubic/SSTcubic;
RMSE_cubic=sqrt(SSEcubic/n);
error_percentcubic=((Error_cubic.*100)./cp);
fprintf('The maximum error percentage in cubic is =');
disp(max(error_percentcubic));
plot(Temperature,predicted_cp3,'linewidth',3,'color','green');
xlabel('Temperature');
ylabel('specific Heat');
legend('actual curve','predicted cubic curve','location','northwest');
title('curve fitting cubic');
%Caculating the 4th order polynomial
figure(3)
plot(Temperature,cp,'linewidth',3);
hold on;
co_eff5 = polyfit(Temperature,cp,4);
predicted_cp4=polyval(co_eff5,Temperature);
%Number of Data values
n=3200;
Error_quad = cp-predicted_cp4;
%Calculating the sum of squared errors
SSEquad=sum(Error_quad.^2);
%Calculating the SSR
SSRquad=sum((predicted_cp4-mean(cp)).^2);
SSTquad=SSEquad +SSRquad;
Rsqrquad=SSRquad/SSTquad;
RMSE_quad=sqrt(SSEquad/n);
error_percentquad=((Error_quad.*100)./cp);
fprintf('The maximum error percentage in 4th degree is =');
disp(max(error_percentquad));
plot(Temperature,predicted_cp4,'linewidth',3,'color','yellow');
xlabel('Temperature');
ylabel('specific Heat');
legend('actual curve','predicted fourth degree curve','location','northwest');
title('curve fitting fourth degree');
%Calculating the 5th order polynomial
figure(4)
plot(Temperature,cp,'linewidth',3);
hold on;
co_eff6 = polyfit(Temperature,cp,5);
predicted_cp5=polyval(co_eff6,Temperature);
%Number of Data values
n=3200;
Error_pent = cp-predicted_cp5;
%Calculating the sum of squared errors
SSEpent=sum(Error_pent.^2);
%Calculating the SSR
SSRpent=sum((predicted_cp5-mean(cp)).^2);
SSTpent=SSEpent +SSRpent;
Rsqrpent=SSRpent/SSTpent;
RMSE_pent=sqrt(SSEpent/n);
error_percentpent=((Error_pent.*100)./cp);
fprintf('The maximum error percentage in 5th degree is =');
disp(max(error_percentpent));
plot(Temperature,predicted_cp5,'linewidth',3,'color','cyan');
xlabel('Temperature');
ylabel('specific Heat');
legend('actual curve','predicted fifth degree curve','location','northwest');
title('curve fitting fifth degree');
%Calculating for 6th degree polynomial
figure(5)
plot(Temperature,cp,'linewidth',3);
hold on;
co_eff7 = polyfit(Temperature,cp,6);
predicted_cp6=polyval(co_eff7,Temperature);
%Number of Data values
n=3200;
Error_six = cp-predicted_cp6;
%Calculating the sum of squared errors
SSEsix=sum(Error_six.^2);
%Calculating the SSR
SSRsix=sum((predicted_cp6-mean(cp)).^2);
SSTsix=SSEsix +SSRsix;
Rsqrsix=SSRsix/SSTsix;
RMSE_six=sqrt(SSEsix/n);
error_percentsix=((Error_six.*100)./cp);
fprintf('The maximum error percentage in 6th degree is =');
disp(max(error_percentsix));
plot(Temperature,predicted_cp6,'linewidth',3,'color','magenta');
xlabel('Temperature');
ylabel('specific Heat');
legend('actual curve','predicted sixth degree curve','location','northwest');
title('curve fitting sixth degree');
%Calculating the for the 7th degree polynomial
figure(6)
plot(Temperature,cp,'linewidth',3);
hold on;
co_eff8 = polyfit(Temperature,cp,7);
predicted_cp7=polyval(co_eff8,Temperature);
%Number of Data values
n=3200;
Error_seven = cp-predicted_cp7;
%Calculating the sum of squared errors
SSEseven=sum(Error_seven.^2);
%Calculating the SSR
SSRseven=sum((predicted_cp7-mean(cp)).^2);
SSTseven=SSEseven +SSRseven;
Rsqrseven=SSRseven/SSTseven;
RMSE_seven=sqrt(SSEseven/n);
error_percentseven=((Error_seven.*100)./cp);
fprintf('The maximum error percentage in 7th degree is =');
disp(max(error_percentseven));
plot(Temperature,predicted_cp7,'linewidth',3,'color','black');
xlabel('Temperature');
ylabel('specific Heat');
legend('actual curve','predicted seventh degree curve','location','northwest');
title('curve fitting seventh degree');
%Calculating the for the 8th degree polynomial
figure(7)
plot(Temperature,cp,'linewidth',3);
hold on;
co_eff9 = polyfit(Temperature,cp,8);
predicted_cp8=polyval(co_eff9,Temperature);
%Number of Data values
n=3200;
Error_eight = cp-predicted_cp8;
%Calculating the sum of squared errors
SSEeight=sum(Error_eight.^2);
%Calculating the SSR
SSReight=sum((predicted_cp8-mean(cp)).^2);
SSTeight=SSEeight +SSReight;
Rsqreight=SSReight/SSTeight;
RMSE_eight=sqrt(SSEeight/n);
error_percenteight=((Error_eight.*100)./cp);
fprintf('The maximum error percentage in 8th degree is =');
disp(max(error_percenteight));
plot(Temperature,predicted_cp8,'linewidth',3,'color','red');
xlabel('Temperature');
ylabel('specific Heat');
legend('actual curve','predicted eight degree curve','location','northwest');
title('curve fitting eight degree');
%Curve fitting for the 9th polynomial
figure(8)
plot(Temperature,cp,'linewidth',3);
hold on;
co_eff10 = polyfit(Temperature,cp,9);
predicted_cp9=polyval(co_eff10,Temperature);
%Number of Data values
n=3200;
Error_nine = cp-predicted_cp9;
%Calculating the sum of squared errors
SSEnine=sum(Error_nine.^2);
%Calculating the SSR
SSRnine=sum((predicted_cp9-mean(cp)).^2);
SSTnine=SSEnine +SSRnine;
Rsqrnine=SSRnine/SSTnine;
RMSE_nine=sqrt(SSEnine/n);
error_percentnine=((Error_nine.*100)./cp);
fprintf('The maximum error percentage in 9th degree is =');
disp(max(error_percentnine));
plot(Temperature,predicted_cp9,'linewidth',3,'color','cyan');
xlabel('Temperature');
ylabel('specific Heat');
legend('actual curve','predicted ninth degree curve','location','northwest');
title('curve fitting ninth degree');
%Calclating for ten degrees
figure(9)
plot(Temperature,cp,'linewidth',3);
hold on;
co_eff11 = polyfit(Temperature,cp,10);
predicted_cp10=polyval(co_eff11,Temperature);
%Number of Data values
n=3200;
Error_ten = cp-predicted_cp10;
%Calculating the sum of squared errors
SSEten=sum(Error_ten.^2);
%Calculating the SSR
SSRten=sum((predicted_cp10-mean(cp)).^2);
SSTten=SSEten +SSRten;
Rsqrten=SSRten/SSTten;
RMSE_ten=sqrt(SSEten/n);
error_percentten=((Error_ten.*100)./cp);
fprintf('The maximum error percentage in 10th degree is =');
disp(max(error_percentten));
plot(Temperature,predicted_cp10,'linewidth',3,'color','red');
xlabel('Temperature');
ylabel('specific Heat');
legend('actual curve','predicted tenth degree curve','location','northwest');
title('curve fitting tenth degree');
In the above MATLAB code we have performed the curve fitting upto tenth power polynomial in order to get the perfect curve.
CURVE SHOWING THE LINEAR FIT
The maximum error percentage in linear is= 2.41884605858629
CURVE SHOWING CUBIC FIT
As we can see the cubic fit is almost conciding with the actual curve.
The maximum error percentage in cubic is = 0.886505228158073
CURVE SHOWING ALL THE COMBINED CUBIC ,LINEAR AND ACTUAL CURVE
The maximum error percentage in 4th degree is = 1.30461071041394
The maximum error percentage in 5th degree is = 1.40525112644816
The maximum error percentage in 6th degree is = 1.07025452030099
The maximum error percentage in 7th degree is = 0.578598831359685
The maximum error percentage in 8th degree is = 0.168770128991255
The maximum error percentage in 9th degree is = 0.0415963571347394
The maximum error percentage in 10th degree is = 0.0440668025690463
Error percentage in 10th degree >Error percentage in the 9th degree
Determining the PERFECT FIT
RMSE 9th degree was found out to be =0.173053991108869
RMSE 10th degree was found out to be=0.139927685543301
R square nine was found out to be 1
R square ten was also found out to be 1
Hence we can conclude that the ninth degree fit is the Perfect Fit
\EXPLANATION OF THE CODE\
We have basically used a simple curve fitting Technique here
Step 1-Load the data using the "Load" command in matlab
Step 2- Assign the values for cp and Temperature
Step 3- Plot the actual curve using the plot command Temperature on the x axis and cp on the y axis
Step 4- Consider a linear relation between cp and temperature and using the polyfit command evaluate the coefficients
Step 5- After evaluating the coefficients using polyval command evaluate the predicted cp values
Step 6-Using the plot command plot the curve between predicted cp and Temperature
Step 7- Calculate the error percentage and all the four paramenters to check for the goodness of fit.
We can also calculate the Error percentage using
Calculate the error values and display them in order to find the perfect fit
Step 8-Increase the degree of the polynomial to get the perfect fit.
Errors faced while programming
Error 1-
Note-Do not ignore the warning, because it is easy and more reliably to consider it: Call polyfit with 3 outputs to let x be scaled and shifted automatically:
Solution-
As given in the mathworks website about polyfit function
We need to perform scaling and centering transformation as it improves the numerical properties of both the polynomial and the fitting algorithm.
we have also solved the perfect fit error for cubic equation and higher degrees
The solution so obtained is that if we split the data and then perform the plotting operation then we will get a perfect fit even using a cubic function
MATLAB code for curve fitting using Splitwise method
%Curve fitting using split wise method
%In order to improve the cubic fit we are dividing the data into 8 equal
%parts and then plotting it.
clear all
close all
clc
cp_data=load('datacpT');
cp=cp_data(:,2);
Temperature=cp_data(:,1);
%This time we are splitting the data into eight equal parts
%Data 1 varies fro, 1 to 400
Temperature1=cp_data(1:400,1);
cp1=cp_data(1:400,2);
%using the cubic equation cp=a*t^3+b*t^2+c*t+d[t here stands for
%temperature]
[coeff1,s1,mu1]=polyfit(Temperature1,cp1,3);
cp_pred1=polyval(coeff1,Temperature1,s1,mu1);
%Data 2 varies from 401 to 800
Temperature2=cp_data(401:800,1);
cp2=cp_data(401:800,2);
%using the cubic equation cp=a*t^3+b*t^2+c*t+d[t here stands for
%temperature]
[coeff2,s2,mu2]=polyfit(Temperature2,cp2,3);
cp_pred2=polyval(coeff2,Temperature2,s2,mu2);
%Data 3 varies from 801 to 1200
%using the cubic equation cp=a*t^3+b*t^2+c*t+d[t here stands for
%temperature]
Temperature3=cp_data(801:1200,1);
cp3=cp_data(801:1200,2);
[coeff3,s3,mu3]=polyfit(Temperature3,cp3,3);
cp_pred3=polyval(coeff3,Temperature3,s3,mu3);
%Data 4 varies from 1201 to 1600
Temperature4=cp_data(1201:1600,1);
cp4=cp_data(1201:1600,2);
%using the cubic equation cp=a*t^3+b*t^2+c*t+d[t here stands for
%temperature]
[coeff4,s4,mu4]=polyfit(Temperature4,cp4,3);
cp_pred4=polyval(coeff4,Temperature4,s4,mu4);
%Data 5 varies from 1601 to 2000
Temperature5=cp_data(1601:2000,1);
cp5=cp_data(1601:2000,2);
%using the cubic equation cp=a*t^3+b*t^2+c*t+d[t here stands for
%temperature]
[coeff5,s5,mu5]=polyfit(Temperature5,cp5,3);
cp_pred5=polyval(coeff5,Temperature5,s5,mu5);
%Data 6 varies from 2001 to 2400
Temperature6=cp_data(2001:2400,1);
cp6=cp_data(2001:2400,2);
%using the cubic equation cp=a*t^3+b*t^2+c*t+d[t here stands for
%temperature]
[coeff6,s6,mu6]=polyfit(Temperature6,cp6,3);
cp_pred6=polyval(coeff6,Temperature6,s6,mu6);
%Data 7 varies from 2401 to 2800
Temperature7=cp_data(2401:2800,1);
cp7=cp_data(2401:2800,2);
%using the cubic equation cp=a*t^3+b*t^2+c*t+d[t here stands for
%temperature]
[coeff7,s7,mu7]=polyfit(Temperature7,cp7,3);
cp_pred7=polyval(coeff7,Temperature7,s7,mu7);
%Data 8 varies from 2800 to 3200
Temperature8=cp_data(2800:3200,1);
cp8=cp_data(2800:3200,2);
%using the cubic equation cp=a*t^3+b*t^2+c*t+d[t here stands for
%temperature]
[coeff8,s8,mu8]=polyfit(Temperature8,cp8,3);
cp_pred8=polyval(coeff8,Temperature8,s8,mu8);
%Plotting the actual curve for cp vs Temperature
plot(Temperature,cp,'linewidth',3);
hold on
%Plotting all the splitwise values for cp and temperature
plot(Temperature1,cp_pred1,'linewidth',3);
plot(Temperature2,cp_pred2,'linewidth',3);
plot(Temperature3,cp_pred3,'linewidth',3);
plot(Temperature4,cp_pred4,'linewidth',3);
plot(Temperature5,cp_pred5,'linewidth',3);
plot(Temperature6,cp_pred6,'linewidth',3);
plot(Temperature7,cp_pred7,'linewidth',3);
plot(Temperature8,cp_pred8,'linewidth',3);
xlabel('Temperature');
ylabel('Specific Heat');
title('Split wise cubic curve fitting');
legend('Actual curve','Predicted cubic curve');
CUBIC POLYNOMIAL PLOT USING SPLITWISE METHOD
We can see that after splitting the data the curve coincides with the actual curve
----//Errors faced while programming//----
This is not a complicated code so the only error that can occur is while creating the functions and only minute issues and can be solved by the following methods
Error 1-Saving the name of the file different as that given in matlab
solution--The file name should be the same as that of the function name other wise it may cause error
Error 2- writing the equations wrong ,always write the correct equations and cross check with the value obtained mathematically with the values obtained through the code.
Error 3- While using the plot function always check to give the right parameters
Error4 --Matlab is Case Sensitive so take care of that
Error5-- Make sure to provide % sign before command statements .
Use help command to get information of any function or go to the mathworks website to know more about the function
The link for the above data is
https://skill-lync.com/projects/Curve-Fitting-and-criterion-to-choose-best-fit-41860
The link for cp vs temperature data
https://drive.google.com/file/d/1vWi9Vj3AdQf0gqUK1u9jwS8zg92BHGR0/view
QUESTIONS AND ANSWERS
Curve fit can be made perfect by interpolating the datasets. By interpolation the errors can be minimized and it is also possible to reduce the errors to zero.
The best fit can be obtained by increasing the order of polynomial and also by splitting the curve fits into multiple number of polynomials for different sets of temperature and specific heat values. The best fit can also be chosen by using 4 different types. They are,
The cubic fit can be improved by splitting the curve fits into multiple number of polynomials for different sets of temperature values. It can also be improved by reducing the error between the original dataset and curve fit value and also by centering and scaling operations
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...
Week 6 - Data analysis
AIM :TO WRITE A PYTHON PROGRAM WHICH READS THE ENGINE OUTPUT PARAMETER DATA FILE AND PERFORM THE REQUIRED OBJECTIVES The programming language used is Python 2.OBJECTIVE : To read the data from a given data file. To take the file name as an input from the user and check whether the file is present or not. To plot a graph…
16 Aug 2021 10:01 AM IST
Week 3 - Solving second order ODEs
Aim: To write a program to simulate the transient behavior of a simple pendulum.(Simulate the motion between 0-20 sec, for angular displacement=0,angular velocity=3 rad/sec at time t=0) To create an animation of its motion. Theory: A pendulum is a weight suspended from a pivot so that it can swing freely. When…
14 Jul 2021 08:05 AM IST
Week 5 - Curve fitting
AIM- To write a PYTHON code that performs curve-fitting on the relationship between cp and Temperature and finding the PERFECT FIT for the following. THEORY- Curve fitting is the process of constructing a curve, or mathematical functions, which possess the closest proximity to the real series of…
14 Jul 2021 06:10 AM IST
Week 2 Air standard Cycle
AIM:To generate the PV diagram and to determine the thermal efficiency of the engine. The programming language used is Python THEORY: Otto Cycle is an idealized thermodynamic cycle that describes the working of a spark-ignition engine. The cycle consists of 4 processes as illustrated in the figure below: it consists…
12 Jul 2021 09:38 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.