Project

CURVE FITTING USING MATLAB

AIM: CURVE FITTING USING MATLAB

OBJECTIVE:

  1. Linear and cubic polynomial curve fitting to a set of data points.
  2. Plotting predicted curves against raw data points.
  3. Studding parameters that measure the goodness of a 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. 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:

  • Interpolation, where you discover a function that is an exact fit to the data points. Since this assumes no measurement error, it has limited applicability to real-life scenarios.
  • Smoothing when we find a function that is an approximate fit to the data points, but we give room for error and we allow our actual points to be near, but not necessarily on the line; given the error is minimized overall.

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:

  1. Load data points and assign variables to data points.
  2. Using polyfit command get coefficients of linear and cubic fit and using polyval command store values of predicted linear and cubic polynomials.
  3. Plot predicted values of linear and cubic polynomials along with raw data points to compare the best fit.
  4. Define formulas of SSE, R-square, RMSE to get the values of parameters which measure the goodness of fit.
  5. Compare the goodness of fit of linear and cubic curve fitting

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:

  1. SUM OF SQUARE OF ERRORS(SSE)

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.

  1. R-square

R-square is the square of the correlation between the response values and the predicted values.

  1. Adjusted R-square

Adjusted R-square is a normalizing variation of R-square which neglects the variation of R-square due to insignificant independent variable.

  1. Root mean square error(RMSE)

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

Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.