time
time

Reputation: 949

Plotting circle

I want to draw a circle like this.

The equation of the circle is : (x-4)^2+(y-2)^2=25

So i wrote down R codes using curve command. But it is not drawing circle exactly.

 curve(4-sqrt(25-(x-2)^2),xlim=c(-4,10),ylim=c(-4,10))

 curve(4+sqrt(25-(x-2)^2),add=TRUE,col="red")

and it is producing circle like following :enter image description here.

Where is/are the fault(s) in my commands? also the commands produce warning

 Warning message:
 In sqrt(25 - (x - 2)^2) : NaNs produced

I know there is a function draw.circle in R.

But i want to identify my codes fault(s).

Upvotes: 2

Views: 243

Answers (1)

J.R.
J.R.

Reputation: 3888

The warning message comes from the fact that you try to take sqrt() of a negative number. The circle can be drawn by increasing the data-points.:

N<-10000
curve(4-sqrt(25-(x-2)^2),xlim=c(-4,10),ylim=c(-4,10),n=N)
curve(4+sqrt(25-(x-2)^2),add=TRUE,col="red",n=N)

of course you should in general use the arguments of from,to to control the calculations of the data-points pointed out by Ben:

curve(4-sqrt(25-(x-2)^2),xlim=c(-4,10),ylim=c(-4,10),from=-3,to=7,n=N)
curve(4+sqrt(25-(x-2)^2),add=TRUE,col="red",from=-3,to=7,n=N) 

also notice you are plotting (y,x) and not the usual (x,y). Judging by the comments, it seems you want:

curve(2-sqrt(25-(x-4)^2),xlim=c(-4,10),ylim=c(-4,10),from=-1,to=9,n=N)
curve(2+sqrt(25-(x-4)^2),add=TRUE,col="red",from=-1,to=9,n=N)

Upvotes: 4

Related Questions