Project

Linear and Cubic Polynomial Curve fitting for Temperature-Cp Data

Introduction

Curve fitting is the process of constructing a curve, or mathematical function, that has the best fit to a series of data points, possibly subject to constraints. Curve fitting can involve either interpolation, where an exact fit to the data is required, or smoothing, in which a "smooth" function is constructed that approximately fits the data.

Aim

To plot the linear and cubic fit curves along with the raw data points and to measure the fitness characteristics for both the curves.

Program Description

Firstly the data files which contains the temperature and Cp values is read and the values are input into a matrix. Then the temperature and Cp values are seperately fed into two different arrays. The polyfit function along with the polyval function is used to produce the linear and Cubic Curvefits. The Root Mean Square Error is found using respective equations and is displayed.

Main Program

%Start Of Program
close all
clear all
clc
%Reading Data File
data=load('data');
%Assigning Temperature values to a seperate variable
temp=data(:,1);
%Assigning Cp values to a seperate variable
cp=data(:,2);
%Getting Coefficients of the equation using polyfit function
cf=polyfit(temp,cp,1);
cf_2=polyfit(temp,cp,3);
%Getting value
pre_cp=polyval(cf,temp);
pre_cp1=polyval(cf_2,temp);
%Plotting Graph
figure(1);
plot(temp,cp,'color','b','linewidth',2);
hold on
plot(temp,pre_cp,'color','r','linewidth',1);
hold on
plot(temp,pre_cp1,'color','k','linewidth',1);
xlabel('Temperature (K)');
ylabel('Specific Heat (kJ/kmol-K)');
legend('Original Dataset','Curve Fit Linear','Curve Fit Cubic');
grid on
%Plotting Percentage difference Curve

rmse_linear= sqrt(mean((cp-pre_cp).^2))
rmse_cubic = sqrt(mean((cp-pre_cp1).^2))

%End Of Program

Result from Main Program:

RMSE Values:

Errors Faced

While plotting the 3rd degree polynomial curve fit, the above warning was shown. It was fixed using the Curve Fitting Application mentioned below. Robust was set to Bisquare for better results.

Using Curve Fitting Application

The curve fitting application is used to produce a more accurate curve fit for the data set input.

Goodness of fit:

Code generated using Curve Fitting

function [fitresult, gof] = createFit(temp, cp)
%CREATEFIT(TEMP,CP)
%  Create a fit.
%
%  Data for 'Curve_Fit_Cubic' fit:
%      X Input : temp
%      Y Output: cp
%  Output:
%      fitresult : a fit object representing the fit.
%      gof : structure with goodness-of fit info.
%
%  See also FIT, CFIT, SFIT.

%  Auto-generated by MATLAB on 12-Feb-2020 21:18:34


%% Fit: 'Curve_Fit_Cubic'.
[xData, yData] = prepareCurveData( temp, cp );

% Set up fittype and options.
ft = fittype( 'poly3' );
opts = fitoptions( 'Method', 'LinearLeastSquares' );
opts.Normalize = 'on';
opts.Robust = 'Bisquare';

% Fit model to data.
[fitresult, gof] = fit( xData, yData, ft, opts );

% Plot fit with data.
figure( 'Name', 'Curve_Fit_Cubic' );
h = plot( fitresult, xData, yData );
legend( h, 'cp vs. temp', 'Curve_Fit_Cubic', 'Location', 'NorthEast', 'Interpreter', 'none' );
% Label axes
xlabel( 'temp', 'Interpreter', 'none' );
ylabel( 'Cp', 'Interpreter', 'none' );
grid on


Result from Curve Fitting Application:

References:

https://in.mathworks.com/matlabcentral/answers/386493-rmse-between-two-variables

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.