Got Questions? Get Answers.
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
need some help for this linear regression that was assigned for homework

Subject: need some help for this linear regression that was assigned for homework

From: omar rodriguez

Date: 12 Apr, 2012 03:29:13

Message: 1 of 3

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 data-sets.

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')
  

Subject: need some help for this linear regression that was assigned for homework

From: Sargondjani

Date: 12 Apr, 2012 06:08:19

Message: 2 of 3

"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 data-sets.
>
> 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

Subject: need some help for this linear regression that was assigned for homework

From: Claudio

Date: 13 Apr, 2012 15:20:51

Message: 3 of 3

"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 data-sets.
> >
> > 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;

 

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us