Search code examples
rgraphtrigonometry

Graphing equations where x and y have trigonometric functions applied to them in r


I am trying to graph the function ((sin(a*(pi/10))+x)^2 + (cos(a*(pi/10))+y)^2 -1)/(0.7*abs(x))=y (with a being any value from 1-20) in r but am struggling as all the r functions seem to need to have equations in the format function(x) = y with no ys in function(x), and no trigonometric functions being applied to y.

I've tried: curve((((sin(1*(pi/10))+x)^2 + (cos(1*(pi/10))+y)^2 -1)/(0.7abs(x)))) and curve((((sin(1(pi/10))+x)^2 + (cos(1*(pi/10))+y)^2 -1)/(0.7*abs(x)))-y) where I've specified y as: y <- seq(-3,3,length=100) (and a=1) but I get warning messages of:

Warning messages:
1: In (sin(1 * (pi/10)) + x)^2 + (cos(1 * (pi/10)) + y)^2 :
  longer object length is not a multiple of shorter object length
2: In (((sin(1 * (pi/10)) + x)^2 + (cos(1 * (pi/10)) + y)^2 - 1)/(0.7 *  :
  longer object length is not a multiple of shorter object length

and the graphs it produced are not right (I have checked by plotting it on Wolfram alpha).

My problem seems to be the same as the one here Graphing more complicated trigonometric functions in R (sorry if this isn't the right way to link to other questions, I'm new to stack overflow) but it hasn't been satisfactorily answered.

Any help would be much appreciated!


Solution

  • This is an implicit equation.

    a <- 1
    f <- function(x, y) {
      y - ((sin(a*(pi/10))+x)^2 + (cos(a*(pi/10))+y)^2 -1)/(0.7*abs(x))
    }
    
    x <- seq(-1.5, 1, len = 200)
    y <- seq(-2.5, 0.5, len = 200)
    z <- outer(x, y, f)
    cr <- contourLines(x, y, z, levels = 0)
    
    plot(cr[[1]]$x, cr[[1]]$y, type = "l")
    

    enter image description here