forked from burakbayramli/books
-
Notifications
You must be signed in to change notification settings - Fork 0
/
02.R
93 lines (85 loc) · 2.61 KB
/
02.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
logistic.map <- function(x,r) {
return(4*r*x*(1-x))
}
logistic.map.ts <- function (timelength,r,initial.cond=NULL) {
x <-vector(mode="numeric",length=timelength)
if(is.null(initial.cond)) {
x[1] <-runif(1)
} else {
x[1] <-initial.cond
}
for (t in 2:timelength) {
x[t] = logistic.map(x[t-1],r)
}
return(x)
}
plot.logistic.map.bifurcations <- function(from=0,to=1,n=201,plotted.points=1000,transients=10000,ylim=c(0,1)) {
r.values = seq(from=from,to=to,length.out=n)
total.time = transients+plotted.points
plot(NULL,NULL,xlab="r",ylab="x",xlim=c(from,to),ylim=ylim,main="Bifurcation Diagram for Logistic Map")
for (r in r.values) {
x = logistic.map.ts(total.time,r)
x = x[(transients+1):total.time]
points(rep(r,times=plotted.points),x,cex=0.01)
}
}
plot.logistic.map.trajectories <- function(timelength,num.traj,r,lined=TRUE,cex=1) {
plot(NULL,NULL,xlim=c(0,timelength),ylim=c(0,1),xlab="t",ylab="x(t)")
i = 0
while (i < num.traj) {
i <- i+1
x <- logistic.map.ts(timelength,r)
if (lined==TRUE) {
lines(1:timelength,x,lty=2)
}
points(1:timelength,x,cex=cex)
}
}
logistic.threestep <- function(x,r) {
logistic.map(logistic.map(logistic.map(x,r),r),r)
}
plot.logistic.map.timeaverages <- function(timelength,num.traj,r,lined=TRUE,cex=1) {
plot(NULL,NULL,xlim=c(0,timelength),ylim=c(0,1),xlab="t",ylab="Time average of x(t)")
i = 0
while (i < num.traj) {
i <- i+1
x <- logistic.map.ts(timelength,r)
x.avg = cumsum(x)/(1:timelength)
if (lined==TRUE) {
lines(1:timelength,x.avg,lty=2)
}
points(1:timelength,x.avg,cex=cex)
}
}
plot.logistic.map.cosine.timeaverages <- function(timelength,num.traj,r,lined=TRUE,cex=1) {
plot(NULL,NULL,xlim=c(0,timelength),ylim=c(-1,1),xlab="t",ylab="Time average of cos(2pi x(t))")
i = 0
while (i < num.traj) {
i <- i+1
x <- cos(2*pi*logistic.map.ts(timelength,r))
x.avg = cumsum(x)/(1:timelength)
if (lined==TRUE) {
lines(1:timelength,x.avg,lty=2)
}
points(1:timelength,x.avg,cex=cex)
}
}
plot.little.line = function(center,width,height,...) {
lines(c(center-width,center+width),c(height,height),...)
}
logistic.map.fates = function(iterations,n=1000,from=0,to=1,r=1,...) {
x = seq(from=from,to=to,length.out=n)
x.ic = x
plot(NULL,NULL,xlim=c(from,to),ylim=c(0,iterations),xlab="x",ylab="t")
iterate = 0
while(iterate <= iterations) {
blacks = x.ic[x <= 0.5]
reds = x.ic[x > 0.5]
num.blacks = length(blacks)
num.reds = length(reds)
sapply(blacks, plot.little.line, width=1/(2*n),height=iterate,col="black",...)
sapply(reds, plot.little.line, width=1/(2*n),height=iterate,col="red",...)
iterate = iterate+1
x = logistic.map(x,r)
}
}