regression in matlab

允我心安 提交于 2019-12-25 04:00:13

问题


I have this matlab code for regression with one indepenpent variable, but what if I have two independent variables(x1 and x2)? How should I modify this code of polynomial regression?

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

回答1:


Matlab has facilities for polynomial regression function polyfit. Have you tried that?

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

But if you want to workout your own formulation,you should probably look at textbook or some online resources on regression e.g.

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



来源:https://stackoverflow.com/questions/6796316/regression-in-matlab

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!