Mach 9.4 Sequences

196 days ago by mpaul

Here are some sequences:

[5,10..25] 
       
[2^k for k in [1..5]] 
       
[1/k for k in [1..5]] 
       

A sequence is a function whose domain is a subset of the natural numbers.

a(n) = n^2 [a(n) for n in [1..10]] 
       

List Comprehension has this form:

[f(x) for x in domain].

domain represents some interval.

 
       

Example 1 - Explicit Definitions

a(k) = k^2 - 1 show(a) 
       
[a(k) for k in [1..6]] 
       
a(100) 
       

Example 2 - Recursive Definitions

def b(k): if k == 1: return 3 else: return b(k-1)+2 
       
[b(k) for k in [1..100]] 
       

Example 3 - Limits of Sequences

If \{a_n\} is some sequence, then the expression \lim_{n \rightarrow \infty} a_n = L means that the values of the sequence get closer and closer to some value L as n continues to gets larger and larger. We say that the sequence converges to the value L and that L is the limit of the sequence.  If there is  no such value that the sequence approaches, then we say that the sequence diverges.

a(n) = 1/n [a(k) for k in [1..10]] 
       
limit(a(n),n=oo) 
       
points([(k,a(k)) for k in [1..100]]) 
       
b(n) = (n+1)/n [b(k) for k in [1..10]] 
       
limit(b(n),n=oo) 
       
points([(k,b(k)) for k in [1..100]]) 
       
c(n) = 2*n [c(k) for k in [1..10]] 
       
limit(c(n),n=oo) 
       
points([(k,c(k)) for k in [1..100]]) 
       
d(n) = (-1)^n [d(k) for k in [1..10]] 
       
limit(d(n),n=oo) 
       
points([(k,d(k)) for k in [1..10]]).show(aspect_ratio=1) 
       

Example 4 - More Finding Limits of Sequences

a(n) = 3*n/(n+1) [a(k) for k in [1..10]] 
       
limit(a(n),n=oo) 
       
points([(k,a(k)) for k in [1..100]]) 
       
b(n) = 5*n^2/(n^3+1) [b(k) for k in [1..10]] 
       
limit(b(n),n=oo) 
       
points([(k,b(k)) for k in [1..100]]) 
       
c(n) = (n^3+2)/(n^2+n) [c(k) for k in [1..10]] 
       
limit(c(n),n = oo) 
       
points([(k,c(k)) for k in [1..10]]) 
       

Arithmetic Sequences

\{a_n\} = \{a, a+d, a+2d, ..., a+(n-1)d\}

var('a n d') [a+(n-1)*d for n in [1..10]] 
       
[2+(n-1)*3 for n in [1..10]] 
       

Here is an example of a generator:

def arithmetic(a,d): while True: yield a a += d 
       

Generators are essentially functions, but generators yield their values rather than return them.

When a function returns its value, it is finished and forgets its state. If you call it again, it has no record of what it did before.

When a generator yields its value, it stays 'on call' ready to generate the next value.  It remembers its state.

Generators are great for conserving resources. Instead of generating a complete sequence at once, they generate terms as needed.

This can be important if you are dealing with a very large sequence of a very complex formula.

a = arithmetic(2,3) [next(a) for i in [1..10]] 
       
var('a d') a = arithmetic(a,d) [next(a) for i in range(10)] 
       

Example 5 - Defining Arithmetic Sequences

arithmetic(a,d,n) = a + (n-1)*d arithmetic 
       
a(n) = arithmetic(1,2) a 
       
[a(n) for n in [1..10]] 
       

Sage interval notation:

[-6,-2..10] 
       

ad = 4

[-6..10, step = 4] 
       

b) 10th term

a(n) = -6+(n-1)*4 a(10) 
       

d) Simplified nth Term

a.simplify() 
       
[4*n-10 for n in [1..10]] 
       

c) Recursive Rule

def a(n): if n == 1: return -6 else: return a(n-1)+4 
       
a(10) 
       
[ln(3),ln(6)..ln(24)] 
       

a) d = \ln{6} - \ln{3}

d = log(6)-log(3) d 
       

b) 10th term

a(n) = ln(3)+(n-1)*ln(2) a;a(10) 
       
[a(n) for n in [1..10]] 
       

d) Simplified nth term

a.simplify() 
       
[(n-1)*ln(2)+ln(3) for n in [1..10]] 
       

c) Recursive Rule

def a(n): if n == 1: return ln(3) else: return a(n-1)*ln(2) 
       
[a(n) for n in [1..10]] 
       

Geometric Sequences

\{a_n\} = \{a, ar, ar^2, ..., ar^{n-1}\}

var('a r') [a*r^(k-1) for k in [1..10]] 
       
[1024*(1/2)^(k-1) for k in [1..11]] 
       

Example 6 - Defining Geometric Sequences

geometric(a,r,n) = a*r^(n-1) geometric 
       
g(n) = geometric(1,3) g 
       
[g(n) for n in [1..10]] 
       

Here's an example of a generator for a geometric sequence:

def geometric(a, r): while True: yield a a *= r 
       
g = geometric(1,2) [next(g) for i in [1..10]] 
       
var('a r') g = geometric(a,r) [next(g) for i in [1..10]] 
       
[3*2^(n-1) for n in [1..10]] 
       
 
       

The second and fifth terms of a sequence are 3 and 24.

Find the explicit and recursive formulas if it is a) arithmetic and b) geometric:

d = (24-3)/(5-2) d 
       
a(n) = -4+(n-1)*7 [a(k) for k in [1..10]] 
       
def a(n): if n == 1: return -4 else: return a(n-1)+7 [a(k) for k in [1..10]] 
       
r = (24/3)^(1/3) r 
       
g(n) = 3/2*2^(n-1) [g(k) for k in [1..10]] 
       
def g(n): if n == 1: return 3/2 else: return g(n-1)*2 [g(k) for k in [1..10]]