2011-07-22 119 views
0

我有一個獨立變量回歸的matlab代碼,但如果我有兩個獨立變量(x1和x2)呢?我應該如何修改這個多項式迴歸的代碼?迴歸在matlab

x = linspace(0,10,200)'; % independent variable 
y = x + 1.5*sin(x) + randn(size(x,1),1); % dependent variable 

A = [x.^0, x];  % construct a matrix of permutations 
w = (A'*A)\(A'*y); % solve the normal equation 
y2 = A*w;    % restore the dependent variable 
r = y-y1;    % find the vector of regression residual 

plot(x, [y y2]); 

回答