mat 4725 numerical analysis section 1.4 loops with “do” statements

33
MAT 4725 Numerical Analysis Section 1.4 Loops with “do” statements http://myhome.spu.edu/lauw

Upload: hubert-pearson

Post on 04-Jan-2016

219 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: MAT 4725 Numerical Analysis Section 1.4 Loops with “do” statements

MAT 4725Numerical Analysis

Section 1.4

Loops with “do” statements

http://myhome.spu.edu/lauw

Page 2: MAT 4725 Numerical Analysis Section 1.4 Loops with “do” statements

Homework

Download homework from the web Read

• 2.1.4 while-do loop

• 1.6.1 documentations

• 1.6.2 format printing

Quiz on 1.6.2, we will not lecture on that section

Page 3: MAT 4725 Numerical Analysis Section 1.4 Loops with “do” statements

Preview

Monotonic Sequence Theorem (Stewart, section 12.1)

Introduce the first type of repetition statements – the for loop

Allow a specific section of code to be executed a number of times

Introduces simple arrays

Page 4: MAT 4725 Numerical Analysis Section 1.4 Loops with “do” statements

Definition

A sequence {an} is bounded above if M such that

anM n

A sequence {an} is bounded below if m such that

anm n

Page 5: MAT 4725 Numerical Analysis Section 1.4 Loops with “do” statements

Monotonic Sequence Theorem

The following sequences are convergent Increasing and bounded above Decreasing and bounded below

1n na a

Page 6: MAT 4725 Numerical Analysis Section 1.4 Loops with “do” statements

Example

Show that the sequence defined by

is convergent and find its limit.

1 1

12 and for 1

3nn

a a na

Page 7: MAT 4725 Numerical Analysis Section 1.4 Loops with “do” statements

Example

From homework 01, we know

1 1

12 and for 1

3nn

a a na

10 2 and for n n na a a n

Page 8: MAT 4725 Numerical Analysis Section 1.4 Loops with “do” statements
Page 9: MAT 4725 Numerical Analysis Section 1.4 Loops with “do” statements

Zeng Section 1.4

Please listen to the explanations before you type in the program.

It takes one minute to explain.

Page 10: MAT 4725 Numerical Analysis Section 1.4 Loops with “do” statements

Example 1 Print the square of the first 10 positive

integers What is the task being repeated?

Page 11: MAT 4725 Numerical Analysis Section 1.4 Loops with “do” statements

Example 1

Page 12: MAT 4725 Numerical Analysis Section 1.4 Loops with “do” statements

Example 1

i

1 2 101 4 100

i2i

Page 13: MAT 4725 Numerical Analysis Section 1.4 Loops with “do” statements

Example 1

> sq();149

Page 14: MAT 4725 Numerical Analysis Section 1.4 Loops with “do” statements

Structure of the for loop

Page 15: MAT 4725 Numerical Analysis Section 1.4 Loops with “do” statements

Structure of the for loop

Page 16: MAT 4725 Numerical Analysis Section 1.4 Loops with “do” statements

Example 2 Print the square of the first 10 positive

odd integers

Page 17: MAT 4725 Numerical Analysis Section 1.4 Loops with “do” statements

Example 2

Page 18: MAT 4725 Numerical Analysis Section 1.4 Loops with “do” statements

Example 2

> sq2();19

25

Page 19: MAT 4725 Numerical Analysis Section 1.4 Loops with “do” statements

Example 3 Print the square of the first n positive

integers

Page 20: MAT 4725 Numerical Analysis Section 1.4 Loops with “do” statements

Example 3 Print the square of the first n positive

integers Introduces array and seq Note that these commands are not

necessary here

Page 21: MAT 4725 Numerical Analysis Section 1.4 Loops with “do” statements

Example 3

Page 22: MAT 4725 Numerical Analysis Section 1.4 Loops with “do” statements

Example 3

[ ]x n

[3]x[2]x[1]x

Page 23: MAT 4725 Numerical Analysis Section 1.4 Loops with “do” statements

Example 3

> sq3(2);1, 4

> sq3(5);1, 4, 9, 16, 25

Page 24: MAT 4725 Numerical Analysis Section 1.4 Loops with “do” statements

Example 4

Fibonacci sequence is defined by

0 1 1 20, 1, for 2,3,

{0, 1, 1, 2, 3, 5, }

k k kF F F F F k

Page 25: MAT 4725 Numerical Analysis Section 1.4 Loops with “do” statements

Example 4 Write a program that generate the first

n+1 terms of the Fibonacci sequence

F0,F1,…,Fn

0 1 1 20, 1, k k kF F F F F

Page 26: MAT 4725 Numerical Analysis Section 1.4 Loops with “do” statements

Example 4 0 1 1 20, 1, k k kF F F F F

Page 27: MAT 4725 Numerical Analysis Section 1.4 Loops with “do” statements

Example 4 0 1 1 20, 1, k k kF F F F F

What happen if we do not

initialize F?

Page 28: MAT 4725 Numerical Analysis Section 1.4 Loops with “do” statements

Example 4 0 1 1 20, 1, k k kF F F F F

Why there is no print statement?

Page 29: MAT 4725 Numerical Analysis Section 1.4 Loops with “do” statements

Example 4 0 1 1 20, 1, k k kF F F F F

Page 30: MAT 4725 Numerical Analysis Section 1.4 Loops with “do” statements

Example 5

2 1 2 1

0 0

( 1) ( 1)sin

(2 1)! (2 1)!

k knk k

k k

x x xk k

Write a program, for the input of x and n, to approximate the value of sin(x) by the first sum of the first n+1 terms in the Taylor series.

Page 31: MAT 4725 Numerical Analysis Section 1.4 Loops with “do” statements

Example 5

2 1

0

( 1)sin

(2 1)!

knk

k

x xk

Page 32: MAT 4725 Numerical Analysis Section 1.4 Loops with “do” statements

Example 5

2 1

0

( 1)sin

(2 1)!

knk

k

x xk

Page 33: MAT 4725 Numerical Analysis Section 1.4 Loops with “do” statements

Example 5

2 1

0

( 1)sin

(2 1)!

knk

k

x xk