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:
A = [1 2 3 4]; B = []; 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):
xs = 0:0.1:1; plot(xs,polyfit(B,xs));
Tada!
Post new comment