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;

Need Expert Guidance with MATLAB or Simulink?

Our 500+ PhD engineers provide verified MATLAB code, custom Simulink models, and 1-on-1 tutoring with Turnitin plagiarism reports.

Verified Feedback

What Engineering Students Say

Real feedback from students across top engineering universities worldwide.

Verified Student

“I got full marks on my MATLAB DSP assignment! The filter design code was completely vectorized, the frequency response plots were exact, and the delivery was 8 hours before my deadline. Highly recommended!”

AS

Aditi Sharma

IIT Bombay • Signal Processing Coursework
Verified Student

“Our Simulink EV powertrain model had severe algebraic loop and solver errors. The MATLABSolutions team fixed the solver configuration in 4 hours and provided an annotated scope diagram. Lifesaver for my final year!”

JM

John M.

Monash University, Australia • Simulink Dynamic Model
Technical Knowledge Base

Latest MATLAB Guides & Tutorials

Explore deep-dive technical articles written by our engineering team to master complex MATLAB & Simulink topics.

MATLAB Guide 5 Min Read

Run Convolutional Neural Networks on ARM Cortex-M with MATLAB

Why Run CNNs on Cortex-M Processors? ARM Cortex-M cores power millions of edge sensors, industrial controllers, and medical wearables. Running 1D or 2D Convolutional N...

MATLAB Guide 5 Min Read

INT8 Deep Learning Quantization in MATLAB for Embedded Systems

The Memory Wall on Edge Microcontrollers Standard neural networks store weights and compute activations using single-precision floating point (32-bit float...