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.)I tried using mesh i got the same error and whe i tried using plot3 it only resulted in straight line plot and not 3D..Help !!
%%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

 Accepted Answer

Walter Roberson
Walter Roberson on 2 Dec 2013

0 votes

You are feeding the vector h, the scalar theta-pi/2, and the vector E_F into sph2cart(). The result you are going to get out for z is therefore going to be a vector, rather than a 2D matrix that surf requires.
Are you thinking that your code is providing a full regular grid of coordinates whose surface is to be represented, are are you thinking that your code is providing a set of scattered coordinates and you want a rectangular surface estimated to fit those scattered points? If you want the latter, you need to do the extrapolation using some other calls before you can surf()

5 Comments

A A
A A on 2 Dec 2013
what I'm thinking is that to produce a 3D plot for the electric field of an antenna suspended from a height (h) of the ground. Based on your answer, what do you suggest I should do??
The antenna is suspended at constant height h, but the electric field is going to surround the antenna and so must be sampled at a variety of heights.
It is not clear to me that you want a 3D plot. It sounds to me as if you would like to be able to go to each (x, y, z) point near-ish to the antenna, and know what the electric field is at that point. That would require a 4D plot, not a 3D plot. Or it would require slicing the 4D plot at intervals to produce a 3D plot; see isosurface() and slice()
A A
A A on 2 Dec 2013
ahaaa ok ok.. I didnt notice that, sry but im kind of a beginner in matlab so can you plz clarify more how to use these commands?
There should be examples in the documentation. It would be easier if you looked at those and then asked more specific questions.
I added the isosurface but now i got a new error: ??? Error using ==> sph2cart.Too many output arguments.
:( :(
% if true
% code
%end
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,v] =sph2cart(h,theta,E_F) ;
p = patch(isosurface(x,y,z,v,-3));
isonormals(x,y,z,v,p)
%plot3(x,y,z) % Generating 3D plot
colormap(JET);
title ('Electric Field in 3D plot')
legend ('Heigth','Theta','Electric Field')
rotate3D on
axis image

Sign in to comment.

More Answers (0)

Tags

Asked:

A A
on 2 Dec 2013

Commented:

A A
on 2 Dec 2013

Community Treasure Hunt

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

Start Hunting!