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]Define the independent variable vector x and dependent variable vector y:
x = [1 2 3 4 5];
y = [2.2 2.8 3.6 4.5 5.1];For a linear model (y = a x + b), you can compute slope (a) and intercept (b) using built-in functions or linear algebra.
% 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);% 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);Calculate predicted values y_fit and overlay the regression line on top of the raw data points:
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;Quantify regression accuracy by computing the Coefficient of Determination ((R^2)):
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);For non-linear relationships (such as exponential curves (y = a cdot e^{bx})), fit models using fit() and fittype():
% 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;Our 500+ PhD engineers provide verified MATLAB code, custom Simulink models, and 1-on-1 tutoring with Turnitin plagiarism reports.
Real feedback from students across top engineering universities worldwide.
“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!”
“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!”
Explore deep-dive technical articles written by our engineering team to master complex MATLAB & Simulink topics.
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...
The Memory Wall on Edge Microcontrollers Standard neural networks store weights and compute activations using single-precision floating point (32-bit float...