# Numden on complex equation returns unexpected result

3 ビュー (過去 30 日間)
YT 2019 年 10 月 21 日
コメント済み: YT 2019 年 10 月 31 日
I have the following complex symbolic equation
syms F_c M B w K;
eq = F_c/(- M*w^2 + B*w*1i + K);
Now I just want to get the numerator and denominator, so I thought using numden should do the trick
[n,d] = numden(eq);
% Numden should return this
> n = F_c;
> d = - M*w^2 + B*w*1i + K
> n = -F_c*1i;
> d = M*w^2*1i + B*w - K*1i
Seems like numden rewrote the equation for an unknown reason.
In this case, where's there's only one division sign, I know I could "fix" it like this
str2sym(strsplit(char(eq),'/'));
> [ F_c, - M*w^2 + B*w*1i + K]
But still the question remains, why does numden act like this? And is there a nicer way to seperate numerator and denominator?

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

### 採用された回答

Walter Roberson 2019 年 10 月 31 日
The order of terms for division by an expression cannot be controlled. The order is not documented, and is difficult to deduce, and is fragile in the sense that minor changes to a low order term can trigger a complete reordering. There are cases where simple looking expressions not much different from A/(B+C) end up being represented as (-1*A) / (-B-C) and there is nothing you can do about it.
Devineni's work-around might work for that one expression, but there are others it will fail for.
If you need numerator and denominator in some normalized form then you are going to need to normalize yourself according to your preferences of what proper order should be. But remember that if you have A*1i+B and divide by 1i in order to normalize to leading real term, then there is the risk that instead of getting A-B*1i that you will get -1i*B+A. The Internal ordering rules do not "play fair" sometimes.

#### 1 件のコメント

YT 2019 年 10 月 31 日
Thanks for the clarification.

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

### その他の回答 (1 件)

Devineni Aslesha 2019 年 10 月 31 日
The given input to numden function is a symbolic expression. However, as it involves a complex number, the numden function handles it in different way which leads to normal form.
Use the below code to get the expected result.
[n, d] = numden(subs(eq, 1i, sym('II')));
n = subs(n, sym('II'), 1i);
d = subs(d, sym('II'), 1i);
Here, in place of sym(‘II’), other symbols can also be used.

#### 1 件のコメント

YT 2019 年 10 月 31 日
Well this is just another work-around, which is fine I guess, but I accepted @Walter's answer because of the somewhat more detailed explanation.

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

R2018a

### Community Treasure Hunt

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

Start Hunting!