Least Square Regression in MATLAB Programming

MATLAB Illustration

Least Squares Regression is a fundamental statistical method used to determine the line or curve of best fit through a set of data points. In MATLAB, it forms the basis for data fitting, trend forecasting, and predictive modeling.

The objective is to minimize the sum of squared residuals (the vertical distances between observed data values (y_i) and predicted model values (hat{y}_i)):

[S = sum_{i=1}^{n} (y_i - hat{y}_i)^2]

Step 1: Defining Data Vectors

Define the independent variable vector x and dependent variable vector y:

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

Step 2: Computing Regression Coefficients

For a linear model (y = a x + b), you can compute slope (a) and intercept (b) using built-in functions or linear algebra.

Method 1: Using polyfit()

MATLAB
% 1 indicates a first-degree (linear) polynomial fit coeff = polyfit(x, y, 1);
a = coeff(1);
% Slope b = coeff(2);
% Intercept  fprintf('Slope: %.4f, Intercept: %.4f ', a, b);

Method 2: Using Matrix Algebra (Normal Equations)

MATLAB
% Construct design matrix with a column of ones for intercept X = [x(:) ones(length(x), 1)];
Y = y(:);
% Compute least squares solution: theta = (X^T * X)^(-1) * X^T * Y theta = (X' * X)  (X' * Y);
a = theta(1);
b = theta(2);
fprintf('Slope: %.4f, Intercept: %.4f ', a, b);

Step 3: Predicting Values and Plotting Results

Calculate predicted values y_fit and overlay the regression line on top of the raw data points:

MATLAB
y_fit = a * x + b;
figure;
scatter(x, y, 'filled', 'MarkerFaceColor', 'b');
hold on;
plot(x, y_fit, 'r-', 'LineWidth', 2);
title('Linear Least Squares Regression in MATLAB');
xlabel('Independent Variable (x)');
ylabel('Dependent Variable (y)');
legend('Observed Data', 'Fitted Line', 'Location', 'northwest');
grid on;
hold off;

Step 4: Evaluating Model Goodness-of-Fit ((R^2))

Quantify regression accuracy by computing the Coefficient of Determination ((R^2)):

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

Step 5: Nonlinear Least Squares Regression

For non-linear relationships (such as exponential curves (y = a cdot e^{bx})), fit models using fit() and fittype():

MATLAB
% Define exponential model structure ft = fittype('a*exp(b*x)');
f = fit(x(:), y(:), ft, 'StartPoint', [1 0.1]);
figure;
plot(f, x, y);
title('Nonlinear Exponential Regression Fit');
xlabel('x');
ylabel('y');
grid on;

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.

MATLAB Autonomous Vehicle Path Planning Project for Students: Step-by-Step with Code

One of the most fascinating and sought-after projects for engineering students is autonomous vehicles. One of the main challenges in the development of self-driving cars is path planning, which is the process of determining a safe, collision-free route from start to o

Battery Management System (BMS) in Electric Vehicles Using MATLAB & Simulink: A Comprehensive Guide

The Battery Management System (BMS) serves as the intelligent brain of an electric vehicle (EV) battery pack. It ensures safety, maximizes performance, extends battery life, and optimizes energy usage in real-world driving conditions. As EVs become mainst