Creating a 3D surface plot with meshgrid
Show older comments
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)
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
James
on 7 Oct 2014
Firstly, the question gives the hint to use the function pol2cart. You should try this.
Secondly, you need to read about the difference between array and matrix operations . Consider the code you have written (r/15)^2 : how is a power operation for matrices defined?
When I changed your code to take into account the above two points, it plotted something like the image you provided. But it is your homework, not mine ;)
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.
James
on 7 Oct 2014
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!