Creating a 3D surface plot with meshgrid

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 个)

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 个评论

hey mate thanks for the reply.
What ive done
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)
*********
So I got just one skinny line, it should look similar to this... Can you read question a and b, ive clearly done something wrong cheers.
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 ;)
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.

请先登录,再进行评论。

类别

帮助中心File Exchange 中查找有关 Logical 的更多信息

提问:

2014-10-7

评论:

2014-10-7

Community Treasure Hunt

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

Start Hunting!

Translated by