1

I am new to R programming and I am trying to create the witch of Agnesi which is should be like this:

enter image description here

This is my code:

dat<- data.frame(t=seq(0.07*2*pi, 0.43*2*pi, by=0.1) )
 m <-  function(t) 2*(1/(tan(t)))
 n <- function(t) (2*sin(t)^2)
ggplot(dat, aes(x=t)) + stat_function(fun=m)+stat_function(fun=n)

Unfortunately, my graph looks like this:

enter image description here

I really appreciate any advice

macintosh
  • 161
  • 12

1 Answers1

0

ggplot will only plot y against x, so this sort of parametrised curve in t will not work - you are just getting the two x and y plots in terms of t. As an alternative, you could do the following...

dat<- data.frame(t=seq(0.07*2*pi, 0.43*2*pi, by=0.1))
m <-  function(t) 2*(1/(tan(t)))
n <- function(t) (2*sin(t)^2)
dat$x <- m(dat$t)
dat$y <- n(dat$t)
ggplot(dat, aes(x=x,y=y)) + geom_line() + ylim(0,NA)

enter image description here

If you want the generating circle as well, you could add this to the ggplot term...

+ coord_fixed() #to keep the x and y dimensions equal
+ annotate("path", x=cos(seq(0,2*pi,length.out=100)),
                   y=1+sin(seq(0,2*pi,length.out=100)),
                   color="blue")
Andrew Gustar
  • 17,295
  • 1
  • 22
  • 32
  • That curve is the Witch of Agnesi. The other is just the circle that generates it. The one you drew is a different curve. – Andrew Gustar Aug 13 '17 at 21:53
  • See here for several ways to draw circles... https://stackoverflow.com/questions/6862742/draw-a-circle-with-ggplot2. I've added one method to the answer above. – Andrew Gustar Aug 13 '17 at 22:25
  • Not only a circle! "Starting with a fixed circle, a point O on the circle is chosen. For any other point A on the circle, the secant line OA is drawn. The point M is diametrically opposite to O. The line OA intersects the tangent of M at the point N. The line parallel to OM through N, and the line perpendicular to OM through A intersect at P. As the point A is varied, the path of P is the Witch of Agnesi." Please check: http://mathworld.wolfram.com/WitchofAgnesi.html – Axis Aug 13 '17 at 22:32