Mathematics Stack Exchange is a question and answer site for people studying math at any level and professionals in related fields. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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:

 for $i=(n-1):-1:1 
 g=g.*u+x(i)  end

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

share|cite|improve this question
for the coefficients of the polyfit function how I can change the format of this coefficients to the long? Plz help me as soon. – user83693 Jun 24 '13 at 14:20
up vote 3 down vote accepted

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

p = polyfit(t,y,2);

fit = polyval(p,t);


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.

share|cite|improve this answer
thank you, since you are familiar with math lab, could you answer this question too? Thanks… – ASROMA Nov 4 '12 at 0:43

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.