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.

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

Solving 6-DOF Aircraft & Quadrotor Flight Dynamics in MATLAB: ode45 vs. ode15s and Quaternion Kinematics

Simulating a 6-Degree-of-Freedom (6-DOF) flight vehicle in MATLAB looks straightforward on paper. You set up Newton-Euler equations of motion, calculate aerodynamic...