CURVE FITTING USING MATLAB
AIM
Code to fit a linear and cubic polynomial for the Cp data.
INTRODUCTION
Curve fitting is the process of constructing a curve, or mathematical function, that has the best fit to a series of datapoints, possibly subject to constraints.
Curve fitting is one of the most powerful and most widely used analysis tools in Origin. Curve fitting examines the relationship between one or more predictors (independent variables) and a response variable (dependent variable), with the goal of defining a "best fit" model of the relationship.
The first degree polynomial equation cp=a*T + b is a line with slope a. A line will connect any two points, so a first degree polynomial equation is an exact fit through any two points with distinct x coordinates.
If the order of the equation is increased to a second degree polynomial, the following results:
cp=a*T^2 + b*T + c This will exactly fit a simple curve to three points.
If the order of the equation is increased to a third degree polynomial, the following is obtained: cp= a*T^3 + b*T^2 + ct +d ;.This will exactly fit four points.
Fitting other functions to data points
Other types of curves, such as trignomentric functions (such as sine and cosine), may also be used, in certain cases.
Fitting a circle by geomentric fit
The method elegantly transforms the ordinarily non-linear problem into a linear problem that can be solved without using iterative numerical methods, and is hence much faster than previous techniques.
Fitting a ellipse by geomentric fit
The above technique is extended to general ellipses by adding a non-linear step, resulting in a method that is fast, yet finds visually pleasing ellipses of arbitrary orientation and displacement.
Finally This can be applied to the surfaces.
MAIN PROGRAM
clear all
close all
clc
%preparing the data
cp_data=load('data (2)') %reading the files
temperature=cp_data(:,1) %data in 1st column
cp=cp_data(:,2); %data in 2nd column
% 1degree polynomial-curve fit
% cp=a*T + b
coeffs1= polyfit(temperature,cp,1);
predicted_cp1=polyval(coeffs1, temperature);
% 2degree polynomial-curve fit
% cp=a*T^2 + b*T + c
coeffs2= polyfit(temperature,cp,2);
predicted_cp2=polyval(coeffs2, temperature);
% 3degree polynomialcurve fit
% cp= a*T^3 + b*T^2 + ct +d
[coeffs3,S,mu] = polyfit(temperature,cp,3);
predicted_cp3=polyval(coeffs3, temperature,[],mu);
%compare curve fit for 1degree polynomial with original data
figure(1)
plot(temperature,cp,'linewidth',3)
hold on
plot(temperature,predicted_cp1,'linewidth',3,'color','r')
xlabel('temperature [k]')
ylabel('specific heat[kj/mol-k]')
legend('original dataset','predicted cp1')
title('curve fit for 1 degree polynomial with original data')
%compare curve fit for 2degree polynomial with the original data
figure(2)
plot(temperature,cp,'linewidth',3)
hold on
plot(temperature,predicted_cp2,'linewidth',3,'Color','r')
xlabel('temperature [k]')
ylabel('specific heat [kj/kmol-k]')
legend('original dataset','predicted cp2')
title('curve fit for 2 degree polynomial with original data')
%compare curve fit for 3degree polynomial with the original data
figure(3)
plot(temperature,cp,'linewidth',3)
hold on
plot(temperature,predicted_cp3,'linewidth',3,'Color','r')
xlabel('temperature [k]')
ylabel('specific heat [kj/kmol-k]')
legend('original dataset','predicted cp3')
title('curve fit for 3 degree polynomial with original data')
%Goodness of fit
%R-Square for 3degree polynomial
error = predicted_cp3-cp(:,1);
sse=sum(error.^2); %Sum of Squares Due to Error
ssr= sum((predicted_cp3-mean(cp(:,1))).^2);% ssr=squares of the regression
sst=ssr+sse; %total sum of squares (SST).
%R_square is the ratio of the sum of squares of the regression (SSR)
% and the total sum of squares (SST)
R_square = ssr/sst
PROCEDURE
Curve fit is a mathematical function we can assume as quadratic cubic or nth order polynomial we can take the function and find the best values of coefficient to closely represent the data
Initially we are reading the files using load command.
Data we are taking is specific heat(cp) as a function of temperature .cp varies with temperature.
First we are calculating the coefficients using polyfit command.
p= polyfit(x,y,n) 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.
[p,s,mu] = polyfit(x,y,n) also returns 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.
With the values of coefficient we can predict the value of specific heat(cp) using polyval command
y = polyval(p,x) evaluates the polynomial p at each point in x.
y= polyval(p,x,[],mu) or [y,delta] = polyval(p,x,s,mu) use the optional output mu produced by polyfit to center and scale the data. mu(1) is mean(x), and mu(2) is std(x). Using these values, polyval centers x at zero and scales it to have unit standard deviation,
This centering and scaling transformation improves the numerical properties of the polynomial.
Finally we are comparing curve fit for 1 ,2,3 ,nth degree polynomial with original data.(cp VS temperature).
RESULTS
GOODNESS OF FIT
.
How to make a curve fit perfectly?
Perfect fit means, the curve should fit the original curve without showing any errors
R2 an indicator of the "goodness of fit is exactly 1.0000 when the fit is perfect, less than that when the fit is imperfect
By increasing the degree of polynomial we can make the curve smoothen.
How to get the best fit?
Best fit” simply means that the differences between the actual measured values and the values predicted by the model equation are minimized. It does not mean a "perfect" fit; in most cases, a least-squares best fit does not go through all the points in the data set.
Evaluating Goodness of Fit
- 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?
[p,s,mu] = polyfit(x,y,n) we can scale and center.
By split-wise method we can improve the cubic fit .
Error here is the polynomial is baldy conditioned we have to center and scale it .
.
REFERENCES
https://www.mathworks.com/help/curvefit/evaluating-goodness-of-fit.html
https://www.mathworks.com/help/matlab/ref/polyfit.html#bufcrts
https://www.mathworks.com/help/matlab/ref/polyval.html#d120e962695