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

2 visualizaciones (últimos 30 días)
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 comentario
Stephen23
Stephen23 el 19 de Feb. de 2024
Editada: Stephen23 el 19 de Feb. de 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)
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.

Iniciar sesión para comentar.

Respuesta aceptada

John D'Errico
John D'Errico el 19 de Feb. de 2024
Editada: John D'Errico el 19 de Feb. de 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
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 comentarios
Torsten
Torsten el 22 de Feb. de 2024
Editada: Torsten el 22 de Feb. de 2024
vpa(pi,150)
ans = 
3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214808651328230664709384460955058223172535940813
More about the choice of precision can be found here:

Iniciar sesión para comentar.

Más respuestas (2)

Steven Lord
Steven Lord el 19 de Feb. de 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)]
y =
1 - 7.34788079488412e-16i 1 - 7.34788079488412e-16i 1 + 0i

Sulaymon Eshkabilov
Sulaymon Eshkabilov el 19 de Feb. de 2024
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

Categorías

Más información sobre Logical en Help Center y File Exchange.

Productos


Versión

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by