Claudio
Posts:
15
Registered:
11/18/11


Re: need some help for this linear regression that was assigned for homework
Posted:
Apr 13, 2012 11:20 AM


"Sargondjani" wrote in message <jm5rgi$2fl$1@newscl01ah.mathworks.com>... > "omar rodriguez" <omrodez@yahoo.com> wrote in message <jm5i69$1de$1@newscl01ah.mathworks.com>... > > Use the following data point generated using random function and write a matlab code to fit a 2nd order polynomia. Please upload the MATLAB code for the following datasets. > > > > x=[0 .1 .2 .3 .4 .5 .6 .7 .8 .9]; > > y=[70. 74. 78. 80. 81. 83. 84. 85. 87. 87.]; > > > > 1) Use polyfit command. > > 2) Without using the polyfit,. > > > > x=0:.1:0.9; > > y=[70,74,78,80,81,83,84,85,87,87]; > > n=1; > > p=polyfit(x,y,n); > > f=a0+a1*x; > > a0=p(2) > > a1=p(1) > > plot(x,f,x,y,'o') > > > > you should use n=2 for second order polynomial. and then f=a0+a1*x+a2*x.^2 > If you want to do it yourself: > > x=x'; %x should be column vector > > X=[x.^0,x.^1,x.^2]; %of course x.^0 is a vector of 1's > > y=y'; %y should be a column vector > > a=regress(y,X); % or you could do a=X\y but i think regress is better
You should also assign a0 and a1 BEFORE usinge them: a0=p(3); a1=p(2); a2=p(1); f=a0+a1*x+a2*x.^2;

