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

2 Ansichten (letzte 30 Tage)
Rahul Gulia
Rahul Gulia am 30 Sep. 2019
Beantwortet: Mahesh Taparia am 3 Okt. 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.

Antworten (1)

Mahesh Taparia
Mahesh Taparia am 3 Okt. 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

Kategorien

Mehr zu Line Plots finden Sie in Help Center und File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by