number format inconsistency between '"format shortE" and "format default"

2 ビュー (過去 30 日間)
sxh
sxh 2024 年 10 月 15 日
コメント済み: Stephen23 2024 年 10 月 15 日
Why does using "format shortE" gives incorrect value for X? X should just be a identity
lambda= [3*1i 0;0 3*1i]
lambda =
0.0000 + 3.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 0.0000 + 3.0000i
K = diag(2*pi*lambda)
K =
0.0000 +18.8496i 0.0000 +18.8496i
L = exp(K)
L =
1.0000 - 0.0000i 1.0000 - 0.0000i
X = diag(L)
X =
1.0000 - 0.0000i 0.0000 + 0.0000i 0.0000 + 0.0000i 1.0000 - 0.0000i
format shortE
lambda= [3*1i 0;0 3*1i]
lambda =
0.0000e+00 + 3.0000e+00i 0.0000e+00 + 0.0000e+00i 0.0000e+00 + 0.0000e+00i 0.0000e+00 + 3.0000e+00i
K = diag(2*pi*lambda)
K =
0.0000e+00 + 1.8850e+01i 0.0000e+00 + 1.8850e+01i
L = exp(K)
L =
1.0000e+00 - 7.3479e-16i 1.0000e+00 - 7.3479e-16i
X = diag(L)
X =
1.0000e+00 - 7.3479e-16i 0.0000e+00 + 0.0000e+00i 0.0000e+00 + 0.0000e+00i 1.0000e+00 - 7.3479e-16i
  2 件のコメント
Stephen23
Stephen23 2024 年 10 月 15 日
編集済み: Stephen23 2024 年 10 月 15 日
"X should just be a identity"
I would not necessarily expect that result when performing numeric calculations. Numeric calculations are definitely not symbolic/algebraic mathematics.
"Why does using "format shortE" gives incorrect value for X?"
This has nothing to do with the FORMAT, the values are most likely not "incorrect". What is much more likely is that you have not taken into account the behaviors of binary floating point mathematics, in particular the accumulation of floating point error during e.g. the EXP operation.
lambda = [3*1i 0;0 3*1i];
K = diag(2*pi*lambda);
L = exp(K);
X = diag(L);
real(X)
ans = 2×2
1 0 0 1
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
imag(X) % not zero
ans = 2×2
1.0e-15 * -0.7348 0 0 -0.7348
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
Using (slower) symbolic mathematics:
K = diag(2*sym(pi)*lambda);
L = exp(K);
X = diag(L)
Stephen23
Stephen23 2024 年 10 月 15 日
"Every operation will accumulate some floating point error which is why decimal point precision needs to be reduced in each step according to the order of error in each step."
Variable precision is not a requirement of IEEE 754: https://en.wikipedia.org/wiki/IEEE_754
"So I guess the question becomes is how to tell MATLAB to take the order of error into account to give the end result with respect to a given precision of PI or EXP."
lambda = [3*1i 0;0 3*1i];
K = diag(2*pi*lambda);
L = round(exp(K),9);
X = diag(L)
X = 2×2
1 0 0 1
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>

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

回答 (0 件)

カテゴリ

Help Center および File ExchangeElementary Math についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by