• No results found

This is a short programming assignment designed to get you comfortable with infinite sequences of real numbers. In this programming assignment, you will study sequences of real numbers to determine if certain types of limits exist. For an infinite sequence of real numbersa1, a2, a3, ...(each ai is aterm orelement of

the sequence) one is often interested in answering the following questions:

1. Does the sequencea1, a2, a3, ...converge to any limit, i.e., do terms in the sequence appear to get closer

to any particular number, and if so, what is that number?

2. Construct a new sequence b1=a1, b2=a1+a2, ..., bn=ni=1ai, ...Does the sequence ofpartial sums

b1, b2, b3, ...converge to any limit?

3. Construct yet another sequence c1=a1, c2= a1+2a2, ..., cn=n1ni=1ai, ...Does this sequence ofpartial

averages converge to anything?

You are given some code which will produce terms from three sequences from which you are to find answers to the above questionsfor each of the three diÿerent sequences. This is implemented as a PASCALfunction get from sequence(i: integer) which will return the next term in the i-th sequence (i = 1,2,3) when called. Thus, if we denote the first two terms of the third sequence as a1 and a2 then they may be obtained as follows:

• start over;

• get from sequence(3);

The procedurestart overensures that the first call toget from sequence(following a call tostart over ) generates the very first term in the sequence. Thus, to get the first two elements once again, it is necessary to callstart overbefore usingget from sequence.

Deliverables for this assignment:

You should hand in two neat copies(one for my records) of:

• Supporting documentation for the program: a paragraph describing your code.

• A complete listing of your code together with procedures I have supplied. Your code will contain copious in-line documentation, of course.

• Annotated output from your program. For each sequence: 1. Print the first 20 terms.

2. Print the 100th term of the sequence, the 100th term in the corresponding sequence of partial sums and partial averages.

3. Do the same for the 10000th terms of the sequence, the sequence of partial sums, and partial averages.

• What do the various sequences(and the constructed sequences of partial sums and partial averages) appear to converge to? Can you argue that these are the limits for the sequence?

Note:

• Use at least 6 decimal places of accuracy when printing real numbers.

• Do not confuse the sequence number with the subscript of a particular element or with constructed versions of a particular sequence. Thus, the 2-nd sequence(i = 2 in get from sequence(i)) has its first, second, third,..., etc terms. Corresponding sequences of partial sums and partial averages are constructed using terms entirely from the 2-nd sequence. Thus the three sequences each have two additional constructed sequences which you must consider.

• You may write your program in C if you like.

• You are encouraged to play around with the sequences to see what convergence behavior they demon- strate.

• This is NOT a long assignment. You should not need more than 50(text) lines of code.

• Have fun!

program first_assignment (input, output); const

m = 2147483647; (* const used in random # generator *) num_proc = 10;

var

p,s: array[0..num_proc] of real; x_seed: integer;

168 CHAPTER 4. STOCHASTIC MODELS IN COMPUTER SCIENCE

(*...*)

(* Random number generator *)

function x_random: real; const a = 16807; q = 127773; r = 2836; var t,lo,hi: integer; begin

hi:= x_seed div q; lo:= x_seed - q*hi; t:= a*lo - r*hi; if (t>0) then x_seed:= t else x_seed:= t+m; x_random:= x_seed/m; end; (*...*) (* initialize the random number generator *)

procedure put_seed(x: integer); begin

if (0<x) and (x<m) then x_seed:= x; end;

(*...*)

(* Reset all sequences *)

procedure start_over; begin put_seed(7774755); index_A:= 1; index_B:= 1; end; (*...*) procedure initialize; var i: integer; begin start_over; p[0]:= 0.4; s[0]:= p[0];

for i:= 1 to num_proc do begin

p[i]:= 0.6/num_proc; s[i]:= s[i-1] + p[i]; end;

(*...*) (* This function returns the next term in the specified sequence*)

function get_from_sequence(x: integer):real; var i: integer; u: real; begin case x of 1: begin get_from_sequence:= 1-1/sqr(index_A); index_A:= index_A + 1; end; 2: begin get_from_sequence:= 1/index_B; index_B:= index_B + 1; end; 3: begin u:= x_random; i:= 0;

while (u>s[i]) and (i<10) do i:= i + 1; get_from_sequence:= i; end; end; end; (*...*) (* main program - insert your code here. You may break your code*) (* into several procedures/functions. *)

procedure terms(sqnc: integer); var

i: integer; n: real; begin

start_over;

writeln(outfile, ’The first 20 terms of sequence’,sqnc:2, ’ are: ’); for i:= 1 to 20 do

begin

n:= get_from_sequence(sqnc); writeln(outfile, n:8:6); end;

for i:= 21 to 100 do get_from_sequence(sqnc); writeln(outfile);

writeln(outfile); writeln(outfile);

write(outfile, ’The 100-th term of sequence’,sqnc:2,’ = ’); writeln(outfile, n:12:6);

writeln(outfile);

for i:= 101 to 10000 do n:= get_from_sequence(sqnc);

write(outfile,’The 10000-th term of sequence’,sqnc:2,’ = ’); writeln(outfile,n:18:16);

170 CHAPTER 4. STOCHASTIC MODELS IN COMPUTER SCIENCE

writeln(outfile); writeln(outfile); writeln(outfile); end;

procedure partial_sums(sqnc: integer); var

i: integer; n: real; k: real;

writeln(outfile, ’ The first 20 terms of partial sums of sequence’, sqnc:2,’ are ’); for i:= 1 to 20 do begin n:= get_from_sequence(sqnc); k:= k+n; writeln(outfile, k:8:6); end; for i:= 21 to 100 do begin n:= get_from_sequence(sqnc); k:= k+n; end; writeln(outfile); writeln(outfile); writeln(outfile);

write(outfile,’The 100-th term of partial sums of sequence’, sqnc:2,’ = ’,k:12:6); writeln(outfile); for i:= 101 to 10000 do begin n:= get_from_sequence(sqnc); k:= k+n; end; writeln(outfile); writeln(outfile);

write(outfile,’The 10000-th term of partial sums of sequence’, sqnc:2,’ = ’,k:12:6);

writeln(outfile); writeln(outfile); writeln(outfile); end;

procedure partial_averages(sqnc: integer);

(* the following procedure computes the partial average of a given sequence *) var i: integer; n: real; k: real; m: integer; begin start_over; k:= 0.0;

writeln(outfile, ’The first 20 terms of partial averages of sequence’, sqnc:2,’ = ’); m:= 1; while n<=20 do begin for i:= 1 to m do begin n:= get_from_sequence(sqnc); k:= k+n; end; k:= k/i; writeln(outfile, k:8:6); m:= m+1; start_over; k:= 0.0; end; for i:= 1 to 100 do begin n:= get_from_sequence(sqnc); k:= k+n; end; writeln(outfile); writeln(outfile); k:= k/i;

write(outfile,’The 100-th term of partial averages of sequence’, sqnc:2,’ = ’,k:12:6); start_over; k:= 0.0; for i:= 1 to 10000 do begin n:= get_from_sequence(sqnc); k:= k+n; end; writeln(outfile); writeln(outfile); k:= k/i;

write(outfile,’The 10000-th term of partial averages of sequence’, sqnc:2,’ = ’,k:24:22); writeln(outfile); writeln(outfile); writeln(outfile); writeln(outfile); end; (*...*) begin initialize; for i:= 1 to 3 do begin terms(i); partial_sums(i); partial_averages(i); end;

172 CHAPTER 4. STOCHASTIC MODELS IN COMPUTER SCIENCE

end.

Sequence 1 Here an = 1− n12. As n → ∞,

1

n2 → 0 and so an → 1. A more formal proof would use

N() = 12, but you were not required to provide this. Next, consider the sequence of partial sums

bn =ni=1ai. One may decompose each term in the sequence of partial sums into two parts: bn =

n

i=1ai = n−

n i=1

1

i2. Any decent calculus textbook will(should) tell you that

n i=1

1

i2 converges.

It happens to converge to π62.So the second part of our decomposition gets closer and closer to the constant π2

6.You might have discovered this for yourself by printing out the latter sum and observing

the convergence. The first part diverges. Since, for any constant c,the sequence n+c→ ∞,we must have bn → ∞. Finally, consider the sequence of partial averages cn = n1ni=1ai. Again, the same

decomposition gives us: cn=n1ni=1ai= 1−n1ni=1i12.Since the latter sum converges to a constant,

n

i=1i12 gets closer to π 2

6 for large n. Hence n1

n

i=1i12 will converge to zero. Thus, cn→ 1. Formal

proofs for the last two cases are slightly more complicated and were not required.

Sequence 2 The second sequence isan= n1.We have seen thatan→0 already. Again, your calculus book

would tell you that the sequence of partial sums,bn=ni=11i diverges, although it’s not experimentally

obvious. It’s not very dicult to show divergence by expressing the sum as an integral. It can be shown that bn≥ln(n+ 1) +C1,for some constant C1. Since ln(n+ 1) diverges, bn,diverges. It can also be

shown thatbn≤ln(n) +C2whereC2is also a constant. Thus for each term in the sequence of partial

averages is less than ln(n)+C2

n and so the sequence of partial averages converges to zero. Most of the

arguments were for sequence 1; I did, however, take oÿ a few points for a complete lack of reasoning or very wrong answers.

Sequence 3 This sequence is random, as most of you observed. The sequence itself and the sequence of partial sums do not converge. However, the sequence of partial averages will appear to converge to 3.3. Limits for random sequences are treated a little diÿerently, as we shall see later. The sequence of partial averages does indeed converge to 3.3, but in diÿerent sense than for real sequences. You were not expected to mention this — I only took oÿ points for incorrect observations.

Related documents