Linear regression in matlab
Linear regression is a statistical technique that is used to analyze the relationship between two variables. It is widely used in various fields such as economics, finance, and social sciences to predict the behavior of one variable based on the other. In this article, we will discuss the implementation of linear regression in MATLAB.
What is Linear Regression?
Linear regression is a technique to find the best fit line that describes the relationship between two variables. It is a method to model a linear relationship between a dependent variable (Y) and one or more independent variables (X). The equation for a simple linear regression is:
Y = a + bX + e
Implementation of Linear Regression in MATLAB
MATLAB provides a variety of built-in functions to perform linear regression. The most commonly used function is the polyfit function. The polyfit function is used to find the coefficients of a polynomial that best fits the data. To perform linear regression using polyfit, we need to provide the independent and dependent variables and the degree of the polynomial.
Example:
Suppose we have a dataset containing the marks of students in a subject and the number of hours they studied. We want to find the relationship between the marks and the number of hours studied.
The data can be represented in MATLAB as follows:
marks = [56, 78, 87, 65, 92, 75, 81, 94, 68, 72];
hours = [2, 3, 4, 2.5, 5, 3.5, 4, 5.5, 3, 3.5];
We can plot the data using the scatter function as follows:
scatter(hours, marks)
xlabel('Number of Hours Studied')
ylabel('Marks Obtained')
The scatter plot shows a positive linear relationship between the number of hours studied and the marks obtained.
To find the best fit line, we can use the polyfit function as follows:
p = polyfit(hours, marks, 1);
The polyfit function returns the coefficients of the polynomial that best fits the data. In this case, it returns the slope and intercept of the best fit line.
To plot the best fit line, we can use the polyval function as follows:
yfit = polyval(p, hours);
hold on
plot(hours, yfit, 'r')
legend('Data', 'Best Fit Line')
The polyval function evaluates the polynomial at the given values of X (hours) and returns the corresponding values of Y (marks). The 'r' in the plot function specifies the color of the line.
The output of the code is shown below:
The red line represents the best fit line that describes the relationship between the number of hours studied and the marks obtained.