Project

Curve Fitting - MATLAB

Write code to fit a linear and cubic polynomial for the Cp data,

Plot the linear and cubic fit curves along with the raw data points. Title and axes labels are a must, legends could be shown if necessary -

Main Code :

clear all
close all
clc

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

% Linear Fit
co_effs_lf = polyfit(temperature,cp,1);
predicted_cp_lf = polyval(co_effs_lf,temperature);

% Cubic Fit
co_effs_cf = polyfit(temperature,cp,3);
predicted_cp_cf = polyval(co_effs_cf,temperature);

% plot
figure(1)
subplot(2,1,1)
plot(temperature, predicted_cp_lf,'linewidth',3,'color','g');
hold on
grid on
plot(temperature, cp ,'linewidth',3,'color','k');
xlabel('Temperature [K]')
ylabel('Specific Heat [KJ/Kmol-K]')
legend('Linear Curve Fit','Original Data')
title('Linear Curve Fit')

subplot(2,1,2)
plot(temperature,predicted_cp_cf,'linewidth',3,'color','b');
hold on
grid on
plot(temperature, cp ,'linewidth',3,'color','k');
xlabel('Temperature [K]')
ylabel('Specific Heat [KJ/Kmol-K]')
legend('Cubic Curve Fit','Original Data')
title('Cubic Curve Fit')

figure(2)
plot(temperature, cp ,'linewidth',3,'color','k');
hold on
plot(temperature, predicted_cp_lf,'linewidth',3,'color','g');
hold on
plot(temperature,predicted_cp_cf,'linewidth',3,'color','b');
grid on
xlabel('Temperature [K]')
ylabel('Specific Heat [KJ/Kmol-K]')
legend('Original Data','Linear Curve Fit','Cubic Curve Fit')
title('Comparision of all')

Firstly data file sould be loaded, note that the data file is in the same registry as the program file. Using the load syntax data can be extracted from a particular file. As the data file consists fo two seperate columns of temperature and specific heat, assign the respective columns to respective parameters.

Using the 'polyfit' syntax the coefficients of the polynomial function is achieved. Using these coefficients we attain the new cp values using 'polyval' syntax.

This pattern is followed for any polynomial function, here we attain the values of cp for linear and cubic functions.

Plots are plotted between original data and respective curve fit, Comparision among all the curves are plotted in other figure. Axes labels, titles & legends are assigned to respective graphs.

Errors :

No errors faced.

Output :

Explain the parameters used to measure the fitness characteristics for both the curves -

There are 4 quantities which help us measure the goodness of fit criteria or how well the equation is representing the datapoints upon finding a curve which satisfies the datapoints :

  • The sum of squares due to error (SSE)
  • R-Square
  • Adjusted R- Square
  • Root Mean Square Error (RMSE)

1. The sum of squares due to error [1] :

2. R-Square [1] :

3. Adjusted R-Square [1] :

4. Root Mean Square Error (RMSE) [1] :

How to make a curve fit perfectly -

The perfect fit is achieved when the geometric distances from the given points to the fitting curve are zero and curve achieved should be error free, i.e, the curve fits perfectly on the points. Perfect fit can be achieved by increasing to higher order of polynomials, this method increases the precession, accuaracy and reduces errors.

How to get the best fit -

We could use the curve fit app in the MATLAB inorder to get best fit [2] :

What could be done to improve the cubic fit -

Use splitwise polynomial function, In this method we split the data into various data folds in two or more set of ranges and are specified to different variables and are plotted together.

References :

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

[2] https://in.mathworks.com/help/curvefit/curve-fitting.html

[3] https://ncss-wpengine.netdna-ssl.com/wp-content/themes/ncss/pdf/Procedures/NCSS/Introduction_to_Curve_Fitting.pdf

[4] https://en.wikipedia.org/wiki/Curve_fitting

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.