Order of polynomial display: increasing order of powers?

A symbolic polynomial will be displayed in order of decreasing powers:
>> taylor(sin(x),x,'Order',11)
ans =
x^9/362880 - x^7/5040 + x^5/120 - x^3/6 + x
Is there a preference value I can set which will display such polynomials in increasing order of powers? Like this:
x - x^3/6 + x^5/120 - x^7/5040 + x^9/362880
Thank you!

4 件のコメント

Karan Gill
Karan Gill 2016 年 10 月 27 日
Why do you need this particular ordering? Details on what you are trying to do would be helpful.
Walter Roberson
Walter Roberson 2016 年 10 月 27 日
The order is important because it is a taylor expansion and the classic representation of a taylor expansion is in increasing orders of differentiation, which for sin() and a number of other important functions results in increasing powers of x.
The order of the terms should be in increasing n so that one can quickly see the effect of truncating at some lower point and so that one can quickly see the pattern in the coefficients.
HiWave
HiWave 2020 年 2 月 18 日
I find this extremely annoying as well. It is as if they anticipate no practical use of the output.
Bilal Akbana
Bilal Akbana 2021 年 4 月 29 日
Following command works and gives result as shown below;
syms x
sympref('PolynomialDisplayStyle','ascend');
sinTaylor = taylor(sin(x),x,'Order',11);
disp(sinTaylor)
x - x^3/6 + x^5/120 - x^7/5040 + x^9/362880

サインインしてコメントする。

 採用された回答

Walter Roberson
Walter Roberson 2016 年 10 月 26 日

1 投票

No, there is no preference for adjusting the order of expressions. The symbolic engine is permitted to return expressions in any order, and is permitted to change the order during different executions.
There might be a rule, but it is not easy to figure out what it is. For example,
>> y*k*x
ans =
k*x*y
>> k*x*k+x*y*x*x*x + y*x*k + k^2
ans =
k^2*x + k^2 + y*k*x + y*x^4
>> k*x*k+x*y*x*x*x + y*x*k + k^2*y
ans =
k^2*x + y*k^2 + y*k*x + y*x^4
The first of those suggests possibly alphabetical order, k coming before x coming before y. But the second of those has y*k*x -- why did it choose that in the larger expression? It has not ordered the expression by powers of x -- if it did then k^2 would have to be first or last. The third shows k^2*x and y*k^2 which has equal powers of k but sorts differently, non-alphabetically. But
>> x*k^2 + y*k^2
ans =
k^2*x + k^2*y
so sometimes it does put them in alphabetical order.
It is not sorting alphabetically. It is not sorting by total power.
But it is influenced by those things. For example if you replace k by the other single-letter names in k^2*x + k^2 + y*k*x + y*x^4 (excluding x and y) then the same order is used in each case, except for z:
y*x^4 + x*z^2 + y*x*z + z^2
When I was testing last week, I found cases where the order changed completely if a constant was added to the expression.

4 件のコメント

Alasdair McAndrew
Alasdair McAndrew 2016 年 10 月 26 日
My beef is that if you are using the live editor, with typeset output, then you should be able to expect that the output "looks nice"; in particular, that you have some control over the ordering of expressions. But it seems you don't.
Maybe this will be included in a future release...?
Walter Roberson
Walter Roberson 2016 年 10 月 26 日
The ordering cannot be controlled even in a mupad notebook at this time :(
Walter Roberson
Walter Roberson 2016 年 10 月 26 日
I understand why you want to do this... Unfortunately there just does not appear to be any way to control it.
By the way, Live Script calls pretty() to format the expression and pretty() does an undocumented call into the mupad engine to turn on pretty formatting and then calls into the engine to ask for the character representation of the expression (and then makes an undocumented call to turn off pretty formatting). The formatting is all done within the engine not at the MATLAB level.
Alasdair McAndrew
Alasdair McAndrew 2016 年 10 月 27 日
The crazy thing is that, as I pointed out above, you can create a polynomial, and then 'coeffs' will display the coefficients in increasing order of powers. But the polynomial itself will be displayed in decreasing order of powers. I find this very annoying.

サインインしてコメントする。

その他の回答 (1 件)

Robert
Robert 2016 年 10 月 26 日

0 投票

Hey there
So this is a bug in matlab I found the bug a few weeks ago and it should be fixed in the next release
It turns out with the addition of a tilda you can change the order in which the coeffs function returns values.
Try the following code and please don't forget to accept the answer so i can get the credit for helping. Thanks!
syms x
y = x - x^3/6 + x^5/120 - x^7/5040 + x^9/362880
[High_Order , ~] = coeffs(y) % Use this to get the higher order first
Coefficient_Low_first = coeffs(y)

4 件のコメント

Alasdair McAndrew
Alasdair McAndrew 2016 年 10 月 26 日
Well, it seems that higher order first is default. What I want is lower order first. However, 'coeffs' seems to output the coefficients in increasing order. That is, if
T = taylor(sin(x),x,'Order',10)
then
coeffs(T)
produces the coefficients in opposite order to their initial display in T. I want everything in increasing order.
Robert
Robert 2016 年 10 月 26 日
You obviously did not try my code I just showed you how to do exactly this. Adding the brackets and the tilda changes the order.
Its a bug in matlab that i have previously reported
The output is as follows. If you tried what i gave you your issue would be solved
>> T = taylor(sin(x),x,'Order',10);
>> coeffs(T)
ans =
[ 1, -1/6, 1/120, -1/5040, 1/362880]
>> [T,~]=coeffs(T)
T =
[ 1/362880, -1/5040, 1/120, -1/6, 1]
>>
Walter Roberson
Walter Roberson 2016 年 10 月 26 日
You can extract the coefficients in one order or the reverse but the user needs the symbolic expression itself to be displayed in a different order
Walter Roberson
Walter Roberson 2016 年 10 月 26 日
Consider for example sin() around the expression. coeffs() would not work because that would not be a multinomial, but you might still have an interest in the order that the expression displays.

サインインしてコメントする。

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by