Project

Writing Code for curve fitting.

OBJECT :

Write code for Curve Fitting.

Curve fitiing is the process for constructing a curve which possess the real proximity to the series of data. By curve fitting, we can mathematically construct the functional relationship between the observed dataset and parameter value.

In this project we can do a curve fitting for specific heat at constant pressure along y axis and temperature along x axis.

In the file name "data" have vaious values for Cp at respective temperature which we call in the coding to get the observed data and then use the observed data to get the predicted Cp while using the polynomial of degree 2 or it may be 3.

After getting the predicted Cp we again plot a curve and analyse that the curve fitted are not exactly match with each other.

Now the role of errors came into picture wich we study and analysing values for those errors due to which curve getted different.

There are some errors name as :

  • The some of squares due to errors(SSE).
  • R-square.
  • Adjusted R-square.
  • Root mean square error.

Now, first we go though the coding to peform curve fitting between observed Cp and Predicted_Cp

In the given datafile temperature is in column 1 and Cp in column 2.

We use the polynomial of order 2

i.e.

`Ax^2 +Bx +C = 0`

To get the value for coefficint A and B for polynomial of order 2

we use,

co_eff = polyfit(temperature,cp,2);

To get the predictd_Cp

predicted_cp = polyval(co_eff,temperature);

Now, Plot a curve betweeen Cp and Predicted_Cp.

To differentiate between curve fitting we use "green" color to indicate for Predicted_Cp.

clear all
close all
clc

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

co_eff = polyfit(temperature,cp,2);

predicted_cp = polyval(co_eff,temperature);
plot(temperature,cp,'linewidth',3);
hold on
plot(temperature,predicted_cp,'linewidth',5,'color','g');

xlabel('temperature')
ylabel('cp')

we get,

In the plot we observed that the two curve can not follow exacly the same path. To get the more accurate curve plot we increase the degree of the polynomial to 3.

co_eff = polyfit(temperature,cp,3);

Now we get,

This is the more accurate curve plotting than the previous one we perform.

But still there are many points in the curve where the curve fitting get differ with one another. The reson for this is only in the value we plotted for Cp and predicted_Cp.

Now here the erors came into play that show how much is the variation and give the quality of the curve.

Now we find some errors:

This error show the differences in the actual Cp and Predited_Cp values in plotting.

error = cp(:,1)-predicted_cp(:,1);

Here there is no confusion that what we subtract i.e Cp or Predicted_Cp because at last we have to make it square so its negative effects get cleared.

1. The some of squares due to errors(SSE).

SSE = sum(error.^2)

2. R-square.

The R_square is given by the equation:

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

SSR =sum((cp(:,1)-mean(cp(:,1))).^2)
SST = SSR + SSE
R_square = SSR/SST

3. Root mean square error.

RMSE = sqrt(SSE./length (cp))

Now,

To know the characteristic of the curve i.e. it is good or not we compare it with the value of R_square

if R_square >0.95
    display('fit is good')
else
    display('fit is not good')
end

NOW,

Complete code for the program is :

clear all
close all
clc

cp_data = load('data'); % TO LOAD DATA FILE
temperature = cp_data(:,1);
cp = cp_data(:,2);

co_eff = polyfit(temperature,cp,3); % TO GET COEFFICIENT

predicted_cp = polyval(co_eff,temperature);
plot(temperature,cp,'linewidth',3);   % CURVE FOR GIVEN VALUES
hold on
plot(temperature,predicted_cp,'linewidth',5,'color','g'); % CURVE FOR PREDICTED VALUES

xlabel('temperature')
ylabel('cp')

error = cp(:,1)-predicted_cp(:,1);  % VARIOUS TYPES OF ERRORS CALCULATED
SSE = sum(error.^2)

SSR =sum((cp(:,1)-mean(cp(:,1))).^2)
SST = SSR + SSE
R_square = SSR/SST

RMSE = sqrt(SSE./length (cp))

if R_square >0.95        %  GET QUALITY OF CURVE
    display('fit is good')
else
    display('fit is not good')
end

RESULT :

SSE =

   9.4272e+04


SSR =

   2.8794e+07


SST =

   2.8888e+07


R_square =

    0.9967


RMSE =

    5.4277

fit is good
>> curve_fit
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.