Is this a logical Error in Matlab ? exp (pi*i*6) = 1.000000000000000 - 0.000000000000001i. WHY an i component ?, should just be 1

2 次查看(过去 30 天)
Is this a Logical Error in Matlab ?
exp (pi*i*6) = 1.000000000000000 - 0.000000000000001i.
WHY an i component ?, shouldnt the answer just be 1?
Please answer.
  1 个评论
Stephen23
Stephen23 2024-2-19
编辑:Stephen23 2024-2-19
"Is this a Logical Error in Matlab ?"
Probably not.
"WHY an i component ?, shouldnt the answer just be 1?"
The answer may be exactly one if you are doing algebraic/symbolic mathematics:
exp(sym(pi)*i*6)
ans = 
1
But you did not tell your computer to do symbolic mathematics, you gave it a numeric task using numeric operations. All numeric operations have precision limited by their data types, take finite speed, have finite domains of validity. And when working with binary floating point numbers they easily accumulate floating point error.
The only "logical error" here is assuming that your computer has infinite precision when doing numeric calculations.

请先登录,再进行评论。

采纳的回答

John D'Errico
John D'Errico 2024-2-19
编辑:John D'Errico 2024-2-19
Remember that pi in MATLAB is not truly pi, any more than pi is 3.14, or even 3.14159. pi is a transcendental number. There are infinitely many digits in pi.
format long
pi
ans =
3.141592653589793
And, yes, that looks vagely like pi. Many digits, but once we get past the first 16 digits or so, we need to recognize that pi there has only the first 16 digits correctly represented. We have an error in the least significant bits, as there must be in any finite representation of a transcendental number.
Double precision numbers are stored in a BINARY form. So they have 52 binary bits. In the case of pi, the binary form would look like
11.001001000011111101101010100010001000010110100011000
So there, the ones represent powers of 2. But the problem is, the true binary form for pi goes on forever, whereas in MATLAB, it stops at that point.
The result of all this is that pi as a double precision number is not truly pi. Close. But not truly pi, any more than pi==3.14. And if we try that simple aproximation for pi, we see the problem far more obviously.
exp(3.14*i*6)
ans =
0.999954342529211 - 0.009555776105247i
Of course you would not expect that result to be exact. But what you did was essentially the same thing.
If you want MATLAB to represent pi as the transcendental number pi, then you need to do something like this
exp(sym(pi)*i*6)
ans = 
1
where MATLAB now understands you want to use the actual number pi, instead of a floating point approximation to pi.
  5 个评论
Torsten
Torsten 2024-2-22
编辑:Torsten 2024-2-22
vpa(pi,150)
ans = 
3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214808651328230664709384460955058223172535940813
More about the choice of precision can be found here:

请先登录,再进行评论。

更多回答(2 个)

Steven Lord
Steven Lord 2024-2-19
πis a transcendental number. pi is the double precision approximation to that number. There are some functions in MATLAB that let you operate as though you were using πinstead of pi (like sinpi and cospi) but exp is not one of them.
So if you wanted to compute this without the roundoff error caused by using pi instead of π, you could use the identity .
format longg
y = [exp(6i*pi); ...
cos(6*pi)+1i*sin(6*pi); ...
cospi(6)+1i*sinpi(6)]
y =
1 - 7.34788079488412e-16i 1 - 7.34788079488412e-16i 1 + 0i

Sulaymon Eshkabilov
It is all about a display format:
format short
exp(pi*i*6)
ans = 1.0000 - 0.0000i
% compare with this:
cos(6*pi)+1i*sin(6*pi)
ans = 1.0000 - 0.0000i
% Compare this:
format long
[exp(pi*1i*6); cos(6*pi)+1i*sin(6*pi)]
ans =
1.000000000000000 - 0.000000000000001i 1.000000000000000 - 0.000000000000001i

类别

Help CenterFile Exchange 中查找有关 Logical 的更多信息

产品


版本

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by