Challenge

Perform Curve Fitting using MATLAB

AIM:

To perform curve fitting on the given data set using MATLAB.

GOVERNING EQUATIONS:

  1. For quadratic equations,`c_p = aT^2 + bT + c` and so on for other degrees of equations.
  2. For R-square method,`R^2 = (SSR)/(SST)``sum(f(x(i))-mean)^2 and SST = SSR + sum(Y(i)-f(x(i)))^2`
  3. For RMSE method, `RMSE = sqrt((SSE)/n)`

Where a, b, c are coefficients of some degree T

f(x(i)) is the coordinate of the fitted curve having equation y = f(x)

Y(i) is the coordinate of the data set (`c_p` in this case)

OBJECTIVES OF THE PROJECT:

  1. Write MATLAB code to fit a linear and cubic polynomial for the data file.
  2. Plot the data and check errors using different methods.

FLOW OF THE PROJECT:

  • Collect sample data of which you want to get a curve.
  • Differentiate the data and assign some variables.
  • Use MATLAB to fit a polynomial degree curve through those variables and thus, obtain coefficients of the variable T.
  • Plot the original data set as well as the curve that is being fitted through those data sets.
  • Find error values so as to get an approximation of how well your curve fits the data sets

MATLAB CODE:

clear all
close all
clc

% Preparing the data
cp_data = load('data');
temperature = cp_data(:,1); %assigning all rows of first column as temperatures
cp = cp_data(:,2); %assigning all rows of second column as cp vslues

% Curve Fitting
% cp = a*T^3 + b*T^2 + c*T + d
coefficients = polyfit(temperature,cp,2);
predicted_cp = polyval(coefficients,temperature);

% Comparing curve fit with original data
plot (temperature, cp, 'linewidth', 3) %Plotting original dataset
hold on
grid on
plot(temperature, predicted_cp, 'linewidth',3, 'color', 'r') %Plotting graph with cp as a result of curve fitting
xlabel('Temperature[K]')
ylabel('Specific Heat[KJ/mol-K]')
legend('Original dataset', 'Curve fit')

% Checking error by R-square method
% Finding the values for SSR
ssr=0;
sse=0;
mean_cp = mean(cp);  %Calculating mean of cp using mean function
for i = 1:length(cp)
    b = (predicted_cp(i)-mean_cp)^2; %looping to generate vales of b for every value of cp in dataset
    ssr=ssr+b;                          %adding all the values of b
end
for j=1:length(cp)
c = (cp(j)-predicted_cp(j))^2;      %looping to generate value of c for every value of cp and predicted cp
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

Code Explanation :

  1. We start off by loading the data file into the code using the load() function.
  2. Then we assigned the `c_p` and temperature values to variables using (:,n) function because the data set is a CSV.
  3. Then we used the polyfit function which requires temperature, cp and degree of polynomial as inputs. I have taken a second degree polynomial.
  4. Next, we used the polyval function to obtain the coefficients a, b and c.
  5. Now using plot function, we plotted the graph of cp of original dataset and predicted cp got from putting the values obtained from the polyval into the polynomial. Compare the fit of the curve visually.
  6. Next we checked the maximum error of the fitting curve using two methods, a.) R-square method and b.)RMSE method.
  7. The value of R-sqaure and R-square method should lie between 0 and 1 where 0 indicates poor fit and 1 indicates perfect fit. Technically, a value greater than 0.95 is a good fit.
  8. The RMSE value in the next step should be as minimum as possible for it to be considered a good fit.

ERRORS FACED:

1.

Called the predicted_cp function incorrectly during the entire code i.e, while plotting and in the for loops. Corrected the typo.

OBSERVATIONS:

  1. Linear polynomial curve is not a good fit due to high value of R and rmse (error)
  1. Quadratic polynomial curve is a good fit due to low value of R
  2. Cubic polynomial curve is a better fit than quadratic polynomial because of lower R and rmse values.
  1. 8th order polynomial curve is a perfect fit because R = 1 and rmse = 0.51 which can be lowered if we go to 9th or more order polynomials.

CONCLUSION:

  1. Required plots are obtained for given values of temperatures and cp.
  2. Errors are checked and shown as above.
  3. A curve can be made to fit perfectly by taking errors into consideration.
  4. The best fit is a fit in which no errors are taken into consideration.
  5. 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.
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.