Skip to content

Instantly share code, notes, and snippets.

@JaneLSumner
Created December 15, 2015 20:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JaneLSumner/81ca424729c9d92f4f86 to your computer and use it in GitHub Desktop.
Save JaneLSumner/81ca424729c9d92f4f86 to your computer and use it in GitHub Desktop.
A quick R code demonstrating some of the logic behind sequences and convergence of sequences.
### Sequences and Convergence
### Jane Lawrence Sumner
### POLS 508
### Dec. 15, 2015
##### Here are the functions that make the thing work. Scroll down for examples.
fxn <- function(x){
fx <- (x+1)/x ## function goes here
return(fx)
}
## this function generates the nth value of the sequence
sequence <- function(n){
ans <- NULL
ns <- seq(from=1,to=n,by=1)
for(i in 1:length(ns)){
ans <- c(ans,fxn(ns[i]))
}
#plot(c(1:n),ans,type="l") ## it'll plot it if you uncomment this
return(ans)
}
converges <- function(){ ## this function has no input and tells you whether the
## sequence converges after 1000 iterations (roughly)
yn <- round(sequence(1000)[999],2)==round(sequence(1000)[1000],2)
if(yn==T){
return(round(sequence(1000)[1000],2))
} else{
return("Does not converge.")
}
}
# this function tells you, for a given epsilon>0, what is your N such that n>N
# implies d(x^n,x)<epsilon. (Recall from your notes, though, that to prove convergence
# this must be true for an epsilon>0, not just a given epsilon>0)
how.many <- function(epsilon,limit){
if(converges()=="Does not converge."){
return("This sequence does not converge.")
} else{
is.limit <- round(sequence(1000)[1000],2)==limit
if(is.limit==T){
n <- 1
while(abs(limit-fxn(n+1))>=epsilon){
n <- n+1
}
beforethat <- abs(limit-fxn(n-1))
afterthat <- abs(limit-fxn(n+1))
return(list(N=n))
}
else{
return(paste("This sequence does not converge to ",limit,".",sep=""))
}
}
}
#######################
## FOR INSTANCE
#######################
# 1 - Sequence s(n)=n+3
fxn <- function(x){
fx <- x+3
return(fx)
}
sequence(5)
converges() ## This sequence does not converge.
how.many(.02,5) ## What is N such that n>N -> metric distance less than epsilon?
# Nope. Sequence does not converge.
# 2 - Sequence s(n)=(n+1)/n
fxn <- function(x){
fx <- (x+1)/x
return(fx)
}
sequence(5) # First five elements of the sequence.
converges() ## This sequence does converge. It converges to 1.
how.many(.02,5) ## What is N such that the sequence converges to 5 at an epsilon of .02?
how.many(.02,1) ## N=50, so for n>50, metric distance is less than .02
how.many(.001,1) ## N=999, so for n>999, metric distance is less than .02
sequence(1002)-1<.001 ## Can confirm by looking at distances for each n.
which(sequence(1002)-1<.001) ## Yes, n>N -> metric distance less than epsilon
how.many(.1,1) ## N=10, so for n>10, metric distance is less than .1
sequence(12)-1<.1 ## Can confirm by looking at distances for each n.
which(sequence(12)-1<.1) ## Yes, n>N -> metric distance less than epsilon
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment