# Bug on polyfit output?

14 views (last 30 days)
DrZoidberg on 8 Jun 2018
Edited: dpb on 13 Jun 2018
Hi, I am wondering why the results change when I call polyfit with the tilde ('~'), in order to obviously surpress the remaining outputs:
>> p = polyfit([1 2 3 5 10], [5 65 84 2 3],1)
p =
-4.7402 51.7087
BUT
>> [p,~,~] = polyfit([1 2 3 5 10], [5 65 84 2 3],1)
p =
-16.8925 31.8000
I thought in both cases p should contain the same coefficients. Does anybody know why there is a difference? The ~ method works fine with other functions for example like size:
>> [p,~] = size([11 11; 11 11;11 11])
p =
3

dpb on 8 Jun 2018
Edited: dpb on 8 Jun 2018
"Feature" or "Quality of Implementation" depending on your viewpoint...
help polyfit
...
[p,S,mu] = polyfit(x,y,n) also returns mu, which is a two-element vector with centering
and scaling values.
mu(1) is mean(x), and mu(2) is std(x). Using these values, polyfit centers x at zero
and scales it to have unit standard deviation
...
I'd never tried it before with the tilde as the third output argument so wasn't aware it (the tilde, that is) was being counted as if the argument were there, but clearly it is.
>> x=[1 2 3 5 10];
>> [mean(x) std(x)]
ans =
4.2000 3.5637
>> [p,~,mu] = polyfit([1 2 3 5 10], [5 65 84 2 3],1)
p =
-16.8925 31.8000
mu =
4.2000
3.5637
>>
It comes from ancient history of how polyfit was initially implemented; truthfully to have the output variable determine whether the independent variable is/is not scaled is/was a less-than-optimal design and almost certainly wouldn't have made the cut under today's ideas of software design/interface. But, 30 years ago or so when first implemented ideas were far different than are today.
ADDENDUM: However, what's the purpose of using the tilde for trailing return value position holders that you don't want, anyway? Any number of output variables beyond those provided for are automagically dropped; the only purpose/need for the tilde is to not return one (or more) arguments that are positioned prior to one that is desired.
Of course, here's a case because of the unusual input design that the output is dependent upon the number of inputs that if you want the scaling you have to provide the output argument.
I've found it somewhat surprising that TMW hasn't introduced a more capable and modern version into base product rather than restricting only to the toolboxes (which I find somewhat cumbersome albeit more flexible).

dpb on 10 Jun 2018
Ah! A reason for using tilde I hadn't thought of...some sense to that altho I suspect you'll grow tired of it with experience. :)
Speaking of which and the previous discussion, anybody recall when ~ was introduced--it's a relatively new feature altho I don't recall just when and was too lazy to go try to look it up. With the newer machine now I don't have the older releases still installed earlier than R2014b.
The precis for polyfit is accurate; other than the inconsistency in model based on whether the output argument is/isn't given (to normalize should be a user input imo even granted that one somewhere needs the normalizing statistics returned), my complaint extends beyond polyfit to polyval that then requires the user to do the explicit standardization instead of optional input.
As noted, all in all the pair really should be updated/modernized but without all the excess baggage of the objects in the toolboxen so they're still "lean 'n mean" and easy to interface which is all that is wanted/needed more often than not.
Jan on 10 Jun 2018
The ~ was introduced in R2009b.
Guillaume on 10 Jun 2018
I used the tilde for function calls that have more than one output, just to remember it has more outputs than the requested ones
For some functions this is not a good idea and will slow down your code. If you don't request the output at all, some functions will not go through the process of calculating the extra outputs. By using ~ you force the function to calculate the output, which you then discard.