3-D plot in matlab
1 view (last 30 days)
Show older comments
Hey..I'm trying to plot the following code but i keep getting this error: (??? Error using ==> surf at 78 Z must be a matrix, not a scalar or vector.)
%% Defining givens:
f= 300e6; %% Chosen frequency
c= 3e8; %% Free space
lambda= c./f; %% Wavelenght
l_2=lambda./2;
k= (2.*pi)./l_2; %% Wave number
Io=5; % Chosen maximum value of the current
eighta=377; % Intrinsic impedance (free space)
r=l_2/(2.*pi); % Distance
h= 0:1:100; %% Variable height from the ground
L=1.25*l_2; % Dipole length
theta= 2*pi; %% Computing the requirements:
%% Electric field:
A= (k.*L.*Io.*exp(-1i.*r.*k))./(4.*pi.*r);
B= 2.*(cos(cos(theta).*k.*h));
E_F= eighta.*1i.*(A).*sin(theta).*(B);
[x,y,z]=sph2cart(h,theta-pi/2,E_F); % Converting to cartesian coordinates
% Generating 3D plot
surf(x,y,z) % Generating 3D plot
colormap(JET);
title ('Electric Field in 3D plot')
legend ('Heigth','Theta','Electric Field')
rotate3D on
axis image
Help!!
0 Comments
Accepted Answer
Azzi Abdelmalek
on 24 Nov 2013
Edited: Azzi Abdelmalek
on 24 Nov 2013
You can't use surf. Use
plot3(abs(x),abs(y),abs(z)) % Generating 3D plot
More Answers (1)
Wara Qasim
on 1 Oct 2019
hello please help me with this code i m trying to plot 3d figure of electric field but when i use plot3 i get error "Data cannot have more than 2 dimensions" and when i use quiver3 i get error that Z and U must be of same size. although sizes of all the matrices is same please help
clear all;
clc;
k = 9*10^9;
% Enter the Relative permittivity
eps_r = 1;
charge_order = 10^-9; % milli, micro, nano etc..
const = k*charge_order/eps_r;
x = 0:2:6;
y = 0:1:6;
z = 0:3:6;
[X,Y,Z]=meshgrid(x,y,z)
Y1=size(Y)
X1=size(X)
Z1=size(Z)
% Enter the dimensions
Nx=7;
Ny=4;
Nz=3;
% Enter the number of charges.
n = 2;
% Electric fields Initialization
E_f = zeros(Nx,Ny,Nz);
Ex = E_f;
Ey = E_f;
Ez = E_f;
% Vectors initialization
ex = E_f;
ey = E_f;
ez= E_f;
r = E_f;
r_square = E_f;
% Array of charges
Q = [2,-2];
% Array of locations
Z = [5,-5];
Y = [0,0];
X=[0,0];
%-------------------------------------------------------------------------%
% COMPUTATION OF ELECTRIC FIELDS
%-------------------------------------------------------------------------%
% Repeat for all the 'n' charges
for l = 1:n
q = Q(l);
% Compute the unit vectors
for i=1:Nx
for j=1:Ny
for k=1:Nz
r_square(i,j,k) = (i-51-X(l))^2+(j-51-Y(l))^2+(k-51-Z(l))^2;
r(i,j,k) = sqrt(r_square(i,j,k));
ex(i,j,k) = ex(i,j,k)+(i-51-X(l))./r(i,j,k);
ey(i,j,k) = ey(i,j,k)+(j-51-Y(l))./r(i,j,k);
ez(i,j,k) = ez(i,j,k)+(j-51-Z(l))./r(i,j,k);
end
end
end
E_f = E_f + q.*const./r_square;
Ex = Ez + E_f.*ex.*const;
Ey = Ez + E_f.*ey.*const;
Ez = Ez + E_f.*ez.*const;
end
e1=size(Ex)
e2=size(Ey)
e3=size(Ez)
plot3(abs(Ex),abs(Ey),abs(Ez))
%quiver3(X,Y,Z,Ex,Ey,Ez,'r');
%axis square;
0 Comments
See Also
Categories
Find more on Electrical Block Libraries in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!