MATLAB - Curve Fitting
Curve Fitting using MATLAB
Objective
To write a script on MATLAB to perform linear and cubic curve fit for the given data series of variance of Cp with temperature to compute the errors in predicted values and to see the trendline for each outcome.
Procedure
- Load command is used to copy the data file to MATLAB directory.
- Data values for each column of temperature and cp is assigned to necessary variables.
- The original data set is plotted and the nitial trendline is observed.
- The inbuilt functions for data optimisation polyfit and polyval are utilised to define data sets for the given functon.
- Plot function is used to plot the original curve and the fitted curve.
Script for Linear function
clear all
close all
clc
%preparing the data
cp_data = load('data');
temperature = cp_data(:,1);
cp = cp_data(:,2);
%curve fit
%cp = a*T^2 + b*T + c
co_effs = polyfit(temperature,cp,1);
predicted_cp = polyval(co_effs, temperature);
%Compare curve fit with original data
plot(temperature,cp,'linewidth', 3)
hold on
plot(temperature,predicted_cp,'linewidth',3, 'color', 'r')
xlabel('Temperature [k] ')
ylabel('Specefic Heat[KJ/Kmol-K]')
legend('Original dataset', 'Curve fit')
Plot Original dataset Vs Curve fit
Script for cubic polynomial function
clear all
close all
clc
%preparing the data
cp_data = load('data');
temperature = cp_data(:,1);
cp = cp_data(:,2);
%cureve fit
%cp = a*T^2 + b*T + c
co_effs = polyfit(temperature,cp,3);
predicted_cp = polyval(co_effs, temperature);
%Compare curve fit with original data
plot(temperature,cp,'linewidth', 3)
hold on
plot(temperature,predicted_cp,'linewidth',3, 'color', 'r')
xlabel('Temperature [k] ')
ylabel('Specefic Heat[KJ/Kmol-K]')
legend('Original dataset', 'Curve fit')
Plot Original dataset Vs Curve fit
Conclusion
From the above plots we can see that for the cubic polynomial the curve fit module has done a near to perfect fit while for the linear function the predictions have deviated from the expected linear behavior which does not seem to follow the supposed trendline accurately. If the curve fitting ws done by seperating the values of temperature then a more accurate curve fit can be obtained.