MATLAB Answers

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

31 views (last 30 days)
Rahul Gulia
Rahul Gulia on 30 Sep 2019
Answered: Mahesh Taparia on 3 Oct 2019
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.

Answers (1)

Mahesh Taparia
Mahesh Taparia on 3 Oct 2019
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

Tags

Community Treasure Hunt

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

Start Hunting!