Curve Fitting using Matlab program and Measuring fitness characteristics
Curve Fit using Matlab Program
Aim: To perform linear and cubic curve fit to set of given data using Matlab program and measure the characteristic of the curve fit performed.
Theory:
Curve fitting is important and useful in mathematics as it describes how your data changes mathematically. You can perform additional mathematical operation which difficult on previous data set. For example, when plotting Population data of a country with respect to time, if you use curve fit it can give function that tell how population changes with respect to time.
In this project, we have given data set of values of Specific Heat at different Temperatures and we have to perform linear and cubic curve fit and measure the fitness characteristics.
Program Description:
(1) First we have to load the data file, for that we use load command to read the file and store it in an array. After that we store the values of temperature and cp in two separate variables.
(2) Now, we first perform linear curve fitting, for that we consider a polynomial function of degree n (here, n=1). Now we have to find the best value for the co-efficients so that we can use this particular formula to closely represent the data.
(3) We use polyfit command to get the co-efficient and use the co-efficient to predict the values of cp using polyval command.
(4) After that we compare the original dataset and the curve fit performed by plotting both the curves and measure the characteristic of the fit using the curve fit toolbar.
(5) Similarly we can write program for other high degree polynomial.
Matlab program for Linear curve fitting:
% Program for Linear curve fitting
clear all
close all
clc
%Preparaing the data
cp_data = load('data');
temperature = cp_data(:,1);
cp = cp_data(:,2);
%Curve fit using linear polynomial
%finding coefficients for linear polynomial
coeffs = polyfit(temperature,cp,1);
% using the coefficient to predict the values of cp
predicted_cp = polyval(coeffs,temperature);
%Compare Linear curve fit with original data
%First plotting curve for actual data file
plot(temperature,cp,'linewidth',3)
hold on
%plotting for the predicted cp data for linear polynomial
plot(temperature,predicted_cp,'linewidth',3,'color','r')
xlabel('Temperature[K]')
ylabel('Specific heat [KJ/Kmol-K]')
legend('Original Dataset','Linear Curve Fit')
Result for linear curve fit:
Matlab program for Cubic curve fitting:
%A program for cubic curve fitting with simple linear regression
clear all
close all
clc
%Preparaing the data
cp_data = load('data');
temperature = cp_data(:,1);
cp = cp_data(:,2);
%Curve fit using cubic polynomial
%finding coefficients for cubic polynomial
coeff = polyfit(temperature,cp,3);
% using the coefficient to predict the values of cp
predicted_cp = polyval(coeff,temperature);
%Compare cubic curve fit with original data
%First plotting curve for actual data file
plot(temperature,cp,'linewidth',3)
hold on
%plotting for the predicted cp data for cubic polynomial
plot(temperature,predicted_cp,'linewidth',3,'color','r')
xlabel('Temperature[K]')
ylabel('Specific heat [KJ/Kmol-K]')
legend('Original Dataset','Cubic Curve Fit')
Error Estimation of cubic curve:
Following Error is displayed in the command window which says Polynomial is badly conditioned. Therefore, to rectify it we use the following function in matlab
[p,S,mu] = polyfit(x,y,n)
Which returns three values, p is the coefficient for a polynomial of degree n that is a best fit (in a least squares sense) for the data in y. The coefficient in p are in descending powers, and the length of p is n+1. Structure S that can be used as an input to polyval to obtain error estimates. mu, which is a two-element vector with centering and scaling values, mu(1) is mean(x) and mu(2) is std(x), using these values, polyfit centers x at zero and scales it to have unit standard deviation.
This centering and scaling transformation improves the numerical properties of both the polynomial and the fitting algorithm.
We use this three outputs to fit a cubic and 5th degree polynomial using centering and scaling, which improves the numerical properties of the problem. Polyfit centers the data in year at 0 and scales it to have a standard deviation of 1, which avoids an ill-conditioned Vandermonde matrix in the fit calculation
[cp_coeff,~,mu] = polyfit(temperature,cp,3);
Use polyval with four inputs to evaluate cp_coeff with scaled temperatures,
(temp – mu(1))/mu(2).
Using this method we write program for cubic and other higher degree polynomials:
Matlab program for cubic fit(Linear regression with error estimate):
% A program for cubic curve fitting using linear regression with error
% estimation
clear all
close all
clc
%Preparaing the data
cp_data = load('data');
temperature = cp_data(:,1);
cp = cp_data(:,2);
%Curve fit using cubic polynomial
%by linear regression with error estimation
%finding coefficients for cubic polynomial with 3 output
[p,S,mu] = polyfit(temperature,cp,3);
% using the coefficient to predict the values of cp
predicted_cp = polyval(p,temperature,S,mu);
%Compare cubic curve fit with original data
%First plotting curve for actual data file
plot(temperature,cp,'linewidth',3)
hold on
%plotting for the predicted cp data for cubic polynomial
plot(temperature,predicted_cp,'linewidth',3,'color','r')
xlabel('Temperature[K]')
ylabel('Specific heat [KJ/Kmol-K]')
legend('Original Dataset','Cubic Curve Fit')
Now there is no error showed in the command box.
Result for cubic curve fit:
Simlarly we can write program for Higher order polynomial to increase the fitness of the curve. Result for 7th order polynomial is as shown below:
Result for 7th order polynomial:
How to make a curve fit perfectly?
In order to make perfect fit we must consider error estimates as well. Perfect fit means, the curve should fit the original curve exactly without showing any errors (such as centering and scaling errors) in that particular degree of polynomial. Perfect can be best fit but best fit cannot be perfect fit. To get the best fit we can either increase the polynomial or use the splitwise polynomial method.
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 representing datapoints. There are,
(1)The sum of squares due to error (SSE)
(2)R-square
(3)Adjusted R-square
(4)Root mean square
Using these four quantities Matlab effectively deduces the goodness of the fit, typically is R-square is greater than 0.95 the fit is assumed to be good.
What could be done to improve the cubic fit?
Cubic fit can be improved by using splitwise polynomial method.
Validation of Fit:
Linear fit:
From the table of fits we can see the value of R square is 0.92(<0.95). Therefore cannot be considered as good fit.
Cubic fit (after estimating centering and scaling errors):
From the table of fits we can get the value of R square is 0.9967(<0.95). Therefore can be assumed as good fit, But not the best fit.
Therefore we can improve fit by increasing the polynomial or by splitwise polynomial method.
High order polynomial(7th degree polynomial):
From the table of fits we can get the value R square is 0.9999; which is greater than linear and cubic values of R square and also better fit than linear and cubic fit. Hence we can say that goodness of fit increases as degree of polynomial increases.
Conclusion:
- We successfully performed linear and cubic curve fitting on the given data set and measured the characteristics of each fit.
- We can also conclude that by increasing the order of polynomial increases the goodness of the fit.