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

조회 수: 1 (최근 30일)
Rahul Gulia
Rahul Gulia 2019년 9월 30일
답변: Mahesh Taparia 2019년 10월 3일
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에서 Graphics Performance에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by