Need help implementing a 2D circular gaussian
18 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Luis Miguel López Santamaría
le 19 Oct 2019
Modifié(e) : Star Strider
le 19 Oct 2019
I have the next equation which represents a circular gaussian. I have a little trouble because when I plot this equation gives me a normal gaussian. I can't see any circular shape. I have the next code:
x = linspace(-3, 3);
y = x;
R = {3, 2, 1};
for i=1:length(R)
f = exp(-((x/R{i}).^2 + (y/R{i}).^2));
hold on;
grid on;
plot3(x, y, f);
end
This code produce me the next plot:
0 commentaires
Réponse acceptée
Star Strider
le 19 Oct 2019
Modifié(e) : Star Strider
le 19 Oct 2019
If you want to plot a surface, you need to use matrix arguments.
Try this:
x = linspace(-3, 3);
y = x;
[X,Y] = ndgrid(x,y);
R = {3, 2, 1};
for i=1:length(R)
f = exp(-((X/R{i}).^2 + (Y/R{i}).^2));
hold on;
grid on;
mesh(X, Y, f);
end
view(-30,30)
If you first define your function in polar coordinates, you can then use the pol2cart function to convert them to Cartesian coordinates. The plot should have a circular shape.
EDIT —
For example:
r = linspace(-3, 3);
th = linspace(0, 2*pi, 90);
[R,T] = ndgrid(r,th);
Z = exp(-R.^2);
[X,Y,Z] = pol2cart(T, R, Z);
figure
surf(X, Y, Z)
shading('interp')
grid on
produces:
Note that I defined the original matrices as ‘r’ (radius) and ‘th’ (angle), then converted them to Cartesian and plotted them. This loses the angle information in the plot, so you need to create them yourself:
r = linspace(-3, 3);
th = linspace(0, 2*pi, 90);
[R,T] = ndgrid(r,th);
Z = exp(-R.^2);
[X,Y,Z] = pol2cart(T, R, Z);
thg = linspace(0, 2*pi, 12);
polgrid = [5*cos(thg); 5*sin(thg)];
figure
surf(X, Y, Z)
hold on
plot3([zeros(size(thg)); polgrid(1,:)], [zeros(size(thg)); polgrid(2,:)], zeros(2, size(thg,2))-0.1, '-k')
hold off
shading('interp')
Ax = gca;
Ax.XAxis.Color = 'none';
Ax.YAxis.Color = 'none';
Ax.XGrid = 'off';
Ax.YGrid = 'off';
grid on
0 commentaires
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur NaNs dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!