why my code gives different result then the formula

3 visualizaciones (últimos 30 días)
fima v
fima v el 17 de Dic. de 2021
Comentada: Star Strider el 17 de Dic. de 2021
Hello,I have triedto implent the formula shown bellow in matlab to create the plot shown bellow.
but when i implemented the formula using the code bellow i get a tottaly different plot then the theory as shown bellow.
Where did i go wrong?
Thanks.
[theta,phi] = meshgrid(-pi:0.2:0.5*pi,-pi:0.2:pi);
f=10;
lambda=300/f;
k=(2*pi)/lambda;
N=10;
M=10;
dx=0.5*lambda;
dy=0.5*lambda;
ksi_x=k*dx.*sin(theta).*cos(phi);
ksi_y=k*dy.*sin(theta).*sin(phi);
AF=((1/M)*((sin(0.5*M*ksi_x))./(sin(0.5*ksi_x)))).*((1/N)*((sin(0.5*N*ksi_y))./(sin(0.5*ksi_y))));
surf(phi,theta,AF)
colorbar
%polarplot(theta,AF)
%delta=-k*d*cos(theta_0);
%AF=(1/N)*((sin(0.5*N.*ksi))./(sin(0.5*ksi)));

Respuestas (1)

Star Strider
Star Strider el 17 de Dic. de 2021
Nothing wrong, just that to get it to appear in polar coordinates, the surface data need to be processed first with the pol2cart function, then plotted. It looks as though it still needs a bit of effort to get it to look like the first two images, however this should help get the correct plot.
It will ikely be necessary to experiment with the order of the first two arguments to pol2cart since I have no idea what the correct order is in this instance. Another option could be sph2cart and since I am not certain wha the objective is here, consider it as well.
[theta,phi] = meshgrid(-pi:0.2:0.5*pi,-pi:0.2:pi);
f=10;
lambda=300/f;
k=(2*pi)/lambda;
N=10;
M=10;
dx=0.5*lambda;
dy=0.5*lambda;
ksi_x=k*dx.*sin(theta).*cos(phi);
ksi_y=k*dy.*sin(theta).*sin(phi);
AF=((1/M)*((sin(0.5*M*ksi_x))./(sin(0.5*ksi_x)))).*((1/N)*((sin(0.5*N*ksi_y))./(sin(0.5*ksi_y))));
[X,Y,Z] = pol2cart(phi, theta, AF); % Polar Coordinate Transformation
figure
surf(phi,theta,AF)
colorbar
figure
surf(X, Y, Z) % Polar Coordinate Plot
colorbar
%polarplot(theta,AF)
%delta=-k*d*cos(theta_0);
%AF=(1/N)*((sin(0.5*N.*ksi))./(sin(0.5*ksi)));
.
  2 comentarios
fima v
fima v el 17 de Dic. de 2021
my axes were theta and phi,they are already polar.
why do we need to do this converstion?
(your solution is correct)
Star Strider
Star Strider el 17 de Dic. de 2021
Thank you!
The variables were polar, however they were plotted as though they were Cartesian, because the surf function interprets them that way. In order for them to correctly appear polar, they need to be transformed from polar to Cartesian, so that MATLAB’s Cartesian coordinate system will plot them correctly.
Also, limting the ‘x’ and ‘y’ axes in the polar plot, and increasing the resolution (by decreasing the step size in the original vectors) will produce a plot that more closely resembles the plots in the second posted image.—
[theta,phi] = meshgrid(-pi:0.01:0.5*pi,-pi:0.01:pi);
f=10;
lambda=300/f;
k=(2*pi)/lambda;
N=10;
M=10;
dx=0.5*lambda;
dy=0.5*lambda;
ksi_x=k*dx.*sin(theta).*cos(phi);
ksi_y=k*dy.*sin(theta).*sin(phi);
AF=((1/M)*((sin(0.5*M*ksi_x))./(sin(0.5*ksi_x)))).*((1/N)*((sin(0.5*N*ksi_y))./(sin(0.5*ksi_y))));
[X,Y,Z] = pol2cart(phi, theta, AF); % Polar Coordinate Transformation
figure
surf(phi,theta,AF)
colormap(turbo) % New In R2021a
colorbar
shading('interp')
figure
surf(X, Y, Z) % Polar Coordinate Plot
colormap(turbo) % New In R2021a
colorbar
shading('interp')
xlim([-1 1]*0.75) % Axis Limits
ylim([-1 1]*0.75) % Axis Limits
Experiment to get the desired result.
.

Iniciar sesión para comentar.

Categorías

Más información sobre 2-D and 3-D Plots en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by