Project

Curve fitting using MATLAB

AIM

To plot an actual curve and the predicted curve of the variation between temperature and specific heat

ASSUMPTIONS AND REQUIRED EQUATIONS

  1. The temperatures are in Kelvin (K)
  2. The specific heat values are in (KJ/Kg-mol
  3. For quadratic equationCp​=aT2+bT+c">`Cp​=aT^2+bT+c`and so on for other degrees of equations.
  4. To Read more about these formulas for RSME,R2">`R^2`click here.

THEORY

The variation of the temperature with the specific heat can be represented by a polynomial having a degree 'n'. The values of specific heat at various temperature values are represented by the plots. In actual practice, while solving engineering problems, we assume that the value of Cp is constant. However, that is not the case, in actual.

OUTLINE

  1. Load data or the inputs to plot the curve.
  2. Differentiate the data and assign some variables.
  3. Use MATLAB to fit a polynomial degree curve through those variables
  4. obtain co-efficients of the variable T.
  5. Plot the original dataset as well as curve that is being fitted through those datasets.
  6. Find error so as to get an approximation of how good your curve fits the data sets.

CODE

clc
clear all
close all

%Importing datafile
datafile = load('data');
temp = datafile(:,1);
cp_val = datafile(:,2);



linear_coeff_predict = polyfit(temp,cp_val,1)
predicted_cp1 = polyval(linear_coeff_predict,temp);
cubic_coeff_predict = polyfit(temp,cp_val,3)
predicted_cp2 = polyval(cubic_coeff_predict,temp);


%Plotting predicted linear curve and actual curve
%cp_linear = a*temp+b
plot(temp,cp_val,'linewidth',2.5,'color','b')
hold on
plot(temp,predicted_cp1,'linewidth',2.5,'color','r')
hold off
xlabel('Temperature')
ylabel('Specific Heat')
legend('Original curve','Predicted curve')
title('Comparison of actual curve with predicted linear curve') 

%Plotting predicted cubic curve and actual curve
%cp_cubic = a*temp^3+b*temp^2+c*temp+d
figure(2)
plot(temp,cp_val,'linewidth',2.5,'color','b')
hold on
plot(temp,predicted_cp2,'linewidth',2.5,'color','r')
xlabel('Temperature')
ylabel('Specific Heat')
legend('Original curve','Predicted curve')
title('Comparison of actual curve with predicted cubic curve')

%finding the best fit for linear curve
M = mean(cp_val);
for i=1:length(cp_val)
    SSE_linear = sum((cp_val(i)-predicted_cp1(i)))^2;
    SSR_linear = (predicted_cp1(i)-M)^2;
    
end

SST_linear = SSE_linear+SSR_linear
R_square_linear=SSR_linear/SST_linear

%finding the best fit for cubic curve
for i=1:length(cp_val)
    SSE_cubic = sum((cp_val(i)-predicted_cp1(i)))^2;
    SSR_cubic = (predicted_cp1(i)-M)^2;
    
end
SST_cubic = SSE_cubic+SSR_cubic
R_square_cubic=SSR_cubic/SST_cubic

cftool

EXPLANATION

  1. Loaded the data given in the challenge itlself, by using load() function.
  2. Then we have assigned the Cp and temperature values to variables using (:,n) function.
  3. Then use polyfit function whose inputs will be temperature and CP and also specify what degree polynomial you will be using. here I have used 1 as the degree.
  4. Then using polyval function, obtain the coefficients a, b and c.
  5. Using plot function, plot the graph of Cp of original dataset and Predicted Cp got from puting the values obtained from the polyval into the polynomial. Compare the fit of the curve visually.
  6. The later part of the code is used for checking the maximum error of the fitting of the curve. 2 methods are used here, namely 1. R-square method and 2. Root mean square method.
  7. The value of R in R-square method should lie between 0 and 1 where 0 means poor fit and 1 means Perfect Fit. Typically a value greater than 0.95 means a good fit.
  8. The RMSE value in the next method should be as less as possible to be called as a good fit.

ERROR

  • The error faced was due to selection of wrong directory during coding.
  • The error was retified after changing the directory.

FINAL CURVES

FOR LINEAR CURVE

R_square_linear = 0.9249

FOR CUBIC CURVE

R_square_cubic = 0.9592

For 8th order Polynomial -Perfect fit because R=1.

QUESTIONS:

  1. How to make curve fit perfectly?
    • From the above analysis as the degree of polynomial increases the curve fit becomes more and more smooth and matches perfectly with the original data set. The curve is thern generated by connecting all the points. Also, for getting the perfect fit, we have to take into consideration the errors obtained.
    • Thus, by increasing the polynomial degree up to 8, the polynomial curve starts fitting with the original data set.
  1. How to get a best fit?
    • Using the goodness of fit we were able to figure out the best fit.
    • Using this we get the values of R square.
  • We know that Typically if the R-square is greater than 0.95 the fit is assumed to be good.
  1. What to be done to improve the cubic fit?
  • The cubic fit can be improved by increasing the number of steps or iterations by splitting the existing data. The polyfit and polyval command can be used n number of times depending the number of times the sub-division of the data takes place.

CONCLUDING REMARKS

  • A best fit is a fit without taking errors into consideration.
  • Perfect fit is the one where errors are also taken into account.
  • How to improve the cubic fit?

To improve any process, reducing the size or in a simpler language, reducing the extent is must.So to improve cubic fit, we can reduce the temperature range and make smaller segments so that it will be easy to fit the curve into smaller segments.

9. REFERENCES

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

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.