Drawing a circle .. help please.

조회 수: 7 (최근 30일)
Keith Elliott
Keith Elliott 2018년 6월 13일
댓글: Keith Elliott 2018년 6월 14일
Hi .. I'm trialling matlab to see if it's worth the money for me. Array limits, speed and graphical output being my concerns.
right now I can't even draw a circle and would appreciate any advice.
the first code I tried was
clear
x=-1:0.01:1;
y=sqrt(1-(x.^2));
y1=-y;
plot(x,y)
plot(x,y1)
This only gives me half a circle(upper or lower).
Also if I use 0.0000001 as the steps instead of 0.01 the calculations are quick but the plot takes forever to happen.
I then tried ..
clear
x=-1;
while(x<1)
y=sqrt(1-(x^2));
plot(x,y)
x=x+0.01;
end
to try and avoid array size issues and plot points as you calculate them (without storing data) but this didn't give me any chart at all (heaven knows why not)
any suggestions / clarifications very welcome
thanks

채택된 답변

Aquatris
Aquatris 2018년 6월 13일
편집: Aquatris 2018년 6월 13일
Use a different equation that describes the circle to make it easier.
theta = 0:0.01:2*pi;
r = 2;
x = r*cos(theta);
y = r*sin(theta);
plot(x,y),axis equal
The problem with your approach is, plot function overwrites the data. Use
plot(x,y,x,y1)
or if you want both parts to be the same color use
plot(x,y,'b',x,y1,'b')
where 'b' stands for blue.
Alternatively, after the first call to plot function, add 'hold on' to tell Matlab the next plot function should not erase the previous content within the plot.
  댓글 수: 1
Keith Elliott
Keith Elliott 2018년 6월 14일
the bit that helped was they 'hold on' command.
the sin / cos approach won't work for me (until I get to the complex plane form) as I'll also be looking to solve x^n+y^n=1 (where n is even) as n tends to infinity to try and include squares (similarly as n-> infinity for ellipses to rectangles)
the hold on command deals with the plotting aspect for simple 2d stuff which got me past one problem! Many more problems to hurdle, no doubt, but little by little is good :)

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

추가 답변 (2개)

Image Analyst
Image Analyst 2018년 6월 13일
Code samples for drawing circles, ellipses, and arcs are in the FAQ: https://matlab.wikia.com/wiki/FAQ#How_do_I_create_a_circle.3F
  댓글 수: 1
Keith Elliott
Keith Elliott 2018년 6월 13일
thanks. I found the section you were referring to.
It's my bad.. I didn't ask my question quite specifically enough.
My overall objective is to derive and plot potentially irregular orbits of stars, planets and moons in theoretical triple star systems over long time scales. this means I will need to calculate (the maths is simple) x,y coordinates (assuming coplanar orbits) for each body's position at a lot of time intervals
To understand matlab likely limitations I wanted to derive points on a circle (x^2+y^2=1) in the two different ways (or three if you include steps of 0.0000001).
So my question should have been...
please can anyone offer advice/suggestions on how to plot a circle on these ways
(ps I've not investigated plotyy yet but that may help)
thanks again (in advance)

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


Keith Elliott
Keith Elliott 2018년 6월 13일
thanks Aquatris.. that definitely helps.
  댓글 수: 2
Image Analyst
Image Analyst 2018년 6월 13일
His code is basically the 5th code chunk in the FAQ I gave you the link for.
Keith Elliott
Keith Elliott 2018년 6월 14일
Thanks. You're right of course. The bit that helped was the 'hold on' command.. I hadn't come across it before. It solved the simple graphical display issue I had.

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

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

제품


릴리스

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by