Project

Curve Fitting

Curve fitting is the process of constructing a curve, or mathematical function, that has the best fit (Closest Proximity) to a series of data points, possibly subject to constraints.

By curve fitting, we can mathematically construct the functional relationship between the observed data set and parameter values, etc. It is highly effective in the mathematical modeling of some natural processes.

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 and axes labels are a must, legends could be shown if necessary.
  • Explain the parameters used to measure the fitness characteristics for both the curves.

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
  • Adjusted R-square
  • Root mean squared error (RMSE)

(Note : MS-Error is nothing but Mean of squared errors(MSE). It is just for the ease of comparision.)

Here, MS_Error, SSE & R-square are used to measure the Fitness of Curve.

error is the difference between cp and pred_cp.

SSE is the sum of squared errors.

MS_Error is the mean of squared errors.

SSR is the sum of squares of the regression, which is basically the sum of square of the difference between the value of the fitting function at a given point(cp) and the arithmetic mean of the data set.

SST is the sum of SSE and SSR.

%MATLAB code for Curve Fitting

clear all
close all
clc

%Loading the data file
cp_data= load('data');

temp= cp_data(:,1);
cp= cp_data(:,2);

%Normalize the X-data (Center and Scale)
temp= normalize(temp);

%Obtaining the desired Linear Fit
co_efficient= polyfit(temp,cp,1);
pred_cp= polyval(co_efficient,temp);

figure(1)
hold on
%Plotting the Original Data Set
plot(temp,cp,'linewidth',3,'color','b')
%Plotting the Predicted Curve
plot(temp,pred_cp,'linewidth',3,'color','r')
xlabel('Temperature')
ylabel('Specific Heat')
title('Linear Polynomial Curve Fitting')
legend('Original Dataset','Curve Fit')

error= cp-pred_cp;
MS_Error = mean(error.^2);
SSE= sum(error.^2);
SSR= sum((cp-mean(cp)).^2);
SST = SSR + SSE;
R1_square= SSR/SST;
Table_For_Linear_Fit= table(MS_Error,SSE,SSR,SST,R1_square,'VariableNames',{'MS_Error','SSE','SSR','SST','R1_square'})
if R1_square>0.95
    disp('Fit is Good.')
else
    disp('Fit is not Good.')
end    

%Obtaining the desired Cubic Curve Fit
co_efficient= polyfit(temp,cp,3);
pred_cp= polyval(co_efficient,temp);

figure(2)
hold on
%Plotting the Original Data Set
plot(temp,cp,'linewidth',3,'color','b')
%Plotting the Predicted Curve
plot(temp,pred_cp,'linewidth',3,'color','r')
xlabel('Temperature')
ylabel('Specific Heat')
title('Cubic Polynomial Curve Fitting')
legend('Original Dataset','Curve Fit')

error= cp - pred_cp;
MS_Error= mean(error.^2);
SSE=sum(error.^2);
SSR=sum((cp-mean(cp)).^2);
SST = SSR + SSE;
R2_square= SSR/SST;
Table_For_Cubic_Curve_Fit= table(MS_Error,SSE,SSR,SST,R2_square,'VariableNames',{'MS_Error','SSE','SSR','SST','R2_square'})
if R2_square>0.95
    disp('Fit is Good.')
else
    disp('Fit is not Good.')
end    

#Graphical Fit Result

Result :-1 Linear Polynomial Curve Fitting

Result :-2 Cubic Polynomial Curve Fitting

Just by visualization, we can see that the cubic polynomial is a better approximation than the linear one. This is because the cubic curve is a higher degree polynomial than linear. Mathematically, we need to check the goodness of fit for both.

Result :-3 Numerical Fit Result

Typically, when R-square is greater than 0.95 the fit is assumed to be good. And Mean of squared errors(MS_Error) must be very small and/or closest to zero.

In this way, we can get Best Fit.

For Perfect Fit, we need to increase the Degree of Polynomial but it won't be good in all cases.The best way to get perfect fit is to divide the data into small regions and then Curve fit each region seperately.

To improve a Cubic Fit, we need to divide the data into small regions and then curve fit each region seperately. And, dividing a data into smaller & smaller region will definitely tend to Zero Error(Closest).

Errors that i'd faced :-

(1) Generally, all the downloaded file store in the "download" folder. So,if you run the program without relocating the file named "data" into MATLAB folder then definitely you will get this error.

(2) If you get this Warning, you can click on the "Center and Scale" at Curve Fitting Tool box. or you can normalize X-data of your data file. In this case, temp is X-data.

Syntax :

temp= normalize(temp);

(Note: It must be after allocating a values in "temp".)

REFERENCE :-

(1) https://en.wikipedia.org/wiki/Curve_fitting

(2) https://www.ctr.unican.es/asignaturas/instrumentacion_5_it/curvefit.pdf

(3) Mathworks Support

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.