Need help implementing a 2D circular gaussian
17 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
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:
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 个评论
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 NaNs 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!