Project

Curve fitting in MATLAB

1. AIM

  • To perform a curve fitting on a given data set using MATLAB software.
  • Write MATLAB code to fit a linear and cubic polynomial for the Cp data.
  • Plotting the data and checking error using different methods.

2. REQUIRED EQUATIONS

For quadratic equation `Cp​=aT^2+bT+c` and so on for other degrees of equations.

To Read more about these formulas for RSME,`R^2`click here.

3. FLOW

  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.

4. MATLAB CODE

clear all
close all
clc

%preparing the data by loading it
cp_data = load('data');
temp = cp_data(:,1);     %assigning first column as temperatures
cp = cp_data(:,2);       %assigning second column as Cp values 

%Curve fitting
coeff = polyfit(temp,cp,1);                  %obtaining values of coefficients of the equation of some degree
predicted_cp = polyval(coeff,temp);          %putting coefficients in the original equation to get L.H.S

%comparing both the curves
plot(temp,cp,'linewidth',2,'color','g')                        %plotting original dataset
hold on
grid on
plot(temp,predicted_cp,'linewidth',2,'color','mageta')  %plotting my graph with cp which is outcome of curve fitting
xlabel('Temperature (K)')
ylabel('Specific Heat (KJ/Kmol-K)')
legend('Original Curve','Expected curve')


%checking error by R-square method
%finding the SSR
ssr=0;
sse=0;
meancp = mean(cp);                  %calculating mean of cp dataset
for i=1:length(cp)
b = (predicted_cp(i)-meancp)^2;     %looping so as to generate values of b 
ssr=ssr+b;                          %adding all the values of b
end
for j=1:length(cp)
c = (cp(j)-predicted_cp(j))^2;      %looping so as to generate value of c 
sse = sse+c;                        %adding all values of c
end
sst = ssr+sse;
R = ssr/sst                         %final value of R-square

%finding RMSE
rmse = (sse/length(cp))^0.5         %final value of RMSE

5. EXPLAINATION

  • Loaded the data given in the challenge itlself, by using load() function.
  • Then we have assigned the Cp and temperature values to variables using (:,n) function.
  • 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.
  • Then using polyval function, obtain the coefficients a, b and c.
  • 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.
  • 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.
  • 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.
  • The RMSE value in the next method should be as less as possible to be called as a good fit.

6. ERRORS

The error was basically a spelling mistake of the color mangenta typed as mageta

7.RESULTS

For Linear Polynomial - Curve isn't a good fit because of high error

For Cubic Polynomial - The curve is better fit than Linear polynomial because of lower R and rmse values.

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

Although rmse is 0.51, it can be lowered if we go to 9 or more order polynomials.

8. CONCLUSION

  • 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?

By increasing the input data size the cubic fit can be improved

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.