5

I want to plot the following function in R

f(w) = 1/(1-5*e^(-iw))

where i is the square root of -1. Can R handle complex numbers in plotting?

jangorecki
  • 16,384
  • 4
  • 79
  • 160
user6291
  • 541
  • 5
  • 17

1 Answers1

10

This should get you started (mostly by demonstrating the notation R uses for representing complex numbers and the exponential function).

f <- function(x) 1/(1-5*exp(-(0+1i)*x))
x <- seq(0, 2*pi, by=0.1)
plot(f(x), asp = 1)

enter image description here

Josh O'Brien
  • 159,210
  • 26
  • 366
  • 455
  • 1
    Awesome thanks! I'll accept the answer in 9 minutes when it lets me – user6291 Nov 20 '13 at 23:27
  • 2
    Interestingly, `curve(f)` seems to behave differently, even though `plot.function` calls `curve`. – joran Nov 20 '13 at 23:33
  • @joran -- That is interesting. The different result arises because `plot(f(x))` actually runs through `plot.default()` and from there `xy.coords()`, which has a branch that splits out the real and imaginary components of a complex number into `x` and `y` coordinates. The original `x` is only represented implicitly in the final plot, as an index of the curve of points. `curve()`, on the other hand, plots the original `x` on on the abcissa and the real component of the returned complex number on the ordinate. (The imaginary component is simply thrown away). – Josh O'Brien Nov 21 '13 at 00:19