Plotting a function with 2 independent variables of different ranges?
조회 수: 35 (최근 30일)
이전 댓글 표시
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...
댓글 수: 0
답변 (2개)
Image Analyst
2019년 2월 12일
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);
댓글 수: 0
Star Strider
2019년 2월 12일
편집: Star Strider
2019년 2월 12일
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
Experiment to get the result you want.
EDIT —
Added plot image. Code unchanged.
댓글 수: 0
참고 항목
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!