Linear Regression in Matlab Programming

MATLAB Illustration

Introduction

Linear regression is one of the most widely used methods for modeling the relationship between a dependent variable yy and one or more independent variables xx. In MATLAB, linear regression can be implemented easily using built-in functions or matrix operations, allowing you to predict, analyze trends, and fit data efficiently.

 

 


Step 1: Define Data

Consider the following dataset:

 
x = [1 2 3 4 5 6]; y = [2.2 2.8 3.6 4.5 5.1 5.9];

Here, x is the independent variable and y is the dependent variable.


Step 2: Perform Linear Regression

Method 1: Using polyfit()

 
coeff = polyfit(x, y, 1); % 1 = linear slope = coeff(1); intercept = coeff(2); fprintf('Slope: %.2f, Intercept: %.2f ', slope, intercept);

Method 2: Using Matrix Approach (Least Squares)

 
X = [x' ones(length(x),1)]; % Design matrix Y = y'; theta = (X'*X)(X'*Y); % Least squares solution slope = theta(1); intercept = theta(2); fprintf('Slope: %.2f, Intercept: %.2f ', slope, intercept);

Method 3: Using fitlm() for Regression Model

 
mdl = fitlm(x, y); % Linear regression model disp(mdl)

fitlm provides additional statistics, such as R-squared, p-values, and confidence intervals.


Step 3: Predict and Visualize

Compute predicted values:

 
y_fit = slope*x + intercept;

Visualize the regression line:

 
scatter(x, y, 'filled') hold on plot(x, y_fit, 'r-', 'LineWidth', 2) title('Linear Regression in MATLAB') xlabel('X') ylabel('Y') legend('Data points', 'Regression line') grid on hold off

Step 4: Evaluate the Regression

Check the goodness of fit using R-squared:

 
SS_res = sum((y - y_fit).^2); SS_tot = sum((y - mean(y)).^2); R_squared = 1 - SS_res/SS_tot; fprintf('R-squared: %.4f ', R_squared);

High R-squared values (close to 1) indicate a strong linear relationship.


Step 5: Advantages of Linear Regression in MATLAB

  1. Easy and fast computation of regression coefficients.

  2. Built-in functions provide statistical insights (e.g., fitlm).

  3. Visualize trends and fitted lines easily.

  4. Extendable to multiple regression for multiple variables.


Conclusion

Linear Regression in MATLAB is a powerful tool for predictive modeling, trend analysis, and data fitting. Whether you are analyzing experimental data, financial trends, or engineering measurements, MATLAB allows you to implement linear regression efficiently and visualize results clearly.

By mastering MATLAB regression functions, you can handle both simple and multiple linear regression with confidence.

What Our Students Say

★★★★★

“I got full marks on my MATLAB assignment! The solution was perfect and delivered well before the deadline. Highly recommended!”

Aditi Sharma, Mumbai
★★★★☆

“Quick delivery and excellent communication. The team really understood the problem and provided a great solution. Will use again.”

John M., Australia

Latest Blogs

Explore how MATLAB Solutions has helped clients achieve their academic and research goals through practical, tailored assistance.

Most Trending Topics in MATLAB Simulation in 2026

MATLAB and Simulink continue to be powerful tools for modeling, simulation, and system design across engineering domains. From electric vehicles to smart grids and AI-driven automation, MATLAB simulation is playing a critical role in modern research and industry ap

Multimodal AI Breakthroughs: Implementing Text + Image + Audio Models with MATLAB

Multimodal AI is one of the most exciting frontiers in artificial intelligence research today. In early 2026, multimodal models-systems that process and reason across text, images, audio, and eve