How do I plot an equation with an imaginary quantity?

5 visualizaciones (últimos 30 días)
Abigail Chaffins
Abigail Chaffins el 4 de Sept. de 2019
Editada: John D'Errico el 4 de Sept. de 2019
Current code:
t=[0:0.01:10]
y=exp(-5*i*t)
plot(t,y)
When I run this script, I get an error message "Warning: Imaginary parts of complex X and/or Y arguments ignored"
How do I account for the imaginary portion of the equation in the plot?

Respuestas (3)

Star Strider
Star Strider el 4 de Sept. de 2019
Try this:
t=[0:0.01:10];
y=exp(-5*1i*t);
figure
plot(t,real(y), t,imag(y))
legend('Re','Im')
You need to plot them separately, so they both appear to be real values to the plot function.

Matt J
Matt J el 4 de Sept. de 2019
Editada: Matt J el 4 de Sept. de 2019
If you mean that you want the values plotted in the complex plane, then
t=[0:0.01:10];
y=exp(-5*i*t);
scatter(real(y),imag(y))
xlabel 'Real', ylabel 'Imag'
untitled.png

John D'Errico
John D'Errico el 4 de Sept. de 2019
Editada: John D'Errico el 4 de Sept. de 2019
Look at what you have.
t=[0:0.01:10];
y=exp(-5*i*t);
y(1:5)
ans =
1 + 0i 0.99875 - 0.049979i 0.995 - 0.099833i 0.98877 - 0.14944i 0.98007 - 0.19867i
The first 5 elements are sufficient to get the idea across. You should appreciate that a complex number is actually TWO numbers, rolled up into one. That is, a real part, and an imaginary part.
So what does it mean when you write
plot(t,y)
When do you think you should see on the vertical axis? A plot axis can show only ONE value. Not two. So plot is confused. The decision made is to plot the real part of a complex number on the veritcal axis there, and throw away the imaginary part for the plot. It even tells you that, via a warning. Yes, there might be lots of things it might do. But plot is a two dimensional thing. Remember that a plot creates a picture, and a picture lives in a two dimensional plane.
What can you do? Well, you can extract the real part, or the imaginary part, using the functions real and imag. Now you can do whatever you want, as you have complete control. But only you know what you might want to do. For example, you might decide to use plot3, where now you have 3 axes, one for t, one for real(y), and one for imag(y). Or, you might decide to forget about t, and just plot the real and imaginary parts of y on one plot. This is a commmon solution. Or, something else.
Your choice. Your decision to make.

Community Treasure Hunt

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

Start Hunting!

Translated by