How can I plot a simple circle not centered at the origin? example: (x-5)^2+(y-10)^2=4

example: (x-5)^2+(y-10)^2=4 I actually want to plot several overlapping circles on the same graph. I have never used matlab prior to this week so if there is a way to do this using SIMPLE function, plot, ezplot, etc. that's what I'm looking for. Thank you.

 채택된 답변

Amit
Amit 2014년 1월 31일
편집: Amit 2014년 1월 31일
You make a function for plotting the circle, something like this:
EDIT:
function plotcircle(x1,y1,r,c)
% x1,y1: center
% r = radius
theta = 0:0.05:2*pi;
hold on;
plot(x1+r*cos(theta),y1+r*sin(theta),c);
axis equal;
For for every circle, you can call this function: like
plotcircle(5,10,2,'r');

댓글 수: 6

That's exactly what i was looking for. I plotted 5 circles, but i want to change the colors and plotcircle(5,10,2,'r'); for example doesn't work. can you tell me what I'm missing?
See the edited code. The prior function didn't have the color variable. Now it does.
I tried it just like you have it here by changing the function to include the "c" but it throws the following error: Error using plotcircle Too many input arguments.
Error in Project1 (line 3) plotcircle(5,10,4,'y');
Did you tried the edited code (not just the edited function call).
I did.... plot(x+r*cos(theta),y+r*sin(theta),c)
I found my mistake... I didn't add the ,c at the very top. Thank you so much for your help!

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

추가 답변 (1개)

Image Analyst
Image Analyst 2014년 1월 31일

댓글 수: 5

I looked there but didn't find exactly what I was looking for. Thank you though.
You'll notice that the code there:
xCenter = 12;
yCenter = 10;
theta = 0 : 0.01 : 2*pi;
radius = 5;
x = radius * cos(theta) + xCenter;
y = radius * sin(theta) + yCenter;
plot(x, y);
axis square;
xlim([0 20]);
ylim([0 20]);
grid on;
is virtually identical to Amit's code except he named the variables differently and put the equations into the plot() function instead of into variables (where they can be accessed later if desired). So it seemed exactly what you needed, to me at least.
Image Analyst, I did not copy the code from the wikia page. The simplest way get all the cartesian coordinate values for a circle is the parametric form equation, which is what I used.
I didn't think you did. It's so very simple so I'm sure you thought of it independently. I just brought it up because I'm hoping the poster, or others, might also scan the other interesting stuff in there. A lot of people don't know about the existence of the FAQ, and won't unless we keep plugging it now and then.
I am really sorry then. I thought by 'he named the variable differently' you meant I copied the code.

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

카테고리

도움말 센터File Exchange에서 2-D and 3-D Plots에 대해 자세히 알아보기

질문:

2014년 1월 31일

댓글:

2014년 1월 31일

Community Treasure Hunt

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

Start Hunting!

Translated by