Yekver
Yekver

Reputation: 5195

regression in matlab

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

Upvotes: 0

Views: 1136

Answers (1)

A. K.
A. K.

Reputation: 38254

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

Upvotes: 1

Related Questions