![Need help implementing a 2D circular gaussian - 2019 10 19.png](https://www.mathworks.com/matlabcentral/answers/uploaded_files/243588/Need%20help%20implementing%20a%202D%20circular%20gaussian%20-%202019%2010%2019.png)
Need help implementing a 2D circular gaussian
22 ビュー (過去 30 日間)
古いコメントを表示
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:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/243567/image.png)
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:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/243568/image.jpeg)
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:
![Need help implementing a 2D circular gaussian - 2019 10 19.png](https://www.mathworks.com/matlabcentral/answers/uploaded_files/243588/Need%20help%20implementing%20a%202D%20circular%20gaussian%20-%202019%2010%2019.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 件のコメント
その他の回答 (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!