plot a function consisting of real and imaginary numbers

조회 수: 3 (최근 30일)
J K
J K 2019년 7월 27일
댓글: Rena Berman 2019년 9월 19일
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;
dkx=kx(2)-kx(1);
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);
figure(6);
plot(t, squeeze(shifted(:,61,61)), '.-');
  댓글 수: 2
Walter Roberson
Walter Roberson 2019년 7월 28일
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.
Rena Berman
Rena Berman 2019년 9월 19일
(Answers Dev) Restored edit

댓글을 달려면 로그인하십시오.

답변 (1개)

Walter Roberson
Walter Roberson 2019년 7월 27일
plot(t, real(shifted(:, 61, 61)), '.-b', t, imag(shifted(:, 61, 61)), '.-r')
Where does your code define shifted?
  댓글 수: 1
Walter Roberson
Walter Roberson 2019년 7월 27일
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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Annotations에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by