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]In MATLAB, you can perform multivariate linear regression using either raw matrix operations (Ordinary Least Squares) or built-in functions like fitlm().
Define your predictor matrix (X) (where each column represents a feature and each row an observation) and target vector (y):
% 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];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):
% 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);The fitlm() function automatically fits the model and calculates key statistical metrics including (R^2), p-values, and standard errors:
% 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)]);When working with two predictor features, you can visualize the regression plane in 3D using scatter3() and surf():
% 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;Inspect model performance using stats extracted directly from the fitted model object:
% Extract Ordinary R-squared value R2 = mdl.Rsquared.Ordinary;
disp(['R-squared: ', num2str(R2)]);“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.
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
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