• * So these are the questions... most of the variables are fixed the long equation has been dramatically simplified in my code :) * *
  • | This is what mine looks like.. |
What it should look like...
This is my code...
% Q = theta
r = linspace(0,15,200);
Q = linspace(0,2*pi,200);
[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)
What have I done wrong? Why isnt mine round, I dont think 'r' is being inputed into the 'w' equation correctly, HELP!

 채택된 답변

Star Strider
Star Strider 2014년 10월 7일

1 개 추천

You need to vectorise your code and it will do just what you want:
r = linspace(0,15);
Q = linspace(0,2*pi);
[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)

댓글 수: 9

James
James 2014년 10월 7일
편집: James 2014년 10월 7일
Thank you so much!
I have a few more questions:
  • I want it to be a surface plot, i did surfc(x,y,w) but i want specifically 10 countours, how do i make this happen?
  • And it is very black :S i want a nice range of colours, this is when i do a surface plot
My pleasure!
Changing the range of the axis requires using the axis function, but there is a problem with the MATLAB plotting routines so the plot itself does not appropriately limit with the axes. (I’ll work on this and see if I can make it display correctly. This is a known bug, but there’s likely a work-around.) I also added the colorbar with 10 levels, so that should also provide the number of contours you want, and the shading call will make it look nice:
surfc(x,y,w)
shading interp
axis([0 20 0 20 0 30])
colormap(hsv(10))
colorbar
James
James 2014년 10월 7일
thanks again!
-this is my (near) final code but for some reason the contour isnt at the base. And is it possible to put the min(15) at the bottom of the colour band?Cheers!
r = linspace(0,15,200);
Q = linspace(0,2*pi,200);
[r,Q] = meshgrid(r,Q);
x = r.*cos(Q);
y = r.*sin(Q);
w = 14.0591*(1-(r/15).^2).^2 + 15;
surfc(x,y,w)
shading interp;
axis([-20,20,-20,20,0,40]);
colormap(hsv(10));
colorbar;
xlabel('x (in)');
ylabel('y (in)');
zlabel('w (in)');
title('3D plot of Clamped circular membrane');
The colorbar tick label problem requires a minor modification:
hc = colorbar
hct = linspace(15, 29, 11);
set(hc, 'YTick',hct, 'YTickLabel',hct)
% Reset Contour to zero
new_level = 0;
h = findobj('type','patch');
zd = get(h,'ZData');
for i = 1:length(zd)
set(h(i),'ZData',new_level*ones(length(zd{i}),1))
end
That puts all the y-axis ticks at exactly the colour changes.
I found a solution to the contour position problem in How do I change the location of the contours produced by MESHC or SURFC?, and added it.
These all replace the current call to colorbar in the code you posted.
I found another solution to the x-axis and y-axis limit problem Why does clipping fail to work with MATLAB 3-D plots?. I’ll read it and work on implementing it here. That will take a few minutes.
James
James 2014년 10월 7일
oh thank you, but what does ytick and ytick label do?
And it seems to want 7 contours instead of 10. Got any idea why its doing this? Thanks again for sharing the links.
Star Strider
Star Strider 2014년 10월 7일
My pleasure!
In the set call, 'YTick' sets the positions of the y-axis ticks, and 'YTickLabel' defines what is written to them. I always specify both, since it’s best not to take a chance on the default behaviour when changing anything.
The code plots 10 colours for me, with 10 levels. Note that the two colours between 19.2 and 22 look the same. That’s simply the nature of the ‘lines’ colormap.
With the axes full (rather than limited to positive x and y), the figure I get with the current code is:
James
James 2014년 10월 7일
Ah I understand, yeah mines looking the same :) which is good. Thanks for your time!
Star Strider
Star Strider 2014년 10월 7일
편집: Star Strider 2014년 10월 7일
My pleasure!
The sincerest expression of gratitude here on MATLAB Answers is to Accept the answer that most closely solves your problem.
James
James 2014년 10월 7일

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

추가 답변 (1개)

Image Analyst
Image Analyst 2014년 10월 7일

1 개 추천

r is an array so you need .^ and not just ^. Try this:
workspace;
r = linspace(0,15,200);
Q = linspace(0,2*pi,200);
[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)
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);

댓글 수: 1

James
James 2014년 10월 7일
편집: James 2014년 10월 7일
Thank you so much!
I have a few more questions: ALL GOOD NOW :)
  • I want it to be a surface plot, i did surfc(x,y,w) but i want specifically 10 countours, how do i make this happen?
  • And it is very black :S i want a nice range of colours, this is when i do a surface plot
(Both answers were fantastic, this is the same reply to the previous answer, feel free to help out some more :) )

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

카테고리

질문:

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