My plot for directivity is not showing?
3 views (last 30 days)
Show older comments
Andrew Yip
on 26 Feb 2019
Commented: Andrew Yip
on 27 Feb 2019
Hi guys,
I'm trying to make a plot of directivity for a given radiation intensity pattern (W/sr). However, for some reason my plot for directivity is not showing when I'm using the plot command. This is the code I have.
% Setting up variables theta and phi.
% phi is a given scalar. theta is an array from -pi to pi.
theta = -pi : pi/512 : pi;
phi = pi/2;
% function Pd
a = sin(2 * pi * sin(theta) * sin(phi)) ./ sin((pi/2) * sin(theta) * sin(phi));
pd = (abs(a)).^2;
% Calculating Prad to obtain directivity.
x = 0 : pi/1024 : pi;
fun = @(x) (abs(sin(2 * pi .* sin(x) * sin(pi/2)) ./ sin((pi/2) .* sin(x) * sin(pi/2)))).^2
q0 = integral(fun, 0, pi);
Prad = 2 * pi * q0;
% Calculating directivity. D = 4 * pi * U / Prad. In this example, U = pd
% (W / sr)
D = 4 * pi .* pd / Prad;
figure(1)
plot(theta,D)
xlabel(theta)
ylabel(D)
title('Directivity versus theta')
My figure 1 does not show a plot of D versus theta where I am expecting it... can anyone let me know or guide me as to why that is? Is it because my variable D (for directivity) is not an array as I think it is? D shows as a 1x1025 double when I run this code.![Directivity Plot Issue.jpg](https://www.mathworks.com/matlabcentral/answers/uploaded_files/205890/Directivity%20Plot%20Issue.jpeg)
![Directivity Plot Issue.jpg](https://www.mathworks.com/matlabcentral/answers/uploaded_files/205890/Directivity%20Plot%20Issue.jpeg)
Even strangely, I was able to plot pd versus theta using the code below without any issues, in the same .m script file:
figure(1)
plot(theta,pd)
xlabel('theta')
ylabel('pd(theta, phi)')
title('pd versus theta')
So I'm confused as to why I can plot pd but not D... I appreciate any help or feedback on this. Thank you for your time!
UPDATE: My plot still does not show correctly when I run my code above (it only shows the title). But, when I right-click my theta and D variables on the workspace, and then use the Plot tab to plot, it does show a plot...![theta versus D.jpg](https://www.mathworks.com/matlabcentral/answers/uploaded_files/205891/theta%20versus%20D.jpeg)
![theta versus D.jpg](https://www.mathworks.com/matlabcentral/answers/uploaded_files/205891/theta%20versus%20D.jpeg)
So now I'm even more confused as to why my code doesn't show a plot when I run the script, but I can right-click my variables and get a plot through the plot tab...
0 Comments
Accepted Answer
Yasasvi Harish Kumar
on 26 Feb 2019
Edited: Yasasvi Harish Kumar
on 26 Feb 2019
Hi,
Your xlabel and ylabel need to be in quotes which is missing in your script.
% Setting up variables theta and phi.
% phi is a given scalar. theta is an array from -pi to pi.
theta = -pi : pi/512 : pi;
phi = pi/2;
% function Pd
a = sin(2 * pi * sin(theta) * sin(phi)) ./ sin((pi/2) * sin(theta) * sin(phi));
pd = (abs(a)).^2;
% Calculating Prad to obtain directivity.
x = 0 : pi/1024 : pi;
fun = @(x) (abs(sin(2 * pi .* sin(x) * sin(pi/2)) ./ sin((pi/2) .* sin(x) * sin(pi/2)))).^2
q0 = integral(fun, 0, pi);
Prad = 2 * pi * q0;
% Calculating directivity. D = 4 * pi * U / Prad. In this example, U = pd
% (W / sr)
D = 4 * pi .* pd / Prad;
figure(1)
plot(theta,D)
xlabel('theta')
ylabel('D')
title('Directivity versus theta')
This fixes your problem.
Regards
More Answers (0)
See Also
Categories
Find more on Annotations 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!