Polyfitting and Vector Reversal in Matlab

I’m currently taking a course in data mining and matrix methods, and we use matlabs for our lab assignments. We have probably spent half of our time figuring out how to do certain things in matlab, and the other half actually learning what we’re supposed to learn.

One problem we had was that we had a vector of coefficients for a polynomial, and we wanted to plot a graph of it, this gave us a few headaches.

First of all, how do you plot a polynomial? polyfit does that, as we figured out, and then one can use plot to plot the points given.

This led to a subproblem, we had our coefficients in the opposite order from what polyfit expects, how do you reverse a vector? Perhaps there is a built-in but we ended up doing the following to put A reversed into B:

  1. A = [1 2 3 4];
  2. B = [];
  3. B(length(A):-1:1) = A(:);

Second subproblem was to realize that you had to plot the same points as you polyfitted (well, that should have been fairly obvious I guess). So (note the different argument orders, ugh):

  1. xs = 0:0.1:1;
  2. plot(xs,polyfit(B,xs));

Tada!

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>. Beside the tag style "<foo>" it is also possible to use "[foo]".

More information about formatting options