Creating a 3D surface plot with meshgrid

7 views (last 30 days)
James
James on 7 Oct 2014
Commented: James on 7 Oct 2014
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.
********************

Answers (1)

Stephen23
Stephen23 on 7 Oct 2014
Edited: Stephen23 on 7 Oct 2014
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 Comments
Star Strider
Star Strider on 7 Oct 2014
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.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!