1
$\begingroup$

First of all i am new to Matlab, and not sure whether to use polyfit or something else for my problem.

I plotted a graph with the following matlab code:

 t=transpose(linspace(-1,1,50)) 
 y=1./(1+25*t.^2) 
 n=10 
 A=fliplr(vander(t)) 
 A=A(:,1:n) 
 x=A\y 
 u=linspace(-1,1,1000) 
 g=x(n)*ones(1,1000) 
 for $i=(n-1):-1:1 
 g=g.*u+x(i)  end
 plot(u,g,'-',t,y,'o') 

Now my question is: plot the figure with polynomial fit of polynomial degree 2.
How should i do this?
Thanks

  • 0
    for the coefficients of the polyfit function how I can change the format of this coefficients to the long? Plz help me as soon.2013-06-24

1 Answers 1

3

So you want to fit y as a function of t, right? Use

p = polyfit(t,y,2);

fit = polyval(p,t);

plot(u,g,'-',t,y,'o',t,fit)

The first line is the built-in polynomial fit function. The number 2 is the degree which you specify and it returns the coefficients of the polynomial in p. Note degree 2 means three coefficients. The second line then evaluates the polynomial using the coefficients in p. And then the third, plots them all together.

  • 0
    thank you, since you are familiar with math lab, could you answer this question too? Thanks http://math.stackexchange.com/questions/228552/how-to-solve-this-estimation-problem-in-matlab2012-11-04