Multivariate Linear Regression in Matlab Programming

MATLAB Illustration

Introduction

Multivariate linear regression is an extension of simple linear regression, where the dependent variable yyy depends on two or more independent variables x1,x2,...,xnx_1, x_2, ..., x_nx1?,x2?,...,xn?.

The model is expressed as:

y=β0+β1x1+β2x2+?+βnxn+?y = beta_0 + beta_1 x_1 + beta_2 x_2 + dots + beta_n x_n + epsilony=β0+β1?x1+β2x2?+?+βn?xn?+?

 

MATLAB provides functions like fitlm and matrix operations to perform multivariate regression efficiently.


Step 1: Prepare Data

Suppose we have the following dataset:

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

Here, X has two features, and y is the target variable.


Step 2: Using Matrix Approach (Least Squares)

Add a column of ones for the intercept:

X_aug = [ones(size(X,1),1) X]; % Augmented matrix with interceptbeta = (X_aug' * X_aug) (X_aug' * y); % Least squares solutiondisp('Regression Coefficients:')disp(beta)
  • beta(1) → Intercept (β0\beta_0β0?)

  • beta(2:end) → Coefficients for features (β1,β2,...beta_1, beta_2, ...β1?,β2?,...)


Step 3: Using MATLAB’s fitlm Function

fitlm provides a higher-level interface with statistics:

mdl = fitlm(X, y);disp(mdl)
  • View coefficients, R-squared, p-values, and confidence intervals

  • predict function can be used for new data:

new_X = [6 9];y_pred = predict(mdl, new_X);disp(['Predicted Value: ', num2str(y_pred)])

Step 4: Visualizing Regression (2 Features Example)

For two features, you can create a 3D plot:

[X1, X2] = meshgrid(1:0.5:6, 2:0.5:9);Y_fit = beta(1) + beta(2)*X1 + beta(3)*X2;scatter3(X(:,1), X(:,2), y, 'filled')hold onsurf(X1, X2, Y_fit)xlabel('X1')ylabel('X2')zlabel('y')title('Multivariate Linear Regression')grid onhold off

Step 5: Evaluating the Model

  • R-squared: Measures the goodness of fit

  • Residual analysis: Check errors for randomness

  • p-values: Test statistical significance of coefficients

fitlm provides these directly:

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

Step 6: Applications of Multivariate Linear Regression

  • Predicting house prices based on size, location, and features

  • Sales forecasting using multiple market factors

  • Engineering and scientific data modeling

  • Machine learning feature-based prediction


Conclusion

Multivariate linear regression in MATLAB allows you to model complex relationships involving multiple independent variables. Using matrix operations or fitlm, you can efficiently compute coefficients, make predictions, and analyze model performance.

Mastering this technique is essential for data analysis, predictive modeling, and engineering applications where multiple factors influence outcomes.

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.

MATLAB Autonomous Vehicle Path Planning Project for Students: Step-by-Step with Code

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

Battery Management System (BMS) in Electric Vehicles Using MATLAB & Simulink: A Comprehensive Guide

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