How to plot six concentric circles using meshgrid and plot?

I am trying to plot six concentric circles using meshgrid() and plot(). The circles' radii vary between 0.5 and 1.75 (with intervals of 0.25). I am wondering whether the code below is sufficiently efficient and whether indeed it should be performed as delineated below:
theta = linspace(0, 2*pi, 50);
[X, Y] = meshgrid(0.5:0.25:1.75, theta);
plot(a+cos(Y).*X, b+sin(Y).*X);
?

 채택된 답변

Matt J
Matt J 2013년 3월 27일
You could conserve a little memory if you did it this way, but I don't know anyone who cares about efficiency for such a small plotting task,
theta = linspace(0, 2*pi, 50).';
R=0.5:0.25:1.75;
plot(a+cos(theta)*R, b+sin(theta)*R);

댓글 수: 8

Should there be a dot after the closed parenthesis, viz. theta = linspace(0, 2*pi, 50)*.'*; What is its purpose?
Matt J
Matt J 2013년 3월 27일
편집: Matt J 2013년 3월 27일
.' transposes a vector or matrix. If you tried my code, you should have seen that it works.
Thanks Matt! Just one thing - I realise it works. However, why would the transpose of theta be requisite in this case? To what end?
Matt J
Matt J 2013년 3월 27일
편집: Matt J 2013년 3월 27일
If a is a column vector and b is a row vector then the entries of a*b will consist of all combinations of products a(i)*b(j). That is what we want to do with a=cos(theta) and b=R. But LINSPACE always outputs a row vector, so the transpose is needed to make it into a column vector.
But is that truly necessary ("needed", as you put it)? I mean, as I have tried I am able to confirm that the required plotting gets done even without that minor emendation. How is that "needed" then? Please care to clarify.
Matt J
Matt J 2013년 3월 27일
편집: Matt J 2013년 3월 27일
You mean you ran my code exactly, but with the transpose omitted and everything worked fine? I don't think so. You should have gotten a dimension mismatch error as I do below
Error using *
Inner matrix dimensions must agree.
Error in test (line 6)
plot(a+cos(theta)*R, b+sin(theta)*R);
And it's perfectly clear why this happens. Matrix multiplication between 2 row vectors together like cos(theta) and R or sin(theta) and R is undefined.
Yuval
Yuval 2013년 3월 27일
편집: Yuval 2013년 3월 27일
I mean I ran mine, the original one I posted here, in which the transpose was clearly omitted, and yet the plot was just as that generated by your own code, with the transpose. Is that still impossible? Did you mean to suggest that that is strictly mandatory if R is defined as you proposed? Please allow me to re-post what works perfectly fine on this machine:
theta = linspace(0, 2*pi, 50); R = 0.5: 0.25: 1.75; [X, Y] = meshgrid(R, theta); plot(a+cos(Y).*X, b+sin(Y).*X);
Matt J
Matt J 2013년 3월 27일
편집: Matt J 2013년 3월 27일
Yes, the transposition of theta was only relevant to my proposed approach -- the one that does not use meshgrid. Notice that you use elementwise .* multiplication whereas I use pure matrix multiplication '*'. Matrix multiplication requires the operands to be appropriately shaped.

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Logical에 대해 자세히 알아보기

질문:

2013년 3월 27일

Community Treasure Hunt

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

Start Hunting!

Translated by