Regression Analysis
CURVE FITTING
Aim
To perform regression analysis for the specific heat of Oxygen at constant temperature.
Regression analysis
Regression analysis is a statistical tool used to estimate the relationship between two or more variables. It can also be used to predict the nature of dependent variable in future. Regression analysis is of three types:
- Simple linear regression
- Multiple linear regression
- Non-linear regression
Simple linear regression
Simple linear regression is a model that is used to assess the relationship between one independent variable and dependent variable. Mathematical equation is:
`y=ax+b`
Where, y is dependent variable
x is independent variable
a is slope of the fitted line
b is y-intercept
Multiple linear regression
It is as same as the simple linear regression model but the only difference is that, it deals with many independent variables. Mathematical equation is:
`y=a+b1x1+b2x2+⋯+bnxn`
Where, y is dependent variable
x1,x2,…,xn are independent variables
a is y-intercept
b1,b2,….,bn are slopes
Non-linear regression
It is used to fit a polynomial of order 2 or more than 2 for the observed data.
Correlation coefficient (R-Square)
It measures how much the fitted model explains the variation in data. Higher the value of R-square, higher the accuracy of the model
Code:
%A Program to fit a linear and cubic polynomial for the variation of
%specific heat at constant pressure of Oxygen with respect to temperature
clear all
close all
clc
%loading data file
specific_heat = load('Cp_Vs_Temp_data');
temperature = specific_heat(:,1);
cp = specific_heat(:,2);
%fitting linear polynomial for the data
linear = polyfit(temperature,cp,1);
predicted_linear = polyval(linear,temperature);
%fitting cubic polynomial for the data
[cubic,S,mu] = polyfit(temperature,cp,3);
predicted_cubic = polyval(cubic,temperature,S,mu);
%plotting the predicted and original values
%plot(temperature,cp,'color','m',temperature,predicted_linear,temperature,predicted_cubic)
plot(temperature,cp,'color','m','linewidth',3)
hold on
plot(temperature,predicted_linear,'color','c','linewidth',3)
hold on
plot(temperature,predicted_cubic,'color','k','linewidth',3)
hold off
xlabel('Temperature in K');
ylabel('Specific heat at constant pressure');
title('Regression for Specific heat of Oxygen');
legend('Original value', 'linear regression', 'cubic regression');
%evaluating the fit by calculating R^2, which is a regression co-efficient
%R^2 = 1 - SSres/SStot
Rsq_linear = 1 - sum((cp - predicted_linear).^2)/sum((cp - mean(cp)).^2);
Rsq_cubic = 1 - sum((cp - predicted_cubic).^2)/sum((cp - mean(cp)).^2);
Step by step explanation:
- Import the given file which contains the data to be fitted with linear and cubic polynomial
- Split the data accordingly as dependent and independent variables.
- Using polyfit command in MATLAB, obtain the coefficient of linear and cubic polynomial and store it in a variables called linear and cubic respectively.
- Then, for the coefficients obtained, the specific heat values are predicted for all temperature values using polyval command in MATLAB.
- Graphs are plotted for given values and predicted values of linear and cubic model.
- Correlation coefficient is calculated for both regression equations
Results
R-square value of linear and cubic models are 0.9249 and 0.9967 respectively.
How to make a curve fit perfectly?
There are many methods for fitting a curve to observed data. One such method is, Least Square method. In MATLAB, polyfit works by using method of least squares. In this method, we try to minimise the square of deviation from the observed data i.e. this method tries to keep R-square as maximum as possible. A perfect fit is a fit which completely describes the variation of data without any error (R-square = 1). To get a perfect fit, either higher degree polynomial must be used or other methods like split wise method can be used.
How to get the best fit?
A best fit describes the maximum variation that is possible for that degree of polynomial. Beyond which, we can’t increase the accuracy of the fit for that degree of polynomial.
What could be done to improve the cubic fit?
Since, in our problem, many points are there, there are some oscillation in fitting the data with higher degree polynomial. This will lead to poor fit which is not smooth in nature. This can be avoided by using centering and scaling technique in MATLAB or using other methods like split wise method. In centering and scaling method, the values in x will be centered to zero with unit standard deviation.
Errors faced
While loading the data file, file name is given as variable instead of a string. Syntax is studied in MATLAB website and solved.
References: