Multivariate Linear Regression in Matlab Programming

MATLAB Illustration

Multivariate linear regression extends simple linear regression by modeling the relationship between a single dependent target variable (y) and multiple independent predictor variables ((x_1, x_2, dots, x_n)).

The general mathematical model is expressed as:

[y = beta_0 + beta_1 x_1 + beta_2 x_2 + dots + beta_n x_n + epsilon]
  • (beta_0): Intercept term
  • (beta_1, beta_2, dots, beta_n): Feature slope coefficients
  • (epsilon): Residual error term

In MATLAB, you can perform multivariate linear regression using either raw matrix operations (Ordinary Least Squares) or built-in functions like fitlm().

Step 1: Preparing the Dataset

Define your predictor matrix (X) (where each column represents a feature and each row an observation) and target vector (y):

MATLAB
% Independent variables (2 features: X1 and X2) X = [1 2; 2 3; 3 5; 4 7; 5 8];
% Dependent target variable y = [3; 5; 7; 10; 12];

Step 2: Matrix Approach (Ordinary Least Squares)

To solve for coefficients using matrix algebra, augment (X) with a column of ones to represent the intercept (beta_0), then solve (boldsymbol{beta} = (X^T X)^{-1} X^T y):

MATLAB
% Add column of ones for the intercept X_aug = [ones(size(X,1), 1) X];
% Calculate coefficients using normal equations (or backslash operator) beta = (X_aug' * X_aug)  (X_aug' * y);
disp('Calculated Coefficients (Intercept, Beta1, Beta2):');
disp(beta);

Step 3: Using fitlm() for Higher-Level Statistical Analysis

The fitlm() function automatically fits the model and calculates key statistical metrics including (R^2), p-values, and standard errors:

MATLAB
% Fit linear regression model mdl = fitlm(X, y);
disp(mdl);
% Predict response for new observation [X1=6, X2=9] new_X = [6 9];
y_pred = predict(mdl, new_X);
disp(['Predicted Value: ', num2str(y_pred)]);

Step 4: Visualizing a 2-Feature Regression Surface

When working with two predictor features, you can visualize the regression plane in 3D using scatter3() and surf():

MATLAB
% Create 2D grid for surface plotting [X1_grid, X2_grid] = meshgrid(1:0.5:6, 2:0.5:9);
Y_fit = beta(1) + beta(2)*X1_grid + beta(3)*X2_grid;
% Plot data points and fitted surface 
figure;
scatter3(X(:,1), X(:,2), y, 'filled', 'MarkerFaceColor', 'r');
hold on;
surf(X1_grid, X2_grid, Y_fit, 'FaceAlpha', 0.5);
xlabel('Feature X1');
ylabel('Feature X2');
zlabel('Target Y');
title('Multivariate Linear Regression Surface');
grid on;
hold off;

Step 5: Evaluating Model Fit

Inspect model performance using stats extracted directly from the fitted model object:

MATLAB
% Extract Ordinary R-squared value R2 = mdl.Rsquared.Ordinary;
disp(['R-squared: ', num2str(R2)]);

Practical Applications

  • Real Estate Valuation: Estimating home prices based on square footage, bedroom count, and age.
  • Financial Forecasting: Modeling stock or revenue returns using multiple economic indicators.
  • Engineering Systems: Analyzing multi-factor sensor measurements and system degradation.

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.

How to Solve Differential Equations in MATLAB (ode45, ode15s, bvp4c)

Differential equation assignments usually boil down to three scenarios: standard initial value problems, stiff systems that crash normal solvers, and boundary value problems where conditions are split between two ends of a domain. This guide walks through how to pick the right MATLAB solv

Physics-Informed Neural Networks (PINNs) in MATLAB: Complete Guide

1. Why Standard AI Fails on Real-World Physics Problems If you've ever tried training a standard deep learning model to predict fluid dynamics, structural stress, or heat transfer, you've likely hit a wall. Standard neural networks excel at recognizing patterns in images or text, but wh