0

我有这个带有一个独立变量的回归 matlab 代码,但是如果我有两个独立变量(x1 和 x2)怎么办?我应该如何修改这个多项式回归的代码?

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]);
4

1 回答 1

1

Matlab 具有多项式回归函数的功能polyfit。你试过吗?

http://www.mathworks.com/help/techdoc/data_analysis/f1-8450.html

http://www.mathworks.com/help/toolbox/stats/bq_676m-2.html#bq_676m-3

但是如果你想锻炼自己的公式,你可能应该看看教科书或一些关于回归的在线资源,例如

http://www.edwardtufte.com/tufte/dapp/DAPP3a.pdf

于 2011-07-22T21:54:11.027 回答