Plotting a function with 2 independent variables of different ranges?

32 views (last 30 days)
Hi all,
I'm trying to plot a function that takes 2 independent variables of different ranges. The 2 variables are theta and phi, as follows:
-pi < theta < pi
0 < phi < pi
The function that I'm trying to plot is:
| sin(2 * pi * sin(theta) * sin(phi)) / sin((pi / 2) * sin(theta) * sin(phi)) |^2
So my initial attempt at this is treating the numerator and the denominator as 2 different functions:
theta = -pi : pi/8 : pi;
phi = 0 : pi/8 : pi;
a = sin(2 * pi * sin(theta) * sin(phi))
b = sin((pi / 2) * sin(theta) * sin(phi))
But I realize that trying to multiply the sin(theta) and sin(phi) won't work since my 2 variables are of different sizes.
So would I have to use a loop (for loop or while loop) to accomplish this?
I'm honestly a newbie Matlab programmer, so I do appreciate any help that is given to me. My main goal is to plot the function to get theta max, phi max, null locations, and local maxima. I think that once I figure out just how to correctly plot this, the rest should follow simply enough...

Answers (2)

Image Analyst
Image Analyst on 12 Feb 2019
I think you might have to use meshgrid and create an image, where phi varies along one dimension, and theta varies along the other direction. Then use image(), imshow(), or surf() to display the function.
numPoints = 100;
% theta = -pi : pi/8 : pi;
% phi = 0 : pi/8 : pi;
theta = linspace(-pi, pi, numPoints);
phi = linspace(0, pi, numPoints);
[X, Y] = meshgrid(theta, phi);
% |sin(2 * pi * sin(theta) * sin(phi)) / sin((pi / 2) * sin(theta) * sin(phi)) |^2
numerator = sin(2 * pi * sin(X) .* sin(Y))
denominator = sin((pi / 2) .* sin(X) .* sin(Y))
z = abs(numerator ./ denominator).^2;
surf(theta, phi, z);
xlabel('theta', 'FontSize', 18);
ylabel('phi', 'FontSize', 18);
0000 Screenshot.png

Star Strider
Star Strider on 12 Feb 2019
Edited: Star Strider on 12 Feb 2019
One option is to calculate ‘a’ and ‘b’ as matrices (by transposing one vector to a column, tne multiplying):
theta = -pi : pi/8 : pi;
phi = 0 : pi/8 : pi;
a = sin(2 * pi * sin(theta)' * sin(phi));
b = sin((pi / 2) * sin(theta)' * sin(phi));
h = a ./ b;
figure
plot(theta, h)
grid
I am not certain that is the result you want. However, that is the only one that will work with your different-length vectors. unless you want to plot it as a surface.
That would be:
figure
surfc(phi, theta, h)
grid on
set(gca, 'XTick',[0,pi/2,pi],'XTickLabel',{'0', '\pi/2', '\pi'}, 'YTick',[-pi,0,pi],'YTickLabel',{'-\pi','0','\pi'}, 'Interpreter','latex')
xlabel('\bf\phi\rm')
ylabel('\bf\theta\rm')
axis tight
Plotting a function with 2 independent variables of different ranges - 2019 02 11.png
Experiment to get the result you want.
EDIT —
Added plot image. Code unchanged.

Categories

Find more on MATLAB in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!