CURVE FITTING USING MATLAB
CURVE FITTING USING MATLAB
AIM - To explain the parameters used to measure the fitness characteristics for curves.
OBJECTIVE -
- 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.
RELATED THEORY :-
Curve fitting is the process of constructing a curve, or mathematical functions, which possess the closest proximity to the real series of data. By curve fitting, we can mathematically construct the functional relationship between the observed dataset and parameter values, etc. It is highly effective in the mathematical modelling of some natural processes.
- How to make curve fit perfectly?
There can be two ways through which we can minimize our error
- By increasing the order of the polynomial until the error reduces.
- Another way to reduce error was dividing the temperature values in smaller divisions.
Use split-wise polynomial function for different temperatures for getting the best fit.
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)
- What could be done to improve the cubic fit?
We can divide the temperature values into smaller intervals.
- We can use the command [p,S,mu] for polyfit. This function returns the mean and standard deviation of x and uses these values to plot a curve for higher order of polynomial.
- Use split-wise polynomial function
- Add points with distinct X values.
EXPLANATION –
- We first load the data using load command and allocate temperature and specific heat to two different variables (temperature and Cp) which are first and second column in the data file.
- 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.
- Further we can get the coefficients value of the interpolation curve using polyval function in MATLAB which would be helpful in obtaining the error.
- First we tried to fit in linear polynomial on the Specific heat and Temperature data and in this case the least squared error was quite high.
- To reduce the error ,I subsquently tried Cubic interpolation curve 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.
- When the program is run there is a warning message that the curve is badly conditioned. So the code is re -run using the centering and scaling code.
clear all
close all
clc
%Preparing the data
cp_data = load('data(1)')
temperature = cp_data(:,1);
cp = cp_data(:,2);
%Curve Fits
%Linear Curve Fit
%cp=a*T^1+b*T^0+c
figure (1)
co_effs_linear = polyfit(temperature,cp,1);
predicted_cp_linear = polyval(co_effs_linear,temperature);
plot(temperature,cp,'linewidth',3,'color','r')
hold on
plot(temperature,predicted_cp_linear,'linewidth',3,'color','k')
xlabel('Temperature[K]')
ylabel('Specific Heat[KJ/Kmol-K]')
legend('original dataset','linear curve fit')
%Cubic Curve Fit
%cp=a*T^3+b*T^2+c*T^1+d
figure (2)
co_effs_cubic = polyfit(temperature,cp,3);
predicted_cp_cubic = polyval(co_effs_cubic,temperature);
plot(temperature,cp,'linewidth',3,'color','r')
hold on
plot(temperature,predicted_cp_cubic,'linewidth',3,'color','g')
xlabel('Temperature[K]')
ylabel('Specific Heat[KJ/Kmol-K]')
legend('original dataset','cubic curve fit')
centering and scaling of data to improve the curve
clear all
close all
clc
%Program to make a linear and cubic curve fit
%load the values of temperature and cp
data=load('data(1)');
%Assign temperature values to variable temp
temp=data(:,1);
%Assign cp values to variable cp
cp=data(:,2);
%Making a curve fit of first order
const1=polyfit(temp,cp,1);
%Getting cp values for each temperature of linear curve fit
cp1=polyval(const1,temp);
%Making a curve fit of third order
[p,~,mu]=polyfit(temp,cp,3);
%Getting cp values for each temperature of cubic curved fit
cp3=polyval(p,temp,[],mu);
a=1; %variable to count frame number
%Creating animation of orignal, first order and third order curves
for i=1:99:3100 %loop for orignal curve
plot(temp(i:i+99),cp(i:i+99),'r','linewidth',1); %plot orignal curve
axis([0 3500 900 1350]); %limit the axis
xlabel('Temperature(K)'); %x axis label
ylabel('Specific heat(kJ/kmole-K)') %y axis label
title('Curve fitting') %title of plot
hold on; %holds the figure
Frame(a)=getframe(gcf); %capture frames into Frame
a=a+1; %assigns next frame
end %terminate the loop
for i=1:49:3100 %loop for linear curve
plot(temp(i:i+49),cp1(i:i+49),'b','linewidth',1); %plot linear curve
Frame(a)=getframe(gcf); %capture frame in Frame
a=a+1; %assign next frame
end %terminate the loop
for i=1:49:3100 %loop for cubic curve
plot(temp(i:i+49),cp3(i:i+49),'k','linewidth',1); %plot cubic curve
Frame(a)=getframe(gcf); %capture frame into Frame
a=a+1; %assign next frame
end %terminate the loop
%Creating plot of orignal, first order and third order curve fit
figure(2) %activates the figure 2
plot(temp,cp,'r','linewidth',2); %plot orignal curve
axis([0 3500 900 1350]); %limit the axis
xlabel('Temperature(K)'); %x axis label
ylabel('Specific heat(kJ/kmole-K)') %y axis label
title('Curve fitting') %title of plot
hold on %holds the figure
plot(temp,cp1,'b','linewidth',2); %plot linear curve
plot(temp,cp3,'k','linewidth',2); %plot cubic curve
%create legend at top of plot
legend('orignal','first order curve','third order curve','Location','northwest');
%Creating and saving the animation file
videofile=VideoWriter('curve_fitting.avi','Uncompressed AVI'); %create file
open(videofile); %open file
writeVideo(videofile,Frame); %write frames
close(videofile); %close file
o validate the fitness characteristics, we can also use the CURVE-FITTING TOOLBOX.
R-square is 0.9249, so it is not a good fit.
R-square is 0.9967, hence it is a good fit.
ERRORS –
1.Wrong Spelling caused unwanted error.
Rectified by correcting the spelling.
2.Warning: Polynomial is badly conditioned.
Rectified by centering and scaling the cubic curve.
OUTPUT –
CONCLUSION- Hence, the curve fitting to the given set of raw data is done and also the answers to the questions are given.
The link to the video is given below.
References
1. The minitab blog (https://blog.minitab.com/blog/adventures-in-statistics-2/curve-fitting-with-linear-and-nonlinear-regression)
2. Matlab (https://www.mathworks.com/help/curvefit/curve-fitting.html)