Creating a 3D surface plot with meshgrid

조회 수: 6 (최근 30일)
James
James 2014년 10월 7일
댓글: James 2014년 10월 7일
a) "Generate a range of values for variables ‘r’ (between zero and rd) and a new variable theta (between zero and 2pi) using the command meshgrid"
rd = 15
I did...
x = linspace(0,15);
y = linspace(0,2*pi);
[x,y] = meshgrid(x,y);
Is this right?
Im really really confused, please view attached for full question, any tips and help are much appreciated.
********************

답변 (1개)

Stephen23
Stephen23 2014년 10월 7일
편집: Stephen23 2014년 10월 7일
It looks fine, although you might want to give different names to the matrices, as you might want those vectors later:
[X,Y] = meshgrid(x,y);
The best way to check your code is to try it, and in MATLAB this is made pretty easy by the close integration of the IDE to the actual running of code. As you are writing your code and want to check that everything is working well, you could:
  • check variables in the workspace (you can open them by double-clicking to view them in more detail).
  • use disp or do your calculation without a semi-colon to display values in the command window.
  • use the inbuilt debugging tools to check how the code runs.
  댓글 수: 4
Star Strider
Star Strider 2014년 10월 7일
When in doubt, vectorise !
Your code is fine but you need to do element-by-element operations in your code using the dot operator, replacing (*) with (.*), (^) with (.^) and (/) with (./) when you are doing element-by-element operations on arrays. See the documentation on ‘Special Characters’ for details.
Your code, vectorised:
r = linspace(0,15);
Q = linspace(0,2*pi);
% Q = theta
[r,Q] = meshgrid(r,Q);
w = 14.0591*(1-(r/15).^2).^2 + 15;
x = r.*cos(Q);
y = r.*sin(Q);
mesh(x,y,w)
It should now do what you want.
James
James 2014년 10월 7일
Thank you! I have it all sorted now :)
Feel free to look at my other question

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

카테고리

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

Community Treasure Hunt

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

Start Hunting!

Translated by