Project

Curve Fitting Using MATLAB

AIM: WAP to fit a linear and cubic polynomial for the Cp data using MATLAB.

OBJECTIVE:

  • To WAP to fit a linear and cubic polnomial for Cp data.
  • To plot the linear and cubic fit curves along the raw data points.

CURVE FITTING: By curve fitting, we can mathematically construct the functional relationship between the observed dataset and parameter values. It is a statistical method to derive coeffecient values for equations that express the value of one varaible as a function of another.

FITTING A CURVE:

The curve fitting is done by assuming the order of the polynomial equation and then finding the coeffecients of the polynomial equation.

For linear fit the equation is `y=a*x+b` and the coeffecients of a and b are found and curve is fitted in two points.

For cubic fit the equation is `y=a*x^3+b*x^2+c*x+d` and the coeffecients of a, b, c and d are found and curve is fitted in four points.

PARAMETERS USED TO MEASURE THE FITNESS OF THE CURVE:

This can be done by four quantities which help us to measure the goodness of fit criteria.

1. SSE: Sum of squares due to error, is the sum of the squares of results or deviations predicted from actual values of data. It measures the discrepancy between the data and an estimation model. Smaller value means tight fit.

2. R square: It is a statistical measure of how close the data are to the fitted regression line. Its value is always between 0 and 1. One being a good fit.

`R^2=(SSR)/(SST)`

SSR is sum of squares of regression.

3. Adjusted R square: It is similar to R square method but it neglects the insignificant independent variables.

`R^2=1-[((1-R^2)*(n-1))/(n-k-1)]`

n is the no. of data points

k is the no. of independent variables

Adjusted R increases if the data is significant.

Adjusted R decreases if the data is insignificant.

4. Root mean squared error: Its is an estimate of standard deviation of the random component in the data, and is defined as

RMSE=sqrt((SSE)/n)`

MATLAB CODE FOR LINEAR FIT

%WAP for linear fit

clc
close all
clear all

load('data') %This command is used to load data from 'data' file
temperature=data(:,1);%temperature array is created
cp=data(:,2);%specific heat array is created

plot(temperature,cp) %plot of given data is created
xlabel("temperature") %used to give labels for x axis and y axis
ylabel('Cp')

%curve fitting
coeff=polyfit(temperature,cp,1); %using the polyfit command the coeffecients of linear fit are found
predicted_cp=polyval(coeff,temperature);% coeffecients found are used to predict Cp values 
hold on
plot(temperature,predicted_cp) %predicted values are plotted
legend('original','predicted') %scale is provided for identification of the plots
title('Cp [KJ/Kmol-K] vs Temperature [K]') %title for the plot is given

sum=0; %initialization
for i=1:3200 %this loop is used to calculate the mean of Cp by calculating the summation of Cp's given and then dividing it by 3200
    sum=sum+data(i,2);
end
mean=sum/3200; %mean of the Cp's is calculated

ssr=0;%initialization
%R_square=SSR/SST SSR is summation(f(x(i))-mean)^2
for j=1:3200 %for calculating ssr
    ssr=ssr+(predicted_cp(i)-mean)^2;
end

%SSE summation(error)^2    error=[Y(i)-f(x(i))]
sse=0;%initialization
for k=1:3200 %for calculating SSE
    sse=sse+(data(k,2)-predicted_cp(k))^2;
end
sst=ssr+sse;
R_square=ssr/sst %indicator of wheteher the fit is good. Generally it should be greater that 0.95

MATLAB CODE FOR CUBIC FIT

%WAP for cubic polynomial fit

clc
close all
clear all

load('data') %This command is used to load data from 'data' file
temperature=data(:,1);%temperature array is created
cp=data(:,2);%specific heat array is created

plot(temperature,cp) %plot of given data is created
xlabel("temperature") %used to give labels for x axis and y axis
ylabel('Cp')

%curve fitting
coeff=polyfit(temperature,cp,3); %using the polyfit command the coeffecients of cubic fit are found
predicted_cp=polyval(coeff,temperature);% coeffecients found are used to predict Cp values 
hold on
plot(temperature,predicted_cp) %predicted values are plotted
legend('original','predicted') %scale is provided for identification of the plots
title('Cp [KJ/Kmol-K] vs Temperature [K]') %title for the plot is given

sum=0; %initialization
for i=1:3200 %this loop is used to calculate the mean of Cp by calculating the summation of Cp's given and then dividing it by 3200
    sum=sum+data(i,2);
end
mean=sum/3200; %mean of the Cp's is calculated

ssr=0;%initialization
%R_square=SSR/SST SSR is summation(f(x(i))-mean)^2
for j=1:3200 %for calculating ssr
    ssr=ssr+(predicted_cp(i)-mean)^2;
end

%SSE summation(error)^2    error=[Y(i)-f(x(i))]
sse=0;%initialization
for k=1:3200 %for calculating SSE
    sse=sse+(data(k,2)-predicted_cp(k))^2;
end
sst=ssr+sse;
R_square=ssr/sst %indicator of wheteher the fit is good. Generally it should be greater that 0.95

RESULTS: Higher the value of R square fit better the fit. As the R square value for cubic fit is greater than linear fit, cubic fit has better fittness characteristics. Also, from the graphs we can see that, cubic fit has better resemblance with the original curve.

CURVE FITTING TOOLBOX:

Curve Fitting Toolbox is an inbuilt feature of MATLAB which reduces our work. In this tool we just have to enter the data and select the X and Y input data. This will plot the original curve. Then we have to select the type of curve and degree of the polynomial which we would like to use for curve fitting for.

After providing the requisite data the original curve and the predicted curve are plotted. Also, the values of R square, SSE, Adjusted R square and RMSE are displayed.

Curve Fitting Toolbox for Linear Fit

Curve Fitting Toolbox for Cubic Fit

HOW TO IMPROVE FIT?

This can be done using centering and scaling the points of the curve fitting equation to get the better results. Centering a variable is subtracting the mean of the variable from each data point so that new variable's mean is zero.

Scaling means multiplying each data point by a constant in order to alter the range of the data.

DIFFERENT FITS AND HOW TO ACHIEVE THEM?

Best Fit is the fit in which there is a diffrence between the actual curve and predicted curve but, it is the lowest possible. Like R square is 0.997.

You can get the best fit by increasing the degree of the polynomial and bringing the value of R as close to 1 as possible.

Perfect Fit is the fit in which the actual curve and the predicted curve meet at all the points and R square value is 1.

We can get the Perfect Fit by increasing the degree of the polynomial in curve fitting tool box but, calculation gets complex.

As you can see in the left hand side of the above image R square is 1. It is obtained at a degree of polynomial 8.

CONCLUSIONS:

1. For good fit, higher degree of polynomial is to be selected.

2. There are several ways in which we can do curve fitting in MATLAB.

REFERENCES:

1. https://projects.skill-lync.com/projects/Curve-Fitting-and-criterion-to-choose-best-fit-41860

2.https://in.mathworks.com/help/matlab/ref/polyfit.html

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.