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;“I got full marks on my MATLAB assignment! The solution was perfect and delivered well before the deadline. Highly recommended!”
“Quick delivery and excellent communication. The team really understood the problem and provided a great solution. Will use again.”
Explore how MATLAB Solutions has helped clients achieve their academic and research goals through practical, tailored assistance.
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
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