Project

Curve fitting using Matlab

TO WRITE THE CODES USING MATLAB FOR THE CURVE FITTING OF THE GIVEN DATA

Curve fitting, also called regression analysis, is a process of fitting a function to a set of data points. The function can then be used as a mathematical model of the data. Since there are many types of functions (linear, polynomial, power, exponential, etc.), curve fitting can be a complicated process.

Here we are performing the curve fitting as a linear and cubic equations. As the deree of fthe polynomial equation increases the curve fitting is done that high efficiently.

Curve fitting with polynomials is done in MATLAB with the polyfit function, which uses the least squares method. The basic form of the polyfit function is:

Syntax for the POLYFIT

  1. p= polyfit(x,y,n) returns the coefficients for a polynomial p(x) of degree n that is a best fit (in a least-squares sense) for the data in y. The coefficients in p are in descending powers, and the length of p is n+1
    p(x)=p1xn+p2xn−1+...+pnx+pn+1p(x)=p1xn+p2xn−1+...+pnx+pn+1.

  2. [p,S] = polyfit(x,y,n) also returns a structure S that can be used as an input to polyval to obtain error estimates.

  3. [p,S,mu] = polyfit(x,y,n) also returns mu, which is a two-element vector with centering and scaling values mu(1) is mean(x) and mu(2) is std(x). Using these values, polyfit centers x at zero and scales it to have unit standard deviation.
    ˆx=x−¯xσx.x^=x−x¯σx .

The above are the possible syntaxes used for the polynomial curve fitting.

Syntax for POLYVAL

  1. y= polyval(p,x) evaluates the polynomial p at each point in x. The argument p is a vector of length n+1 whose elements are the coefficients (in descending powers) of an nth-degree polynomial.
  2. y= polyval(p,x,S,mu) use the optional output mu produced by polyfit to center and scale the data. mu(1) is mean(x), and mu(2) is std(x). Using these values, polyval centers x at zero and scales it to have unit standard deviation.

The above are the possible syntaxes used for the polynomial curve fitting.

Ploting the data as per the code will give the fitted curves as per the respective values. It is important to find the goodness of the fit in order to check the accuracy of the curve fitting. Goodness of the fit can be evaluated by the following methods.

-The sum of squares due to error (SSE)

-R-square

-Adjusted R-square

-Root mean squared error (RMSE)

We have found the goodness of the fit using the R-square method.

R-Square method.

This statistically measures how successful the fit is in explaining the variation of the data. R-square is the square of the correlation between the response values and the predicted response values. It is also called the square of the multiple correlation coefficient and the coefficient of multiple determination.

R-square is defined as the ratio of the sum of squares of the regression (SSR) and the total sum of squares (SST). SSR is defined as

SSE=∑|y(i)−f(x(i))|2SSE=∑|y(i)-f(x(i))|2;

SSR=∑|f(x(i))−mean|2SSR=∑|f(x(i))-mean|2 ;

mean=∑y(i)nmean=∑y(i)n; n is total number of data points.

SST is also called the sum of squares about the mean where SST = SSR + SSE. Given these definitions, R-square is expressed as

R2=SSRSST=1−SSESSTR2=SSRSST=1-SSESST

R-square can take on any value between 0 and 1, with a value closer to 1 indicating that a greater proportion of variance.

Following is the code for the linear and cubic curve fitting for the data of CP and temperature.

close all 
clear all
clc

%preparing the data
cp_data=load('data');
temperature=cp_data(:,1);
cp=cp_data(:,2);


%curve fit
% cp= a*T^n+b*T^(n-1)+_ _ _ _ here the degree is n and as the degree increases, the curve
% fit accuracy also increases.
%we use polyfit(x value,y value,degre of the eqn)
%polyfit is used to find the constant values like a,b,_ _ _ using the data of
%the cp and T as per the above equation were temperature T is x value, cp is
%y-value as the eqn y=ax^n+bx^(n-1)+_ _ _ _.

%Using polyfit for the lienar and cubic fit respectively
linear_co_eff=polyfit(temperature,cp,1);
[cubic_co_effs,s,mu]=polyfit(temperature,cp,3);

%using polyval(coefficients array,x values) to find the y values as per the
%above equation respectively for the linear and cubic fits.
cplinear=polyval(linear_co_eff,temperature);
predicted_cpcubic=polyval(cubic_co_effs,temperature,s,mu);

%predicted cp is the values calculated from the eqaution above as the
%inputs are given as coeffs and the temperature arrays respectively.

%plot for linear fit
figure(1)
plot(temperature,cp,'linewidth',3)
hold on
plot(temperature,cplinear,'linewidth',3)
xlabel('temperature[K]')
ylabel('specific heat [KJ/Kmol-K]')
legend('original dataset','linear curve fit')
title('linear curve fitting')

%plot for cubic fit
figure(2)
plot(temperature,cp,'linewidth',3)
hold on
plot(temperature,predicted_cpcubic,'linewidth',3,'color','r')
xlabel('temperature[K]')
ylabel('specific heat [KJ/Kmol-K]')
legend('original dataset','cubic curve fit')
title('cubic curve fitting')

% to find the goodness of the fit

%mean value of the cp from the given data
mean=sum(cp)/length(cp);

%SSE= sum of the squared errors
%SSR=sum of squares of the regression
%SST= total sum of squares
%R^2=R-square can take on any value between 0 and 1

%goodness of the linear fit 
l_error=abs(cp-cplinear);
l_SSE=sum(l_error.^2);
l_diff=abs(cplinear-mean);
l_SSR=sum(l_diff.^2);
l_SST=l_SSR+l_SSE;
l_R2=l_SSR/l_SST

%goodness of the cubic fit
c_error=abs(cp-predicted_cpcubic);
c_SSE=sum(c_error.^2);
c_diff=abs(predicted_cpcubic-mean);
c_SSR=sum(c_diff.^2);
c_SST=c_SSR+c_SSE;
c_R2=c_SSR/c_SST

In the above code, initially the data is loaded into variable inorder to access it by giving the data respectively to temperature and cp. Then the polyfit and polyval commands are used to do the curve fitting procedure and then plotted accordingly. Then the goodness of fit is found using the R-Square method for the linear and cubic curve fitting done.

Results obtained

The above is the workspace window that shows the results so obtained. It is observed that the Goodness of fit for the linear curve fitting (l_R2 from the aboce picture) is about 92.49% and for that of the cubic curve fitting (c_R2 from the above picture) it is 99.67%.

Errors faced

- Curve fitting of linear and cubic not working for the same syntax of the polyfit anf polyval, so it is solved by using the right syntax as in above code

right syntax for the cubic curve fit

[p,s,mu]polyfit(x,y,n);

a=polyval(p,x,s,mu);

So from the above study

-How to make a curve fit perfectly?

By increasing the degree of the polynomial, the goodness of fit efficiency increases there by we get the best fit for the given data which practically correct i.e. R-square value should be very near to 1 to have the best fit.

-How to get the best fit?

Best fit is the curve that passes through almost all the data points. In order to get the best fit we can do it by increasing the order of the polynomial.

- What could be done to improve the cubic fit?

To improve the cubic fit, we have to use polyfit with 3 outputs in such a that, [p,S,mu] = polyfit(x,y,n) also returns mu, which is a two-element vector with centering and scaling values mu(1) is mean(x) and mu(2) is std(x). Using these values, polyfit centers x at zero and scales it to have unit standard deviation.

Conclusion

For the given data, linear and cubic curve fitting is done using the matlab code and also the goodness of the fit is found by the R-Square method and found that the linear curve fitting have a 0.9249 and for cubic curve fitting of 0.9967 value denoting the best fit possible.

References

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.