![0000 Screenshot.png](https://www.mathworks.com/matlabcentral/answers/uploaded_files/203696/0000%20Screenshot.png)
Plotting a function with 2 independent variables of different ranges?
18 次查看(过去 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);
![0000 Screenshot.png](https://www.mathworks.com/matlabcentral/answers/uploaded_files/203696/0000%20Screenshot.png)
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
![Plotting a function with 2 independent variables of different ranges - 2019 02 11.png](https://www.mathworks.com/matlabcentral/answers/uploaded_files/203830/Plotting%20a%20function%20with%202%20independent%20variables%20of%20different%20ranges%20-%202019%2002%2011.png)
Experiment to get the result you want.
EDIT —
Added plot image. Code unchanged.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 View and Analyze Simulation Results 的更多信息
产品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!