Is this a logical Error in Matlab ? exp (pi*i*6) = 1.000000000000000 - 0.000000000000001i. WHY an i component ?, should just be 1
3 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Moses
am 19 Feb. 2024
Kommentiert: Moses
am 23 Feb. 2024
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 Kommentar
Stephen23
am 19 Feb. 2024
Bearbeitet: Stephen23
am 19 Feb. 2024
"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)
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.
Akzeptierte Antwort
John D'Errico
am 19 Feb. 2024
Bearbeitet: John D'Errico
am 19 Feb. 2024
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
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)
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)
where MATLAB now understands you want to use the actual number pi, instead of a floating point approximation to pi.
Weitere Antworten (2)
Steven Lord
am 19 Feb. 2024
π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)]
0 Kommentare
Sulaymon Eshkabilov
am 19 Feb. 2024
It is all about a display format:
format short
exp(pi*i*6)
% compare with this:
cos(6*pi)+1i*sin(6*pi)
% Compare this:
format long
[exp(pi*1i*6); cos(6*pi)+1i*sin(6*pi)]
0 Kommentare
Siehe auch
Kategorien
Mehr zu Logical finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!