How to do a 3D plot of this loop. I have tried the following.

2 次查看(过去 30 天)
clc;
clear all;
close all;
fc = 1*10^6; % Carrier frequency
c = 3*10^8; % Speed of light
WL = c / fc; % wavelength lamda
ko = (2*pi)/WL; % Wavenumber
N = 10; % Number of Electric fields created from IO's
a = 0 + (2-0)*rand(1,N); % amplitudes of Interacting Objects (IO's)
phi = randi([0 360],1,N); % angle of incidence (w.r.t the x-aaxis)
theta = randi([0 360],1,N); % angle of arrival
for n = 1 : N
for x = 1 : 5
for y = 1 : 5
E(x,y,:) = a.*exp(-1i*ko*(x.*cos(phi)+y.*sin(phi))).*exp(1i.*theta);
end
end
end
surf(x,y,real(E(x,y)))
And this is where i am getting the error:
Error using surf (line 82)
Z must be a matrix, not a scalar or vector.

回答(1 个)

Mahesh Taparia
Mahesh Taparia 2019-10-3
Hi,
As per your code, in the last line you are using surf command with (x,y) arguments. The variables x and y are taking the last value of the for loop, which is equal to 5. These arguments should be matrices instead of scalar value. You can modify your code as following:
p=1:5;
q=1:5;
surf(p,q,real(E(x,y)))
or,
[X,Y] = meshgrid(1:1:5,1:1:5);
surf(X,Y,real(E(x,y)))
You can refer the documentation of surf function in the below link for reference: https://www.mathworks.com/help/matlab/ref/surf.html#bvgp3nn-2

类别

Help CenterFile Exchange 中查找有关 Line Plots 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by