Plotting a 3d gaussian function using surf

조회 수: 78 (최근 30일)
Desmond Johnson
Desmond Johnson 2015년 2월 27일
댓글: Xiang Chen 2018년 10월 16일
Let me start off by saying that I am extremely new to MATLAB. I would to use these functions and turn them into a 3d plot using surf. I have already made a mesh grid of my x and y but I am confused on how to plug my gaussian function in as Z. I would like the surf plot to look like this http://i.stack.imgur.com/QiTPe.png
x=randn(1,10000);
y=x';
[X,Y]=meshgrid(x,y);
z=(1000/sqrt(2*pi)*exp(-X.^2/2))
surf(x,y,z);shading interp
Thanks in advance for the help.
  댓글 수: 1
Xiang Chen
Xiang Chen 2018년 10월 16일
This is garbage answer. Making the matrix smaller will be much better.

댓글을 달려면 로그인하십시오.

채택된 답변

Star Strider
Star Strider 2015년 2월 27일
편집: Star Strider 2015년 2월 27일
This works:
N = 3.0;
x=linspace(-N, N);
y=x;
[X,Y]=meshgrid(x,y);
z=(1000/sqrt(2*pi).*exp(-(X.^2/2)-(Y.^2/2)));
surf(X,Y,z);
shading interp
axis tight
Experiment to get the result you want.
  댓글 수: 5
Desmond Johnson
Desmond Johnson 2015년 2월 28일
Thank you for explaining your process to me and I already accepted your answer. I am still not able to plug in my data and get the same surf plot that you have. The only way I get the gaussian function to display correctly is if I sort the x and y values. Is it possible to make my gaussian function look like yours without using the sort function? Here is the code I am currently using and I am running MATLAB 2014a.
x=randn(1,10000);
y=x;
[X,Y]=meshgrid(x,y);
z=(1000/sqrt(2*pi).*exp(-(X.^2/2)-(Y.^2/2)));
surf(X,Y,z);
Star Strider
Star Strider 2015년 2월 28일
My pleasure.
You have to use a linear vector in x and y, as did I with linspace. I see no reason to use randn here, since it is by definition random and will create very strange matrices with meshgrid.
To experiment, do a simple surf plot of the meshgrid outputs using a randn vector and using linspace for x and y. Use different randn calls for x and y each in order to avoid the ‘banding’ you saw in the plot you posted.
If you want to use randn at all to look at the distribution of the points, consider using scatter3:
x = randn(1,10000);
y = randn(1,10000);
zf=@(X,Y) (1000/sqrt(2*pi).*exp(-(X.^2/2)-(Y.^2/2)));
z = zf(x,y);
figure(1)
scatter3(x, y, z, 'b.')
grid on
Rotate it in the GUI to get an interesting perspective on the density of the distribution.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by