Curve Fitting using Matlab
Objective:
1Write code to fit a linear and cubic polynomial for the Cp data.
2 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.
3. Explain the parameters used to measure the fitness characteristics for both the curves.
Procedure:
1.Load the data using load command and allocate temperature and specific heat to two different variables (temp and Cp) which are first and second column in the data file.
2 Use polyfit function of Matlab which returns the coefficients for a polynomial p(x) of degree n that is a best fit (in a least-squares sense) for the data in y. The coefficients in p are in descending powers, and the length of p is n+1
3. Further we can get the cofficients value of the interpolation curve using polyval function in matlab which would be helpful in obtaining the error.
4. Firstly i tried to fit in linear polynomial on the Specific heat and Temperature data and in this case the least squared error was quite high.
5. To reduce the error i subsquently tried with Quadratic and Cubic interpolation curves and observed that the error in case of cubic interpolation was in the acceptable range and it provided a good approximation of the relationship between Specific heat and temperature.
6. Just to understand how increasing the Order of polynomial effects the error value i tried with Fourth order polynomial and in this case the error was even lesser than cubic interpolation.
7. Furthermore i divided the data into 6 divisions to understand how it effects the error and fitted in linear polynomial curve . On doing this i observed that if we divide the data into smaller divisions then the error value for each polynomial fit reduces significantly. (figure5)
Matlab Code
%Code to perform curve fitting for the cp vs temp data file
clear all
close all
clc
%Loading Data from a saved file
cp_data = load('data');
temp = cp_data(:,1); %Allocating data to temp which is the first column of the data
cp = cp_data(:,2); %Allocating data to cp second column
%cp = a*T+b Linear Polynomial
co_eff1 = polyfit(temp,cp,1);
predicted_cp1 = polyval(co_eff1,temp);
%cp = a*T^2 + b*T + c Quadratic Polynomial
co_eff2 = polyfit(temp,cp,2);
predicted_cp2 = polyval(co_eff2,temp);
%cp = a*T^3 + b*T^2 + c*T + d Cubic Polynomial
co_eff3 = polyfit(temp,cp,3);
predicted_cp3 = polyval(co_eff3,temp);
%cp = a*T^4 + b*T^3 + c*T*2 + d*T+e Polynomial of 4th order
co_eff4 = polyfit(temp,cp,4);
predicted_cp4 = polyval(co_eff4,temp);
%Error in data
Error_linear = cp - predicted_cp1;
Error_quadratic = cp - predicted_cp2;
Error_cubic = cp - predicted_cp3;
Error_fourth =cp - predicted_cp4
%Percentage Error
per_errorlin =abs(Error_linear./cp)*100;
per_errorquad = abs(Error_quadratic./cp)*100;
per_errorcub = abs(Error_cubic./cp)*100;
per_errorfour = abs(Error_fourth./cp)*100;
% Plotting
figure(1)
plot(temp,cp,'linewidth',3,'color','b');
hold on
plot(temp,predicted_cp1,'linewidth',3,'color','r');
xlabel('Temperature in [K]');
ylabel('Specific Heat in [KJ/Kmol-K]');
legend('Original Dataset','Linear Curvefit');
figure(2)
plot(temp,cp,'linewidth',3,'color','b');
hold on
plot(temp,predicted_cp2,'linewidth',3,'color','r');
xlabel('Temperature in [K]');
ylabel('Specific Heat in [KJ/Kmol-K]');
legend('Original Dataset','Quadratic Curvefit');
figure(3)
plot(temp,cp,'linewidth',3,'color','b');
hold on
plot(temp,predicted_cp3,'linewidth',3,'color','r');
xlabel('Temperature in [K]');
ylabel('Specific Heat in [KJ/Kmol-K]');
legend('Original Dataset','Cubic Curvefit')
figure(4)
plot(temp,cp,'linewidth',3,'color','b');
hold on
plot(temp,predicted_cp4,'linewidth',3,'color','r');
xlabel('Temperature in [K]');
ylabel('Specific Heat in [KJ/Kmol-K]');
legend('Original Dataset','Fourth order Curvefit')
% Table
T1 = table(cp,predicted_cp1,Error_linear,per_errorlin);
T2 = table(cp,predicted_cp2,Error_quadratic,per_errorquad);
T3 = table(cp,predicted_cp3,Error_cubic,per_errorcub);
T4 = table(cp,predicted_cp4,Error_cubic,per_errorfour);
%to reduce the error further more, dividing the data into 6 divisions
%dividion 1 (300-900)
cp2=cp(1:600);
temp2=temp(1:600);
co_eff2=polyfit(temp2,cp2,1);
pred_cp2=polyval(co_eff2,temp2);
figure(5)
hold on
plot(temp2,cp2,'linewidth',2,'color','b')
plot(temp2,pred_cp2,'linewidth',2,'color','r')
xlabel('TEMPERATURE')
ylabel('SPECIFIC HEAT')
legend('orginal value2','predicted value 2')
error1=(cp-predicted_cp1);
err1=max(error1);
disp("The error corresponding to no division of data")
disp(err1)
error2=(cp2-pred_cp2);
err2=max(error2);
disp("The error corresponding to division of data")
disp(err2)
Results:
Answers to asked questions:
1. How to make curve fit perfectly?
1.There can be two ways through which we can minimize our error 1. By increasing the order of the polynomial until the error reduces.
2. Another way to reduce error was dividing the temperature values in smaller divisions
2.How to get the best fit?
Upon finding a curve which satisfies the data points there are 4 quantities which help us measure the goodness of fit criteria or how well the equation is a representing the datapoints. There are,
The sum of squares due to error (SSE)
R-square
Adjusted R-square
Root mean squared error (RMSE)
3. What could be done to improve the cubic fit?
We can divide the temperature values into smaller intervals.