MATLAB: Does the COEFFS function in the Symbolic Math Toolbox 7.0 (R14) return the coefficients of a symbolic polynomial in unexpected order

ascendingcoeffsMATLABorderseriessymbolictaylor

When I repetitively call the COEFFS function with the same inputs, I find that after a certain number of iterations, the order of the results change. This can be observed using the following code:
clear maplemex
syms x;
f = taylor(exp(x), 20);
for ii=1:150
t = coeffs(f);
%%%Method to identify incorrect order %%%
X = sort(t);[y i]=ismember(X,t);
if any(diff(i)>0)
disp(sprintf('Unsorted order after %d iterations',ii));
keyboard
end
end
For this example, I find the results are returned in a different order after 42 iterations.
Unsorted order after 42 iterations

Best Answer

The documentation for the COEFFS function does not specify that the results will be in any specific order. If you change the call to COEFFS from:
t = coeffs(taylor(f,tot))'
to
[t u]=coeffs(taylor(f,tot))'
You will see that the coefficients match with the order of polynomial.
You can post-process the output by finding out the order and sorting it to ascending order.
For example, if the output is as follows:
t =
[ 1, 1/362880, 1/3628800, 1/5040, 1/40320, 1, 1/2, 1/6, 1/24, 1/120, 1/720]
u =
[ 1, x^9, x^10, x^7, x^8, x, x^2, x^3, x^4, x^5, x^6]
Add these lines of code to find the order:
syms x positive;
order = log(u)/log(x)
Then you can rearrange "t" according to the values returned by "order".