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

I am new to Matlab, help please. In the book I saw this picture for the following polynomial: $$g(t)=x_1+x_2t+x_3t^2+ \ldots +x_{10}t^9$$ for some $t$ and $g(t)$.

enter image description here

The following code is given:

for i=(n-1):-1:1

I am trying this code in Matlab but not getting the same picture. I think I need to input some code at first, before the above written code, that somehow tells to Matlab the code is for polynomial of degree 9. I do not see how this code tells about polynomial degree to Matlab.

What do I need to do to get the same picture?

share|cite|improve this question
up vote 1 down vote accepted

I just copied and pasted that code into a new .m file and ran it, without adding any extra code at all, and I got the correct plot.enter image description here

Here's my code (exactly the same as the code given in your question):

enter image description here

share|cite|improve this answer
Did you write anything to represent this polynomial: $g(t)=x_1+x_2t+x_3t^2+ \ldots +x_{10}t^9$. If no, then how matlab understood that this polynomial is degree 9, if i were to do the same for degree 2, which code should i changed? Thanks – ASROMA Nov 5 '12 at 1:25
I literally wrote nothing, I just copied your code with ctrl+c and pasted it with ctrl+v, then ran the code. I'll edit my answer to include my code. What happens for you when you run the code? – littleO Nov 5 '12 at 1:36
The line $A = A(:1:n)$ with $n = 10$ is doing something specific to the fact that you have a polynomial of degree $9$. If you want to use a degree 2 polynomial instead, just change $n$ to $3$. – littleO Nov 5 '12 at 1:45
Ok i got that to, i guess had some extra code there, had to delete everything :) – ASROMA Nov 5 '12 at 1:45

MATLAB is a vector-based language (like the open-source Octave).In this case, variable x is a vector of length 10, which represents 10 coefficients of a polynomial. In your code, polynomial evaluation is given by Horner's method, however in MATLAB or Octave it can be much more simple by using polyval function:

plot(u, polyval(x(end:-1:1),u))

MATLAB (or Octave) interprets x as a polynomial of degree 9 (i.e., 10 coefficients). Here is your code on Octave.

share|cite|improve this answer

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.