Need help implementing a 2D circular gaussian

조회 수: 22 (최근 30일)
Luis Miguel López Santamaría
Luis Miguel López Santamaría 2019년 10월 19일
편집: Star Strider 2019년 10월 19일
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:

채택된 답변

Star Strider
Star Strider 2019년 10월 19일
편집: Star Strider 2019년 10월 19일
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:
Need help implementing a 2D circular gaussian - 2019 10 19.png
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개)

카테고리

Help CenterFile Exchange에서 NaNs에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by