Spiral Graph Creation

Using Fibonacci Sequence

SChan

Fibonacci Sequence

The Fibonacci Sequence is the series of numbers:

1, 1, 2, 3, 5, 8, 13, 21, 34, ...

The next number is found by adding up the two numbers before it.

The 2 is found by adding the two numbers before it (1+1). Similarly, the 3 is found by adding the two numbers before it (1+2).

Chart Creation Using Fibonacci Sequence (1)

We can use the Fibonacci numbers to create a spiral on a Cartesian plane. First, we can use sapply function over a sequence of numbers starting from 1 to create a Fibonacci series.

library(UsingR)
library(numbers)
n<-20
s <- seq(1:n)
fib <- as.integer(sapply(s, fibonacci))  

Then we choose the numbers with odd indices to be the x-coordinates and those with even indices to be the y-coordinates.

x1<- ifelse(seq(1:n)%%2 ==1, fib, 0)
y1<- ifelse(seq(1:n)%%2 ==0, fib, 0)

Chart Creation Using Fibonacci Sequence (2)

Next, for both series, we alternate the signs. To make as many points visible as possible, we take the log of the absolute value of the non-zero coordinates, while keeping their original signs.

x2 <-ifelse(seq(1:n)%%4==1, x1,-x1)
y2 <-ifelse(seq(1:n)%%4==2, y1,-y1)
x <- ifelse(x2 >0, log(x2), ifelse(x2<0, -log(-x2),0))
y <- ifelse(y2 >0, log(y2), ifelse(y2<0, -log(-y2),0))

With the transformed series of x and y coordinates, we can plot the points and line them up.

plot(x,y)
lines(x,y, col="red")  

Chart Creation Using Fibonacci Sequence (3)

plot of chunk unnamed-chunk-5