mat 4725 numerical analysis section 1.4 loops with “do” statements
TRANSCRIPT
MAT 4725Numerical Analysis
Section 1.4
Loops with “do” statements
http://myhome.spu.edu/lauw
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
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
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
Monotonic Sequence Theorem
The following sequences are convergent Increasing and bounded above Decreasing and bounded below
1n na a
Example
Show that the sequence defined by
is convergent and find its limit.
1 1
12 and for 1
3nn
a a na
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
Zeng Section 1.4
Please listen to the explanations before you type in the program.
It takes one minute to explain.
Example 1 Print the square of the first 10 positive
integers What is the task being repeated?
Example 1
Example 1
i
1 2 101 4 100
i2i
Example 1
> sq();149
Structure of the for loop
Structure of the for loop
Example 2 Print the square of the first 10 positive
odd integers
Example 2
Example 2
> sq2();19
25
Example 3 Print the square of the first n positive
integers
Example 3 Print the square of the first n positive
integers Introduces array and seq Note that these commands are not
necessary here
Example 3
Example 3
[ ]x n
[3]x[2]x[1]x
Example 3
> sq3(2);1, 4
> sq3(5);1, 4, 9, 16, 25
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
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
Example 4 0 1 1 20, 1, k k kF F F F F
Example 4 0 1 1 20, 1, k k kF F F F F
What happen if we do not
initialize F?
Example 4 0 1 1 20, 1, k k kF F F F F
Why there is no print statement?
Example 4 0 1 1 20, 1, k k kF F F F F
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.
Example 5
2 1
0
( 1)sin
(2 1)!
knk
k
x xk
Example 5
2 1
0
( 1)sin
(2 1)!
knk
k
x xk
Example 5
2 1
0
( 1)sin
(2 1)!
knk
k
x xk