Project

Curve Fitting in MATLAB

Aim:

To write a code to fit a linear and cubic polynomial for the given Cp vs T data.

Objective:

  • Plot the linear and cubic fit curves along with the raw data points.
  • Explain the parameters used to measure the fitness characteristics for both the curves.

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.

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:

  1. The sum of squares due to error (SSE):

It is the sum of square of individual errors for each point with respect to the actual value

`SSE ("sum of squared errors") = sum_(i=1)^n"Error(i)"^2`

Where, `"Error" = abs(y(i) - "value of " f(x) " at x(i)") = abs(y(i) - f(x(i))`

  1. R-square:

This quantity measures how successful the fit is in explaining the variation of the data. Put another way, R-square is the square of the correlation between the response values and the predicted response values. It is also called the square of the multiple correlation coefficient and the coefficient of multiple determination.

What that means in layman's terms is that the higher the value of R-square the better the fit, the lower it is the lousier the fit, R-square ranges from a value of 0 to 1

`R^2=(SSR)/(SST) , where`

`SSR = sum_(i=1)^n(f(x(i))-Mean)^2`

`SST=SSR+SSE`

  1. Adjusted R-square:

Adjusted R-square is a normalizing variation of R-square which neglects the variation of R-square due to insignificant independent variables.

where,

n - the number of datapoints

k - the number of independent variables

  1. Root mean squared error (RMSE)

This statistic is also known as the fit standard error and the standard error of the regression. It is an estimate of the standard deviation of the random component in the data, and is defined as,

`RMSE = sqrt((sum_(i=1)^n(Y(i)-f(x(i)))^2)/n) = sqrt((SSE)/n)`

where,

n is the total number of datapoints available.

Using these four quantities Matlab effectively deduces the goodness of fit, typically is R-square is greater than 0.95 the fit is assumed to be good.

Workflow:

Program Code:

clear all
close all
clc

% loading the data and initialising variables
data = load('data');
temp = data(:,1);
cp = data(:,2);

% determining coeff for linear fit
coeff1 = polyfit(temp,cp,1);
new_cp1 = polyval(coeff1,temp);

% determining coeff for cubic fit
coeff3 = polyfit(temp,cp,3);
new_cp3 = polyval(coeff3,temp);

% calculating fitness factors for linear fit
for i = 1:numel(temp)
    error11(i) = cp(i) - new_cp1(i);
    error21(i) = new_cp1(i) - mean(cp);
end
SSE1 = sumsqr(error11);
SSR1 = sumsqr(error21); 
SST1 = SSE1 + SSR1;
R_sqr1 = SSR1/SST1;
adj_R_sqr1 = R_sqr1;              % since no. of independent variables is zero
RMSE1 = sqrt(SSE1/numel(cp));

% calculating fitness factors for cubic fit
for i = 1:numel(temp)
    error13(i) = cp(i) - new_cp3(i);
    error23(i) = new_cp3(i) - mean(cp);
end
SSE3 = sumsqr(error13);
SSR3 = sumsqr(error23); 
SST3 = SSE3 + SSR3;
R_sqr3 = SSR3/SST3;
adj_R_sqr3 = R_sqr3;              % since no. of independent variables is zero
RMSE3 = sqrt(SSE3/numel(cp)); 

% plotting and comparing linear fit
figure(1);
hold on
plot(temp,cp,'linewidth',2);
plot(temp,new_cp1,'linewidth',2);
title('Specific heat vs Temperature');
xlabel('Temperature [K]');
ylabel('Specific Heat [KJ/Mole-K]');
legend('given data','linear fit');

% plotting and comparing cubic fit
figure(2);
hold on
plot(temp,cp,'linewidth',2);
plot(temp,new_cp3,'linewidth',2);
title('Specific heat vs Temperature');
xlabel('Temperature [K]');
ylabel('Specific Heat [KJ/Mole-K]');
legend('given data','cubic fit');

Code Explanation:

1. Clearing workspace and initialising temperature and cp values by loading from data file.

2. Determining coefficients of polynomial for linear and cubic fit using polyfit.

3. Determining the new cp values using polyval.

4. Calculating fitness factors for linear and cubic fit.

5. Plotting and comparing the curves for both linear and cubic fit.

Errors:

Basic syntax errors were encountered which were resolved immediately.

Output:

The plots for cp vs T were obtained for both linear and cubic regression.

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 without showing any errors (such as centering and scaling errors) in that particular degree of polynomial. Best fit can always be a perfect fit but perfect fit cannot be a best fit. So, in the 3rd order polynomial, the curve can be fit perfectly by scaling and centering the curve.

How to get the best fit?

In order to get the best fit, we must use a higher order derivative until the error becomes zero or we can also use split wise function to fit multiple curves and remove the error.

What could be done to improve the cubic fit?

In order to improve the cubic fit, we an use scaling entering in the curve by modifying the polyfit command as shown:

References:

Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.