All Courses
All Courses
Courses by Software
Courses by Semester
Courses by Domain
Tool-focused Courses
Machine learning
POPULAR COURSES
Success Stories
AIM: CURVE FITTING USING MATLAB OBJECTIVE: Linear and cubic polynomial curve fitting to a set of data points. Plotting predicted curves against raw data points. Studding parameters that measure the goodness of a fit. THEORY: Curve fitting is the process of constructing a curve or mathematical function,…
NAVEEN SWAMI
updated on 09 May 2020
AIM: CURVE FITTING USING MATLAB
OBJECTIVE:
THEORY:
Curve fitting is the process of constructing a curve or mathematical function, that has the best fit to a series of data points. 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. By curve fitting, we can capture the trend in the data and make predictions of how the data series will behave in the future.
Types of curve fitting include:
Linear and Polynomial Curve Fitting:
Linear curve fitting, or linear regression, is when the data is fit to a straight line. A straight line provides a reasonable enough fit to make predictions.
Since the equation of a generic straight line is always given by,
f(x)= a x + b, equation for linear curve fitting
The goal is to identify the coefficients ‘a’ and ‘b’ such that f(x) ‘fits’ the data well.
How to find coefficients for best fit?
Considering the vertical distance from each point to a prospective line as an error, and summing them up over our range, gives us a concrete number that expresses how far from ‘best’ the prospective line is. A line that provides a minimum error can be considered the best straight line.
Since it’s the distance from our points to the line we’re interested in—whether it is positive or negative distance is not relevant—we square the distance in our error calculations. This also allows us to weight greater errors more heavily. So this method is called the least-squares approach.
Polynomial curve fitting is when we fit our data to the graph of a polynomial function. The same least-squares method can be used to find the polynomial, of a given degree, that has a minimum total error.
PROGRAM:
%curve fitting of data points
clear all
close all
clc
%load data set
data=load('data');
%assigning data
temp=data(:,1);
cp=data(:,2);
%linear curve fitting
coef_linear=polyfit(temp,cp,1);
p_cp_linear = polyval(coef_linear,temp);
%plotting linear fitting vs raw data point
figure(1)
plot(temp,cp,'--');
hold on;
plot(temp,p_cp_linear,'linewidth',2);
xlabel('Temperature (K)');
ylabel('Specific Heat (KJ/kg)');
title('Linear Curve Fitting');
legend('data points','predicted curve');
grid on;
%cubic curve fitting
coef_cubic=polyfit(temp,cp,3);
p_cp_cubic=polyval(coef_cubic,temp);
%plotting cubic fitting vs raw data point
figure(2)
plot(temp,cp,'--');
hold on;
plot(temp,p_cp_cubic,'linewidth',2);
xlabel('Temperature (K)');
ylabel('Specific Heat (KJ/kg)');
title('Cubic Curve Fitting');
legend('data points','predicted curve');
grid on;
%parameters to measure goodness of fit
%for linear curve fitting
%SSE calculation
SSE=0;
for i=1:length(cp)
SSE = SSE + (cp(i)-p_cp_linear(i))^2;
end
SSE
%R^2 calculation
mean=(sum(cp))/(length(cp));
SSR=0;
for i=1:length(cp)
SSR=SSR + (p_cp_linear(i)-mean)^2;
end
SST=SSE+SSR;
R_square=SSR/SST
%RMSE calculation
RMSE=sqrt(SSE/length(cp))
%parameters to measure goodness of fit
%for cubic curve fitting
%SSE calculation
SSE_c=0;
for i=1:length(cp)
SSE_c = SSE_c + (cp(i)-p_cp_cubic(i))^2;
end
SSE_c
%R^2 calculation
mean=(sum(cp))/(length(cp));
SSR_c=0;
for i=1:length(cp)
SSR_c=SSR_c + (p_cp_cubic(i)-mean)^2;
end
SST_c=SSE_c+SSR_c;
R_square_c=SSR_c/SST_c
%RMSE calculation
RMSE_c=sqrt(SSE_c/length(cp))
STEPS:
ERRORS: No error encountered
GOODNESS OF FIT:
There are four quantities that help us measure the goodness of fit or how well the predicted curve is representing the raw data points:
Assume data points (x1,y1),(x2,y2)………(xn,yn) and assume curve fit for these data points to be y=f(x),
when we differentiate the SSE with each and every coefficient of f(x) and equate to zero, we find the values of coefficients
This method is known as LEAST SQUARE METHOD.
R-square is the square of the correlation between the response values and the predicted values.
Adjusted R-square is a normalizing variation of R-square which neglects the variation of R-square due to insignificant independent variable.
n = no. of data points
OUTPUT:
Command window output:
Linear fitting cubic fitting
We can compare the value of R-square in linear and cubic curve fitting. R-square value of Cubic fitting is closer to 1.
By minimizing the SSE we can make a curve to perfectly fit data points. With a high degree of the polynomial we get less value off SSE and more value of R-square(closer to 1), this results in high accuracy of curve fitting
REFERENCE:
https://projects.skill-lync.com/projects/Curve-Fitting-and-criterion-to-choose-best-fit-41860
https://www.iiserpune.ac.in/~bhasbapat/phy221_files/curvefitting.pdf
https://en.wikipedia.org/wiki/Curve_fitting
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: MOTION ANALYSIS OF IC ENGINE VALVE TRAIN MECHANISM THEORY: Valve Train is a mechanical system in an IC engine that is responsible for controlling the intake and the exhaust of the combustion process. During the intake process, the intake valve opens to let air enter the combustion chamber and during the exhaust process,…
21 Jun 2020 08:00 AM IST
MOTION ANALYSIS OF IC ENGINE PISTON ASSEMBLY MECHANISM
AIM: MOTION ANALYSIS OF IC ENGINE PISTON ASSEMBLY MECHANISM. THEORY: In IC Engine, the combustion is driven by Slider Crank Mechanism. Inside the cylinder, the piston performs reciprocating motion which results in compression and expansion of gas inside the cylinder. This motion of the piston is guided by the Slider Crank…
18 Jun 2020 06:12 AM IST
MOTION ANALYSIS OF PLANETARY GEAR SYSTEM
AIM: MOTION ANALYSIS OF PLANETARY GEAR SYSTEM THEORY: A planetary gear mechanism also is known as Epicyclic Gear Train, is a gear mechanism consisting of 4 components, namely, sun gear A, several planet gears B, internal gear(ring gear) C and carrier D that connects planet gears as seen in the image below. It has a very…
11 Jun 2020 11:28 AM IST
MULTI-BODY DYNAMICS SIMULATION OF INTERNAL GENEVA MECHANISM IN SOLIDWORKS
AIM: MULTI-BODY DYNAMICS SIMULATION OF INTERNAL GENEVA MECHANISM IN SOLIDWORKS. THEORY: Geneva mechanism is the mechanism through which continuous rotation motion is converted into intermittent rotation motion. In this, the driver wheel rotates continuously and the driven wheel has rotation in regular intervals not continuously.…
02 Jun 2020 09:49 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.