www.gusucode.com > matlab 案例源码 matlab代码程序 > matlab/MultipleRegressionExample.m

    %% Multiple Regression
% This example shows how to use multiple regression to model data that is a function of more than one
% predictior variable.

% Copyright 2015 The MathWorks, Inc.


%%
% When y is a function of more than one predictor variable, the matrix
% equations that express the relationships among the variables must be
% expanded to accommodate the additional data. This is called _multiple
% regression_.

%%
% Measure a quantity $y$ for several values of $x_{1}$ and
% $x_{2}$. Store these values in vectors |x1|, |x2|, and |y|, respectively.
x1 = [.2 .5 .6 .8 1.0 1.1]';
x2 = [.1 .3 .4 .9 1.1 1.4]';
y  = [.17 .26 .28 .23 .27 .24]';

%%
% A model of this data is of the form
%
% $$y = a_{0} + a_{1}x_{1} + a_{2}x_{2}.$$

%%
% Multiple regression solves for unknown coefficients $a_{0}$, $a_{1}$, and
% $a_{2}$ by minimizing the sum of the squares of the deviations of the data from the model (least-squares fit).

%%
% Construct and solve the set of simultaneous equations by forming a design
% matrix, |X|.
X = [ones(size(x1))  x1  x2];

%%
% Solve for the parameters by using the backslash operator.
a = X\y

%%
% The least-squares fit model of the data is
%
% $$y = 0.1018 + 0.4844x_{1} - 0.2847x_{2}.$$

%%
% To validate the model, find the maximum of the absolute value of the
% deviation of the data from the model.
Y = X*a;
MaxErr = max(abs(Y - y))

%%
% This value is much smaller than any of the data values, indicating that this model accurately follows the data.