All Courses
All Courses
Courses by Software
Courses by Semester
Courses by Domain
Tool-focused Courses
Machine learning
POPULAR COURSES
Success Stories
AIM: To perform a curve fit on given data and estimate the fitness characteristics using MATLAB. OBJECTIVE: 1. To plot the linear and cubic fit curves along with the raw data points. Write a code to show split wise method. 2. To validate the fitness characteristics using the curve fitting toolbox. 3. Analyze…
Ayush Ulhas Deshmukh
updated on 15 Aug 2020
AIM: To perform a curve fit on given data and estimate the fitness characteristics using MATLAB.
OBJECTIVE:
1. To plot the linear and cubic fit curves along with the raw data points. Write a code to show split wise method.
2. To validate the fitness characteristics using the curve fitting toolbox.
3. Analyze the following questions:
- How to make a curve fit perfectly?
- How to get the best fit?
- What could be done to improve the cubic fit?
THEORY:
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.
Consider N points in the curve. Assume y = f(x) is the curve fitting equation.
The error for any point i can be given by:
Err(i)=|[y(i)-f(x(i))]|Err(i)=|[y(i)−f(x(i))]|
To remove the modulus sign, we square the error at each point on the curve and add them to get:
SSE=n∑i=1Err(i)2SSE=n∑i=1Err(i)2
Assume the proposed equation is a NthNth degree polynomial function as:
y(x)=A+Bx+Cx2+Dx3+...+Nxn-1y(x)=A+Bx+Cx2+Dx3+...+Nxn−1
Differentiate each SSE with individual coefficient and equate to zero to find the minima and all the coefficients of the above equation:
[∂SSE∂A∂SSE∂B∂SSE∂C∂SSE∂D...∂SSE∂N]=0
This method is called as Least Square Method and commonly used for fitting a polynomial function.
R-squared: It is the correlation between the given values and the predicted values. It determines to what extent the given data is a successful fit. It is also called the square of multiple correlation and coefficient of multiple determination.
The value of R-squared ranges from 0 (worst possible fit) to 1 (best possible fit) and can be calculated as:
R2=SSRSST
SSR=l∑i=1[f(x(i))-Mean]2
SSE=n∑i=1[y(i)-f(x(i))]2
SST=SSR+SSE
where;
SSR = Sum of squares of regression
SST = Sum of squared total
Adjusted R-squared: It is a normalizing variation of R-squared that neglects the variation of R-squared due to insignificant independent variables.
As you keep on adding new variables even if they seem completely unrelated, they will still be related in an infinitesimally small manner.
Adjusted R-squared can be formulated as:
R2adj=1-(1-R2)(n-1)n-k-1
where;
n = number of data points
k = number of independent variables
Root Mean Squared Error: It is the estimate of the standard deviation of a random component in the data. It is also called the fit standard error and standard error of the regression.
Root Mean Squared Error can be formulated as:
RMSE=√SSEn
Best Fit: Refers to the curve that has the least error (maximum R-squared value) with respect to the original curve.
Perfect Fit: Refers to the curve that has the zero error (R-squared value = 1) with respect to the original curve.
SOLUTION:
Programming Language: MATLAB
Procedure/Steps:
1. Load the given data file into the working directory. Assign suitable variables to store the given input values.
2. Create a for loop for generation of polynomial degrees as per the requirement.
3. Compute the coefficients and predicted Cp value using polyfit and polyval commands respectively.
polyfit is a function that computes a least squares polynomial for a give set of data. Polyfit generates the coefficients of the polynomial, which can be used to model a curve to fit the data.
polyval evaluates a polynomial for a given set of x values.
4. The parameters for the goodness of fit (SSE, R-sqr, Adjusted R-sqr, RMSE) are defined with proper relations.
5. The curves are plotted with the help of plot function.
Code #1: The MATLAB program to fit the curve is shown below.
% Program to fit a linear and cubic polynomial for given data.
% Inputs:
file = load('data');
temp = file(:,1);
Cp = file(:,2);
% Solution:
for i=1:3
coeff = polyfit(temp,Cp,i);
Cp_pr(:,i) = polyval(coeff,temp);
% Goodness of Fit
SSE(i) = sum((Cp - Cp_pr(:,i)).^2) % Sum of squared errors
SSR(i) = sum((Cp_pr(:,i) - mean(Cp)).^2); % Sum of squares of regression
SST(i) = SSE(i) + SSR(i); % Sum of squared total
R_sqr(i) = SSR(i)/SST(i) % R-squared
n = length(Cp_pr(:,i)); % number of data points
k = 1; % number of independent variables
R_sqr_adj(i) = 1 - ((1-R_sqr(i))*(n-1)/(n-k-1)) % Adjsuted R-squared
RMSE(i) = sqrt(SSE(i)/n) % Root Mean Squared Error
% Plot:
plot(temp,Cp,'linewidth',4,'color','k')
hold on
plot(temp,Cp_pr,'linewidth',4,'linestyle','-.')
hold off
xlabel('Temperature (K)')
ylabel('Entropy (kJ/kmol-K)')
title('Curve Fit')
grid on
end
% Labeling
legend('Dataset','Curve Fit Linear','Curve Fit Quadratic','Curve Fit Cubic')
Output #1: The program is executed and goodness of fit parameters are computed as shown below.
Plot #1: Entropy varies with respect to Temperature for the original and predicted dataset as shown below.
Curve Fitting Toolbox #1:
Linear:
Quadratic:
Cubic:
Curve Fit Validation #1:
Type | SSE | R-squared | Adjusted R-squared | RMSE |
Linear | 2.1630e+06 | 0.9249 | 0.9249 | 26.0072 |
Quadratic | 5.4011e+05 | 0.9812 | 0.9812 | 12.9978 |
Cubic | 1.0617e+05 | 0.9963 | 0.9963 | 5.7636 |
SSECubic<SSEQuadratic<SSELinear
R2Cubic>R2Quadratic>R2Linear
RMSECubic<RMSEQuadratic<RMSELinear
We can observe that the Cubic form is extrapolated with highest accuracy amongst all.
Code #2: The MATLAB program to split wise fit the curve is shown below.
% Program to split wise curve fit a linear and cubic polynomial for given data.
% Inputs:
file = load('data');
temp = file(:,1);
Cp = file(:,2);
% Solution:
% Splitting the data
temp_1 = file(1:800,1);
temp_2 = file(801:1600,1);
temp_3 = file(1601:2400,1);
temp_4 = file(2401:3200,1);
Cp1 = file(1:800,2);
Cp2 = file(801:1600,2);
Cp3 = file(1601:2400,2);
Cp4 = file(2401:3200,2);
coeff = polyfit(temp,Cp,3);
Cp_pr = polyval(coeff,temp);
% Coefficients in split-wise order
coeff_1 = polyfit(temp_1,Cp1,3);
Cp_pr_1 = polyval(coeff_1,temp_1);
coeff_2 = polyfit(temp_2,Cp2,3);
Cp_pr_2 = polyval(coeff_2,temp_2);
coeff_3 = polyfit(temp_3,Cp3,3);
Cp_pr_3 = polyval(coeff_3,temp_3);
coeff_4 = polyfit(temp_4,Cp4,3);
Cp_pr_4 = polyval(coeff_4,temp_4);
for i = 1:length(Cp)
% Goodness of Fit
SSE = sum((Cp(i) - Cp_pr(i)).^2); % Sum of squared errors
SSR = sum((Cp_pr(i) - mean(Cp(i))).^2); % Sum of squares of regression
SST = SSE + SSR; % Sum of squared total
R_sqr = SSR/SST; % R-squared
n = length(Cp); % number of data points
k = 1; % number of independent variables
R_sqr_adj = 1 - ((1-R_sqr)*(n-1)/(n-k-1)); % Adjsuted R-squared
RMSE = sqrt(SSE/n); % Root Mean Squared Error
end
% Plot:
plot(temp,Cp,'linewidth',5,'color','c')
hold on
plot(temp_1,Cp_pr_1,'linewidth',3,'linestyle','-.')
hold on
plot(temp_2,Cp_pr_2,'linewidth',3,'linestyle','-.')
hold on
plot(temp_3,Cp_pr_3,'linewidth',3,'linestyle','-.')
hold on
plot(temp_4,Cp_pr_4,'linewidth',3,'linestyle','-.')
hold off
xlabel('Temperature (K)')
ylabel('Entropy (kJ/kmol-K)')
title('Curve Fit (Split Wise)')
legend('Dataset','Curve Fit 1','Curve Fit 2','Curve Fit 3','Curve Fit 4')
grid on
Output #2: The program is executed as per given parameters as shown below.
Plot #2: Entropy varies with respect to Temperature for the original and predicted dataset (in split fashion) as shown below.
Curve Fitting Toolbox #2:
Cp1 vs temp_1
Cp2 vs temp_2
Cp3 vs temp_3
Cp4 vs temp_4
Curve Fit Validation #2:
Split No. | SSE | R-squared | Adjusted R-squared | RMSE |
1 | 1.5215e+05 | 0.9994 | 0.9994 | 1.3825 |
2 | 0.1047 | 1.0000 | 1.0000 | 0.0115 |
3 | 0.1047 | 1.0000 | 1.0000 | 0.0115 |
4 | 0.1047 | 1.0000 | 1.0000 | 0.0115 |
We can observe that the sections after the first split are the same without any variation and the curve fit is extrapolated with the highest accuracy thereafter.
CONCLUSION: From the above results, we can infer that:
- A perfect curve fit can be achieved by incorporating a split wise method by dividing the given data and polynomials with a different degree at each range.
- A best fit can be achieved by mimicking original dataset and High R2 value with less error.
- Improvement of the cubic fit can be achieved by 'Center and Scale' command in curve fit toolbox or by increasing the degree of the polynomial.
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...
Simple Crank Mechanism for different cases using HYPERWORKS
AIM #1: To simulate the simple crank mechanism using cylinders (diameter = 2m) in a 2D plane (XY) as shown in the figure below. In the case shown above, Revolute joints are located at P0 and P1. What type of joint must be given at P2 (Joint C) so that the system doesn't have redundancy issues? NOTE: Joint…
26 Mar 2021 03:39 PM IST
Compound Pendulum using HYPERWORKS
AIM: To simulate the motion of a compound pendulum under the influence of gravity as per the figure shown below. - Location of the points are shown in the figure, at P0 and P1 the rods are connected by revolute joints, all characteristic data for the rods/cylinders should be taken from…
22 Mar 2021 08:22 PM IST
Projectile Motion of a Cylinder using HYPERWORKS
AIM: To simulate the projectile motion of a cylinder (diameter = 2m) in a 2D plane (XY) as per the figure shown below. - Initial translation velocity about the center of mass = 10 m/s, Initial angular velocity about the center of mass = 10 rad/s. - All physical attributes for the model must be automatically…
22 Mar 2021 08:13 PM IST
Free Falling Body using HYPERWORKS
AIM: To simulate and plot a free-falling body (point mass) of mass 1 kg and inertial properties 1 kgm2 in the Y-axis (gravity should be 9.81 m/s2 in the negative Y-direction, use dimension of length in meters not mm) A screenshot of the entire interface must be attached including the plot…
22 Mar 2021 08:11 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.