Borrar filtros
Borrar filtros

Why is 6*i not a complex number in my program?

7 visualizaciones (últimos 30 días)
Doug Hull
Doug Hull el 18 de En. de 2011
I though i was for imaginary in MATLAB.

Respuesta aceptada

Doug Hull
Doug Hull el 18 de En. de 2011
You may have used a variable called "i" earlier in your program or session, thus overwriting the imaginary constant i with your own number. In this case, MATLAB will use your new value for i instead of treating i as sqrt(-1). Five ways to ensure that you receive a complex result are:
  • Use the syntax 6i; MATLAB always interprets this as 6*sqrt(-1)
y = 6i;
  • Redefine i back to sqrt(-1)
i=sqrt(-1)
y = 6*i;
  • Clear your redefinition of i
clear i
y = 6*i;
  • Use j instead of i (assuming you haven't used a variable called "j" earlier in you program or session)
Note: these are very good reasons for not using i & j as indexes (in FOR loops, for example)
y = 6*j;
  • Use the COMPLEX function
y = complex(0, 6);
[From the MATLAB FAQ of Ancient Times]

Más respuestas (1)

Anish
Anish el 18 de En. de 2011
Looks like you might have a variable defined named "i" which is a real. If this is the case, then the "definition" of "i" as imaginary unit is overridden. For example:
i = 6;
disp(6*i)
Output:
36
See:

Categorías

Más información sobre Get Started with MATLAB en Help Center y File Exchange.

Etiquetas

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by