Polyval/Polyfit input values
Show older comments
Hi, I'm new to MatLab and I've been attempting to use Polyval/Polyfit.
The code resembles something like this:
xmin=5;
xmax=100;
xrange = xmin:xmax;
p = [6 2 4 1 1];
polyval(p, xrange)
For some reason, the above results in a list of return values that are not in numerical order. In other words, the y values calculated using polyval are not all in the order that they should be in. Sporadically there are negative values outputted in the return between positive values.
For the function y=6x^4 + 2x^3 + 4x^2 + x + 1 where all my input values are positive, this theoretically should not be the case.
It is also interesting to note that if I instead insert xmin and xmax directly into the polyval parameters, the return values are in correct order.
Anyone ever run into a similar problem?
Thanks!
1 Comment
Are Mjaavatten
on 23 Apr 2016
When I enter your code into Matlab I get nice positive values that increase monotonically, as expected. Could it be that you have accidentally redefined polyval? Try
which polyval
This should result in something like
C:\Program Files\MATLAB\R2014b\toolbox\matlab\polyfun\polyval.m
If you get a very different answer, enter the command
clear polyval
to revert to the original behaviour.
Accepted Answer
More Answers (1)
Image Analyst
on 23 Apr 2016
Edited: Image Analyst
on 23 Apr 2016
This works fine:
xmin=5;
xmax=100;
xrange = xmin:xmax;
p = [6 2 4 1 1];
y=polyval(p, xrange)
plot(xrange, y, 'b*-')
grid on;
No negatives, and nothing out of order. Demos attached.
Categories
Find more on MATLAB in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!