All Courses
All Courses
Courses by Software
Courses by Semester
Courses by Domain
Tool-focused Courses
Machine learning
POPULAR COURSES
Success Stories
1.AIM: To perform a curve fitting for the given dataset and to find the best fit and perfect fit for that particular dataset using MATLAB. 2.OBJECTIVES OF THE PROJECT: 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 and axes…
Amit Chilap
updated on 15 Feb 2021
1.AIM:
2.OBJECTIVES OF THE PROJECT:
3.THEORY & GOVERNING EQUATIONS:
What is Curve Fitting?
Curve fitting is the process of constructing a curve or mathematical function, that has the best fit to a series of data points, possibly subject to constraints. Curve fitting can involve either interpolation, where an exact fit to the data is required, or smoothing, in which a "smooth" function is constructed that approximately fits the data. It is highly effective in the mathematical modeling of some natural processes. Fitted curves can be used as an aid for data visualization, to infer values of a function where no data are available, and to summarize the relationships among two or more variables Extrapolation refers to the use of a fitted curve beyond the range of the observed data and is subject to a degree of uncertainty since it may reflect the method used to construct the curve as much as it reflects the observed data.
Curve Fitting in MATLAB
MATLAB has two functions, polyfit and polyval, which can quickly and easily fit a set of data points with a polynomial.
Here, the coefficients are the a1, a2.
If you had a straight line, then n = 1, and the equation would be,f(x)=a1x+a2 .
We can see that if f(x) was called y and a1 was called "m" and a2 was called "b" that we would have the familiar equation for a straight line: y = mx + b.
Thus, a straight line is a first-degree polynomial equation. The reason the degree is equal to one is that the "x” in the equation is raised to the power of one it has an exponent of one.
There is another type of polynomials.
Assume data points (x1,y1),(x2,y2),(x3,y3),...(xn,yn) and assume the best fit for these set of points will be y = f(x), the criterion under which the equation of the curve will be the best fit possible is as follows.
Suppose each of these n points has an individual error (difference in y co-ordinate values)
The error for a general "i" point can be given by the function,
To remove the modulus sign, it is effective if we square the error term, when we square the error terms and add the errors for all the "n" points we get,
assume the proposed equation is an nth degree polynomial of the form,
When we differentiate the SSE with each and every coefficient of the proposed equation f(x) and equate it to zero, we can find the minima and value of that coefficient.
Upon solving the multiple systems of equations, we can find the values of all the coefficients.
This method is known as the Least Squares Method and is the most commonly used technique as long as the fitting function is a polynomial.
How to choose the best fit?
Upon finding a curve that satisfies the data points 4 quantities help us measure the goodness of fit criteria or how well the equation is representing the data points. There are,
The first quantity SSE was already measured in the previous section of this article.
This quantity measures how successful the fit is in explaining the variation of the data. Put another way. 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.
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 O to 1
The value of R-square can be calculated using the following formula
where,
SSR = sum of squares of the regression, which is the square of the difference between the value of the fitting function at a given point and the arithmetic mean of the data set.
if y = f(x) is the fit curve then the SSR is defined as follows,
Similarly, SSE is defined as follows,
Now the term SST is given by the summation of SSR and SSE
From here, we can calculate the value of R2.
Adjusted R-square is a normalizing variation of R-square which neglects the variation of R-square due to insignificant independent variables.
for, y=f(x1,x2,x3,...,xn), here, x1,x2,...,xn are the independent variables and is the dependent variable.
Adjusted R-square disregards these infinitesimally small increments to R-square, and only considers the variables which contribute to the output of the system drastically.
R-square is formulated as shown below,
where,
n- the number of data points
k- the number of independent variables
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,
Where “n” is the total number of data points available.
4.BODY OF THE CONTENT:
%OBJECTIVES OF THE PROJECT:
%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 and axes labels are a must, legends could be shown if necessary.
%Write a code to show the split-wise method.
%Explain the parameters used to measure the fitness characteristics for both curves.
close all
clear all
clc
cp_data =load('data'); %Loading the Data File.
temp=cp_data(:,1); %Collecting Temperature Data from Data File.
cp=cp_data(:,2); %Collecting CP Data from Data File.
% Linear Predicted Curve
% cp=a*t+b
coeff_L = polyfit(temp,cp,1); %in build "polyfit()" command is used to find coefficients for Linear Predicted Curve.
Predicted_Cp_L=polyval(coeff_L,temp); %in build "polyval()" command is used to solve and collect CP Data for Linear Predicted Curve.
% Cubic Predicted Curve
% cp=a*t^3+b*t^2+c*t+d
coeff_C = polyfit(temp,cp,3); %in build "polyfit()" command is used to find coefficients for Cubic Predicted Curve.
Predicted_Cp_C=polyval(coeff_C,temp); %in build "polyval()" command is used to solve and collect CP Data for Cubic Predicted Curve.
% Calculation of Goodness of Fit.
% 1.The sum of squares due to error(SSE).
for i=1:length(cp); %Using FOR loop to calculate errors at every value for Predicted Curve.
sse_L(i)=(cp(i)-Predicted_Cp_L(i))^2; %Calculating errors at every value for Linear Predicted Curve.
sse_C(i)=(cp(i)-Predicted_Cp_C(i))^2; %Calculating errors at every value for Cubic Predicted Curve.
end %Ending 'for' function.
SSE_L=sum(sse_L); %Summations of errors of each value of Linear Predicted Curve.
SSE_C=sum(sse_C); %Summations of errors of each value of Cubic Predicted Curve.
% 2.R-Square.
for i=1:length(cp); %Using FOR loop to calculate errors at every value for Predicted Curve.
ssr_L(i)=(Predicted_Cp_L(i)-(sum(cp)/length(cp)))^2; %Calculating SSR every value for Linear Predicted Curve.
ssr_C(i)=(Predicted_Cp_C(i)-(sum(cp)/length(cp)))^2; %Calculating SSR every value for Cubic Predicted Curve.
end %Ending 'for' function.
SSR_L=sum(ssr_L); %Summations of SSR of each value of Linear Predicted Curve.
SST_L=SSE_L+SSR_L; %Calculating SST of Linear Predicted Curve.
Rsq_L=SSR_L/SST_L; %Calculating R-Square value of Linear Predicted Curve.
SSR_C=sum(ssr_C); %Summations of SSR of each value of Cubic Predicted Curve.
SST_C=SSE_C+SSR_C; %Calculating SST of Cubic Predicted Curve.
Rsq_C=SSR_C/SST_C; %Calculating R-Square value of Cubic Predicted Curve.
% 3.Adjusted R-Square.
k_L=length(coeff_L)-1; %Calculating independent variables in Linear Predicted Curve.
Rsq_adj_L=1-[(1-Rsq_L^2)*(length(cp)-1)/(length(cp)-k_L-1)]; %Calculating Adjusted_R-Square value of Linear Predicted Curve.
k_C=length(coeff_C)-1; %Calculating independent variables in Cubic Predicted Curve.
Rsq_adj_C=1-[(1-Rsq_C^2)*(length(cp)-1)/(length(cp)-k_C-1)]; %Calculating Adjusted_R-Square value of Cubic Predicted Curve.
% 4.Root mean square error(RMSE).
RMSE_L=(SSE_L/length(cp))^(0.5); %Calculating RMSE value of Linear Predicted Curve.
RMSE_C=(SSE_C/length(cp))^(0.5); %Calculating RMSE value of Cubic Predicted Curve.
% Putting Errors in form of an Table
error_names = {'SSE';'R-Square';'Adjusted R-Sqaure'; 'RMSE'}; %Title of goodness of curve fit.
Linear_Predicted_Curve=[SSE_L;Rsq_L;Rsq_adj_L;RMSE_L]; %Creating matrix of goodness of curve fit of Linear Predicted Curve.
Cubic_Predicted_Curve=[SSE_C;Rsq_C;Rsq_adj_C;RMSE_C]; %Creating matrix of goodness of curve fit of Cubic Predicted Curve.
disp('Goodness Parameter for Linear and Cubic Curve fit') %Title of Table.
Goodness_Parameters = table(Linear_Predicted_Curve,Cubic_Predicted_Curve,'RowNames',error_names) %Creating Goodness of Curve Fit Table.
% Plotting the CURVES
hold on %Command to add multiple Plots in single Figure.
plot(temp,cp,'color','g','linewidth',5) %Plotting Original Data Curve.
plot(temp,Predicted_Cp_L,'color','r','linewidth',3) %Plotting Linear Predicted Curve.
plot(temp,Predicted_Cp_C,'color','b','linewidth',3) %Plotting Cubic Predicted Curve.
txt1='The Goodness of Fit of'; %Text to display on plot.
txt2='Linear Predicted Curve'; %Text to display on plot.
txt3=sprintf('SSE %1.4f',SSE_L); %Text to display SSE of Linear Predicted Curve on plot.
txt4=sprintf('R^2 %1.4f',Rsq_L); %Text to display R^2 of Linear Predicted Curve on plot.
txt5=sprintf('Adjusted R^2 %1.4f',Rsq_adj_L); %Text to display Adjusted_R^2 of Linear Predicted Curve on plot.
txt6=sprintf('RMSE %1.4f',RMSE_L); %Text to display RMSE of Linear Predicted Curve on plot.
txtL={txt1,txt2,txt3,txt4,txt5,txt6}; %Text to display Goodness of Fit of Linear Predicted Curve on plot.
text(1500,1000,txtL,'HorizontalAlignment','center'); %Position & Text to display Goodness of Fit of Linear Predicted Curve on plot.
txt7='The Goodness of Fit of'; %Text to display on plot.
txt8='Cubic Predicted Curve'; %Text to display on plot.
txt9=sprintf('SSE %1.4f',SSE_C); %Text to display SSE of Cubic Predicted Curve on plot.
txt10=sprintf('R^2 %1.4f',Rsq_C); %Text to display R^2 of Cubic Predicted Curve on plot.
txt11=sprintf('Adjusted R^2 %1.4f',Rsq_adj_C); %Text to display Adjusted_R^2 of Cubic Predicted Curve on plot.
txt12=sprintf('RMSE %1.4f',RMSE_C); %Text to display RMSE of Cubic Predicted Curve on plot.
txtC={txt7,txt8,txt9,txt10,txt11,txt12}; %Text to display Goodness of Fit of Cubic Predicted Curve on plot.
text(3000,1000,txtC,'HorizontalAlignment','center'); %Position & Text to display Goodness of Fit of Cubic Predicted Curve on plot.
xlabel('Temperature [K]') %Labelling X-Axis.
ylabel('Specific Heat (Cp) [KJ/(kg*K)') %Labelling Y-Axis.
legend('Orignal Curve','Linear Predicted Curve','Cubic Predicted Curve','location','northwest') %Adding Legends on Plot.
Using the Curve fitting toolbox in MATLAB to validate your fitness characteristics.
Based on the research you have done, answer the following questions:
%OBJECTIVES OF THE PROJECT:
%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 and axes labels are a must, legends could be shown if necessary.
%Write a code to show the split-wise method.
%Explain the parameters used to measure the fitness characteristics for both curves.
close all
clear all
clc
cp_data =load('data'); %Loading the Data File.
temp=cp_data(:,1); %Collecting Temperature Data from Data File.
cp=cp_data(:,2); %Collecting CP Data from Data File.
for d=1:10 %Using FOR loop to create Predicted Curve at different polynomial degree.
[coeff,S,mu] = polyfit(temp,cp,d); %in build "polyfit()" command is used to find coefficients for Predicted Curve.
Predicted_Cp=polyval(coeff,temp,S,mu); %in build "polyval()" command is used to solve and collect CP Data for Predicted Curve.
% Calculation of Goodness of Fit
% 1.The sum of squares due to error(SSE).
for i=1:length(Predicted_Cp); %Using FOR loop to calculate errors at every value for Predicted Curve.
sse(i)=(cp(i)-Predicted_Cp(i))^2; %Calculating errors square at every value for Predicted Curve.
end %Ending 'for' function.
SSE(d)=sum(sse); %Sumation of errors of each value of Predicted Curve.
% 2.R-Square.
for i=1:length(Predicted_Cp); %Using FOR loop to calculate errors at every value for Predicted Curve.
ssr(i)=(Predicted_Cp(i)-(sum(cp)/length(cp)))^2; %Calculating SSR at every value for Predicted Curve.
end %Ending 'for' function.
SSR(d)=sum(ssr); %Sumation of SSR of each value of Predicted Curve.
SST(d)=SSE(d)+SSR(d); %Calculating SST of Predicted Curve.
Rsq(d)=SSR(d)/SST(d); %Calculating R-Square of Predicted Curve.
% 3.Adjusted R-Square.
k=length(coeff)-1; %Calculating independent variables in Predicted Curve.
Rsq_adj(d)=1-[(1-Rsq(d)^2).*(length(cp)-1)/(length(cp)-k-1)]; %Calculating Adjusted_R-Square value of Predicted Curve.
% 4.Root mean square error(RMSE).
RMSE(d)=(SSE(d)/length(cp))^(0.5); %Calculating RMSE value of Predicted Curve.
% Plotting the CURVES
plot(temp,cp,'color','g','linewidth',6) %Ploting Original Data Curve.
hold on %Command to add multiple Plots in a single Figure.
plot(temp,Predicted_Cp,'color','r','linewidth',3) %Ploting Predicted Curve.
txt1=sprintf('Degree of Polynomial is %1.0f',d);%Text to display degree of polynomial of Predicted Curve on plot.
txt2='The Goodness of Fit'; %Text to display on the plot.
txt3=sprintf('SSE %1.4f',SSE(d)); %Text to display SSE of Predicted Curve on plot.
txt4=sprintf('R^2 %1.4f',Rsq(d)); %Text to display R^2 of Predicted Curve on plot.
txt5=sprintf('Adjusted R^2 %1.4f',Rsq_adj(d)); %Text to display Adujsted_R^2 of Predicted Curve on plot.
txt6=sprintf('RMSE %1.4f',RMSE(d)); %Text to display RMSE of Predicted Curve on plot.
txt={txt1,txt2,txt3 txt4,txt5,txt6}; %Text to display Goodness of Fit of Predicted Curve on plot.
text(2000,1000,txt,'HorizontalAlignment','center'); %Postion & Text to display Goodness of Fit of Predicted Curve on plot.
hold off %Command to stop adding multiple Plots in a single Figure.
xlabel('Temperature [K]') %Labelling X-Axis.
ylabel('Specific Heat (Cp) [KJ/(kg*K)') %Labelling Y-Axis.
legend('Orignal Curve','Predicted Curve','location','northwest') %Adding Legends on Plot.
M(d)=getframe(gcf); %Convert the Plot Figure into movie frame.
pause(3); %Pause time between the two consecutive Plot Figures.
end %Ending 'for' function.
movie(M) %Function used to make a movie/animation from movie frames.
videofile=VideoWriter('Best_Curve_Fit_by_Poly_Degree.avi','Uncompressed AVI'); %Creating Video file name and profile of it.
open(videofile) %Opening the video file.
writeVideo(videofile,M) %Writing in the video file with collected movie frames.
close(videofile) %Closing the video file.
Explanation/Workflow for Best Curve Fit by Poly Degree
%OBJECTIVES OF THE PROJECT:
%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 and axes labels are a must, legends could be shown if necessary.
%Write a code to show the split-wise method.
%Explain the parameters used to measure the fitness characteristics for both curves.
close all
clear all
clc
cp_data =load('data'); %Loading the Data File.
temp=cp_data(:,1); %Collecting Temperature Data from Data File.
cp=cp_data(:,2); %Collecting CP Data from Data File.
for d=[1,3]%Using FOR loop to create Predicted Curve at Linear & Cubic degree.
for s=1:10; %Using FOR loop to Split the curve at Different Quantity.
a=floor(linspace(1,3200,s+1)); %Splitting the curve in equal Amount.
%floor(X) rounds each element of X to the nearest integer less than or equal to that element.
for i=1:length(a)-1 %Using FOR loop to create Predicted Curve at Different Splitted Sections.
b=a(:,i); %Collecting Starting point of Section.
c=a(:,i+1); %Collecting Ending point of Section.
temp_i=temp(b:c); %Collecting Temperature data for/of a Section.
cp_i=cp(b:c); %Collecting CP data for/of a Section.
[coeff_i,S,mu] = polyfit(temp_i,cp_i,d); %in build "polyfit()" command is used to find coefficients for Predicted Curve.
Predicted_Cp(b:c)=polyval(coeff_i,temp_i,S,mu); %in build "polyval()" command is used to solve and collect CP Data for Predicted Curve.
end
% Calculation of Goodness of Fit
% 1.The sum of squares due to error(SSE).
for i=1:length(Predicted_Cp); %Using FOR loop to calculate errors at every value for Predicted Curve.
sse(i)=(cp(i)-Predicted_Cp(i))^2; %Calculating error square at every value for Predicted Curve.
end %Ending 'for' function.
SSE(s)=sum(sse); %Sumation of errors of each value of Predicted Curve.
% 2.R-Square.
for i=1:length(Predicted_Cp); %Using FOR loop to calculate errors at every value for Predicted Curve.
ssr(i)=(Predicted_Cp(i)-(sum(cp)/length(cp)))^2; %Calculating SSR at every value for Predicted Curve.
end %Ending 'for' function.
SSR(s)=sum(ssr); %Sumation of SSR of each value of Predicted Curve.
SST(s)=SSE(s)+SSR(s); %Calculating SST of Predicted Curve.
Rsq(s)=SSR(s)/SST(s); %Calculating R-Square of Predicted Curve.
% 3.Adjusted R-Square.
k=length(coeff_i)-1; %Calculating independent variables in Predicted Curve.
Rsq_adj(s)=1-[(1-Rsq(s)^2).*(length(cp)-1)/(length(cp)-k-1)]; %Calculating Adjusted_R-Square value of Predicted Curve.
% 4.Root mean square error(RMSE).
RMSE(s)=(SSE(s)/length(cp))^(0.5); %Calculating RMSE value of Predicted Curve.
%Plotting the Surves
plot(temp,cp,'color','g','linewidth',6) %Ploting orignal Data Curve.
hold on %Command to add multiple Plots in a single Figure.
plot(temp,Predicted_Cp,'color','r','linewidth',3) %Ploting Predicted Curve.
plot(temp(a),Predicted_Cp(a),'o','markersize',3,'markerfacecolor',[0 0 0 ]) %Marking start point of Predicted Curves.
txt1=sprintf('Splitting curve into %1.0f equal sections',s);%Text to display no. of sections(split) of Predicted Curve on plot.
txt2=sprintf('Degree of Polynomial is %1.0f',d); %Text to display on plot.
txt3='The Goodness of Fit'; %Text to display the degree of the polynomial of Predicted Curve on the plot.
txt4=sprintf('SSE %1.4f',SSE(s)); %Text to display SSE of Predicted Curve on plot.
txt5=sprintf('R^2 %1.4f',Rsq(s)); %Text to display R^2 of Predicted Curve on plot.
txt6=sprintf('Adjusted R^2 %1.4f',Rsq_adj(s)); %Text to display Adjusted_R^2 of Predicted Curve on plot.
txt7=sprintf('RMSE %1.4f',RMSE(s)); %Text to display RMSE of Predicted Curve on plot.
txt={txt1,txt2,txt3,txt4,txt5,txt6,txt7}; %Text to display Goodness of Fit of Predicted Curve on plot.
text(2000,1000,txt,'HorizontalAlignment','center'); %Postion & Text to display Goodness of Fit of Predicted Curve on plot.
hold off %Command to stop adding multiple Plots in a single Figure.
xlabel('Temperature [K]') %Labelling X-Axis.
ylabel('Specific Heat (Cp) [KJ/(kg*K)') %Labelling Y-Axis.
legend('Orignal Curve','Predicted Curve','location','northwest') %Adding Legends on Plot.
if d==1; %Using if function to collect Plot figures of Linear Predicted Curve
M1(s)=getframe(gcf); %Convert the Plot Figure into movie frame.
pause(1); %Pause time between the two consecutive Plot Figures.
elseif d==3; %Using elseif function to collect Plot figures of Cubic Predicted Curve
M3(s)=getframe(gcf); %Convert the Plot Figure into movie frame.
pause(1); %Pause time between the two consecutive Plot Figures.
end %Ending 'if' function.
end %Ending 'for' function.
end %Ending 'for' function.
movie(M1) %Function used to make a movie/animation from movie frames.
videofile=VideoWriter('Best_Linear_Curve_Fit_by_Splitting.avi','Uncompressed AVI'); %Creating Video file name and profile of it.
open(videofile) %Opening the video file.
writeVideo(videofile,M1) %Writing in the video file with collected movie frames.
close(videofile) %Closing the video file.
movie(M3) %Function used to make a movie/animation from movie frames.
videofile=VideoWriter('Best_Cubic_Curve_Fit_by_Splitting.avi','Uncompressed AVI'); %Creating Video file name and profile of it.
open(videofile) %Opening the video file.
writeVideo(videofile,M3) %Writing in the video file with collected movie frames.
close(videofile) %Closing the video file.
Explanation/Workflow for Best Curve Fit by Splitting
5.CONCLUSION:
6.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...
Week-3 Challenge: ADVISOR Tool
Introduction to HEV using MATLAB & Simulink Week-3 Challenge: ADVISOR Tool AIM: To simulate the given data and conditions for an EV using Advisor Tool in MATLAB. About ADVISOR ADVISOR, NREL’s Advanced Vehicle Simulator, is a set of model, data, and script text files for use with MATLAB and Simulink. It…
04 Jul 2022 11:04 AM IST
Project -BAJA All Terrain Vehicle (ATV) model
Simulink for Mechanical & Electrical Engineers - Challenges Final Project Aim To study, analyze and make a detailed report on BAJA All Terrain Vehicle (ATV) model using Simulink & compare between its different modes. Objective Prepare a technical report explaining the model properties & comments on the results.…
03 Jun 2021 03:25 AM IST
Week - 4
Simulink for Mechanical & Electrical Engineers Challenges = Week 4 Aim To Make a Simulink model using State-Flow for given questions. Questions & Solution Q1. Implement control logic of a “washing machine” using Stateflow as per given sequence: If the power supply is available, the system gets activated. If the Water supply…
21 May 2021 06:29 PM IST
Week -2
Simulink for Mechanical & Electrical Engineers Challenges = Week 2 Aim To Make a Simulink model of Doorbell using solenoid block. To Use a thermistor to sense the temperature of a heater & turn on or turn off the fan according to temperature. Questions & Solution Q1. Make a Simulink model of Doorbell using…
14 May 2021 12:30 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.