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)]);Our 500+ PhD engineers provide verified MATLAB code, custom Simulink models, and 1-on-1 tutoring with Turnitin plagiarism reports.
Real feedback from students across top engineering universities worldwide.
“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!”
“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!”
Explore deep-dive technical articles written by our engineering team to master complex MATLAB & Simulink topics.
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...