MATLAB Answers


plot a function consisting of real and imaginary numbers

Asked by J K
on 27 Jul 2019
Latest activity Commented on by Rena Berman on 19 Sep 2019
Hi guys,
I have this code and upon execution Matlab says that Imaginary parts of complex X and/or Y arguments were ignored. How can I plot the function without ignoring imaginary parts? I tried "plot(t, real(squeeze(shifted(:,61,61))),imag(squeeze(shifted(:,61,61))), '.-');" but Matlab said "Data must be a single matrix Y or a list of pairs X,Y" Tried a few other things but they did not work either.
Here is the full code before any modifications and attempts.
c = 0.3; % um/fs
z = 5;
w_o = 2*pi*c;
w = -6*w_o:0.1:6*w_o;
dw = w(2)-w(1);
kx = -6:0.1:6;
ky = kx;
dky = ky(2)-ky(1);
delta =pi;
deltaW = 3*pi;
deltaK = 1;
nw = length(w);
dt = pi/w(end);
t =[(-nw/2+1:nw/2)]*dt;
nkx = length(kx);
dx = pi/kx(end);
x =[(-nkx/2+1:nkx/2)]*dx;
nky = length(ky);
dy = pi/ky(end);
y = [(-nky/2+1:nky/2)]*dy;
[Kx, W, Ky] = meshgrid(kx, w, ky);
[X, T, Y] = meshgrid(x, t, y);
input = exp(-((W-w_o).^2)/deltaW.^2).*exp(-(Kx.^2+Ky.^2)/(deltaK.^2)).*exp(1*i.*sqrt((W/c).^2-(Kx.^2+Ky.^2)).*z(j));
fourier = fftn(fftshift(input));
shifted = fftshift(fourier);
plot(t, squeeze(shifted(:,61,61)), '.-');


Note that you have
input = exp(-((W-w_o).^2)/deltaW.^2).*exp(-(Kx.^2+Ky.^2)/(deltaK.^2)).*exp(1*i.*sqrt((W/c).^2-(Kx.^2+Ky.^2)).*z(j));
which involves z(j) but that j has not been assigned in your code. When you do not assign to j then j is interpreted as sqrt(-1) which is not a valid subscript.
(Answers Dev) Restored edit

Sign in to comment.

1 Answer

Answer by Walter Roberson
on 27 Jul 2019

plot(t, real(shifted(:, 61, 61)), '.-b', t, imag(shifted(:, 61, 61)), '.-r')
Where does your code define shifted?

  1 Comment

For fft of real values it is common for the real and imaginary components to effectively end up with different scales. In particular, the first output slot is sum() of the input and that will be real valued for real input. If the input has a nonzero mean, that first output slot can end up fairly large compared to the other outputs.

Sign in to comment.