Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
NUMERICAL ANALYSIS
AND COMPUTER
PROGRAMMING
INTRODUCTION TO
COMPUTER SCIENCE AND
PROGRAMMING
06/04/2020 A.J.M. Minhuz Uddin Ahmed, IUT GS 4353: Numerical Analysis and Computer Programming 2
Goals
06/04/2020 A.J.M. Minhuz Uddin Ahmed, IUT GS 4353: Numerical Analysis and Computer Programming 3
What does a computer do?
• Fundamentally a computer:
– Performs calculations
– Remembers the results
• What calculations?
– Built in primitives
– Creating our own methods of calculating
06/04/2020 A.J.M. Minhuz Uddin Ahmed, IUT GS 4353: Numerical Analysis and Computer Programming 4
Is that all it does?
A billion calculations per second
06/04/2020 A.J.M. Minhuz Uddin Ahmed, IUT GS 4353: Numerical Analysis and Computer Programming 5
Are simple calculations enough?
• Searching the World Wide Web
• Playing chess
• Good algorithm design also needed to accomplish
a task!
06/04/2020 A.J.M. Minhuz Uddin Ahmed, IUT GS 4353: Numerical Analysis and Computer Programming 6
… so are there limits?
• Despite its speed and storage, a computer
does have limitations
– Some problems still too complex
• Accurate weather prediction at a local scale
• Cracking encryption schemes
– Some problems are fundamentally impossible to
compute
• Predicting whether a piece of code will always halt
with an answer for any input
06/04/2020 A.J.M. Minhuz Uddin Ahmed, IUT GS 4353: Numerical Analysis and Computer Programming 7
Computational Problem Solving
• What is computation?
– What is knowledge?
– Declarative knowledge
• Statements of fact
– Imperative knowledge
• “how to” methods or recipes
06/04/2020 A.J.M. Minhuz Uddin Ahmed, IUT GS 4353: Numerical Analysis and Computer Programming 8
Declarative Knowledge
• “The square root of a number x is a number y
such that y*y = x”
06/04/2020 A.J.M. Minhuz Uddin Ahmed, IUT GS 4353: Numerical Analysis and Computer Programming 9
Imperative Knowledge
• Here is a “recipe” for deducing a square root of a
number x – attributed to Heron of Alexandria in the
first century AD
• Start with a guess, called g
• If g*g is close enough to x, stop and say that g is the answer
• Otherwise make a new guess, by averaging g and x/g
• Using this new guess, repeat the process until we get close
enough
06/04/2020 A.J.M. Minhuz Uddin Ahmed, IUT GS 4353: Numerical Analysis and Computer Programming 10
An Example
• Find the square root of 25
06/04/2020 A.J.M. Minhuz Uddin Ahmed, IUT GS 4353: Numerical Analysis and Computer Programming 11
Algorithms are recipes
1. Put custard mixture over heat
2. Stir
3. Dip spoon in custard
4. Remove spoon and run finger across back of
spoon
5. If clear path is left, remove custard from heat
and let cool
6. Otherwise repeat from step 2
06/04/2020 A.J.M. Minhuz Uddin Ahmed, IUT GS 4353: Numerical Analysis and Computer Programming 12
How do we capture a recipe in a
mechanical process?
• Build a machine to compute square roots
– Fixed Program Computers
• Calculator
• Atanasoff and Berry’s (1941) computer for systems of
linear equations
• Alan Turing’s (1940’s) bombe–decode Enigma codes
• Use a machine that stores and manipulates
instructions
– Stored Program Computer
06/04/2020 A.J.M. Minhuz Uddin Ahmed, IUT GS 4353: Numerical Analysis and Computer Programming 13
Stored program computer
• Sequence of instructions (program) stored inside
computer
– Built from predefined set of primitive instructions
• Arithmetic and logic
• Simple tests
• Moving data
06/04/2020 A.J.M. Minhuz Uddin Ahmed, IUT GS 4353: Numerical Analysis and Computer Programming 14
A basic machine architecture
MEMORY
CONTROL ARITHMATIC
UNIT LOGIC UNIT
Program Counter accumulator
Input Output
06/04/2020 A.J.M. Minhuz Uddin Ahmed, IUT GS 4353: Numerical Analysis and Computer Programming 15
What are the basic primitives?
• Turing showed that using six primitives, can compute
anything
– Turing complete
• Fortunately, modern programming languages have a more
convenient set of primitives
• Also have ways to abstract methods to create new
“primitives”
• But anything computable in one language is computable in
any other programming language
06/04/2020 A.J.M. Minhuz Uddin Ahmed, IUT GS 4353: Numerical Analysis and Computer Programming 16
Creating “Recipes”
• Each programming language provides a set of
primitive operations
06/04/2020 A.J.M. Minhuz Uddin Ahmed, IUT GS 4353: Numerical Analysis and Computer Programming 18
Aspects of languages
• Static semantics – which syntactically valid
strings have a meaning
– English – “I are big” has form <noun>
<intransitive verb> <noun>, so syntactically valid,
but is not valid English because “I” is singular, “are”
is plural
– Programming language – for example, <literal>
<operator> <literal> is a valid syntactic form, but
2.3/’abc’ is a static semantic error
06/04/2020 A.J.M. Minhuz Uddin Ahmed, IUT GS 4353: Numerical Analysis and Computer Programming 19
Aspects of languages
• Semantics – what is the meaning associated with a
syntactically correct string of symbols with no static
semantic errors
06/04/2020 A.J.M. Minhuz Uddin Ahmed, IUT GS 4353: Numerical Analysis and Computer Programming 20
Where can things go wrong?
• Syntactic errors
– Common but easily caught by computer
• Static semantic errors
– Some languages check carefully before running,
others check while interpreting the program
– If not caught, behavior of program unpredictable
• Programs don’t have semantic errors, but
meaning may not be what was intended
– Crashes (stops running)
– Runs forever
– Produces an answer, but not programmer’s
intent
06/04/2020 A.J.M. Minhuz Uddin Ahmed, IUT GS 4353: Numerical Analysis and Computer Programming 21
Our Goal
• Learn the syntax and semantics of a programming
language
06/04/2020 A.J.M. Minhuz Uddin Ahmed, IUT GS 4353: Numerical Analysis and Computer Programming 22
GS 4353
NUMERICAL METHODS AND COMPUTER PROGRAMMING
PROGRAMMING LANGUAGES
06/04/2020 A.J.M. Minhuz Uddin Ahmed, IUT GS 4353: Numerical Analysis and Computer Programming 23
GOAL
- Need a way to describe algorithmic steps such
that computer can use them to execute process
06/04/2020 A.J.M. Minhuz Uddin Ahmed, IUT GS 4353: Numerical Analysis and Computer Programming 24
Options for programming languages
06/04/2020 A.J.M. Minhuz Uddin Ahmed, IUT GS 4353: Numerical Analysis and Computer Programming 25
Options for programming languages
06/04/2020 A.J.M. Minhuz Uddin Ahmed, IUT GS 4353: Numerical Analysis and Computer Programming 26
Options for programming languages
06/04/2020 A.J.M. Minhuz Uddin Ahmed, IUT GS 4353: Numerical Analysis and Computer Programming 28
Python programs
• Program (or script) is a sequence of definitions
and commands
- Definitions evaluated and commands executed by
Python interpreter in a shell
- Can be typed directly into a shell, or stored in a file
that is read into the shell and evaluated
06/04/2020 A.J.M. Minhuz Uddin Ahmed, IUT GS 4353: Numerical Analysis and Computer Programming 29
Objects
• At heart, programs will manipulate data objects
• Objects are:
- Scalar (i.e. cannot be subdivided), or
- Non-scalar (i.e. have internal structure that can be
accessed)
06/04/2020 A.J.M. Minhuz Uddin Ahmed, IUT GS 4353: Numerical Analysis and Computer Programming 30
Scalar Objects
• int – used to represent integers, e.g., 5 or 10082
• float – used to represent real numbers, e.g., 3.14
or 27.0
• bool – used to represent Boolean values True and
False
• The built in Python function type returns the type of
an object
>>> type(3)
<class ‘int’>
>>> type(3.0)
<class ‘float’>
06/04/2020 A.J.M. Minhuz Uddin Ahmed, IUT GS 4353: Numerical Analysis and Computer Programming 31
Expressions
• Objects and operators can be combined to
form expressions, each of which denotes
an object of some type
06/04/2020 A.J.M. Minhuz Uddin Ahmed, IUT GS 4353: Numerical Analysis and Computer Programming 32
Operators on ints and floats
• i + j – sum – if both are ints, result is int, if
either is float, result is float
• i-j – difference
• i*j – product
• i/j – division – if both are ints, result is int,
representing quotient without remainder
• i%j – remainder
• i**j – i raised to the power of j
• i//j – floor division (lower rounding)
06/04/2020 A.J.M. Minhuz Uddin Ahmed, IUT GS 4353: Numerical Analysis and Computer Programming 33
Some simple examples
>>> 3 + 5
8
>>> 3.14 * 20
62.800000000000004
>>> (2 + 3)*4
20
>>> 2 + 3*4
14
06/04/2020 A.J.M. Minhuz Uddin Ahmed, IUT GS 4353: Numerical Analysis and Computer Programming 34
Performing simple operations
• Parentheses define sub‐computations – complete
these to get values before evaluating larger expression
– (2+3)*4
– 5*4
– 20
• Operator precedence:
– In the absence of parentheses (within which expressions are
first reduced), operators are executed left to right, first using
**, then * and /, and then + and -
06/04/2020 A.J.M. Minhuz Uddin Ahmed, IUT GS 4353: Numerical Analysis and Computer Programming 35
Comparison operators on ints and floats
06/04/2020 A.J.M. Minhuz Uddin Ahmed, IUT GS 4353: Numerical Analysis and Computer Programming 36
Operators on bools
• a and b is True if both are True
06/04/2020 A.J.M. Minhuz Uddin Ahmed, IUT GS 4353: Numerical Analysis and Computer Programming 37
Type conversions (type casting)
• We can open convert an object of one
type to another, by using the name of the
type as a function
float(3) has the value of 3.0
int(3.9) truncates to 3
06/04/2020 A.J.M. Minhuz Uddin Ahmed, IUT GS 4353: Numerical Analysis and Computer Programming 38
Simple means of abstractions
• While we can write arbitrary expressions, it is
useful to give names to values of expressions,
and to be able to reuse those names in place of
values
• pi = 3.14159
• radius = 11.2
• area = pi * (radius**2)
06/04/2020 A.J.M. Minhuz Uddin Ahmed, IUT GS 4353: Numerical Analysis and Computer Programming 39
Binding variables and values
• The statement pi = 3.14159
assigns the name pi to the value of
the expression to the right of the =
06/04/2020 A.J.M. Minhuz Uddin Ahmed, IUT GS 4353: Numerical Analysis and Computer Programming 40
Non-scalar objects
• We will see many different kinds of compound
objects
06/04/2020 A.J.M. Minhuz Uddin Ahmed, IUT GS 4353: Numerical Analysis and Computer Programming 41
Operators on string
>>> 3 * ‘a’
‘aaa’
>>> ‘a’ + ‘a’
‘aa’
>>> ‘a’ + str(3)
‘a3’
>>> len(‘abc’)
3
06/04/2020 A.J.M. Minhuz Uddin Ahmed, IUT GS 4353: Numerical Analysis and Computer Programming 42
Extracting parts of strings
• Indexing:
-‘abc’[0] returns the string ‘a’
-‘abc’[2] returns the string ‘c’
-‘abc’[3] is an error (as we cannot go beyond the
boundaries of the string)
-‘abc’[-1] returns the string ‘c’ (essentially counting
backwards from the start of the string)
• Slicing:
- If s is a string, the expression s[start:end]
denotes the substring that starts at start, and ends at
end-1
• ‘abc’[1:3] has the value ‘bc’
06/04/2020 A.J.M. Minhuz Uddin Ahmed, IUT GS 4353: Numerical Analysis and Computer Programming 43
Programs (or scripts)
• While we can type expressions directly to a Python
interpreter (for example using an interface such as
an IDLE shell), in general we will want to include
statements in a program file
06/04/2020 A.J.M. Minhuz Uddin Ahmed, IUT GS 4353: Numerical Analysis and Computer Programming 44
Providing input
• If we are going to write programs or scripts, we
will need a way to incorporate input from a
user.
06/04/2020 A.J.M. Minhuz Uddin Ahmed, IUT GS 4353: Numerical Analysis and Computer Programming 45
A straight line program
Suppose we type the following into a file, and load it into a
Python IDLE window
x = 3
x = x*x # square value of x
print(x)
y = float(input('Enter a number: '))
print(y*y)
06/04/2020 A.J.M. Minhuz Uddin Ahmed, IUT GS 4353: Numerical Analysis and Computer Programming 46
Some observations
• Comments appear after a #
- These are very valuable, as they help a user
understand decisions the programmer has made in
creating the program
- Well commented code should be very readable by a
user
• A straight line program simply executes each
statement in order, with no variation in order
• Most programs require more sophisticated flow
control
06/04/2020 A.J.M. Minhuz Uddin Ahmed, IUT GS 4353: Numerical Analysis and Computer Programming 47
Branching programs
• The simplest branching
statement is a conditional
06/04/2020 A.J.M. Minhuz Uddin Ahmed, IUT GS 4353: Numerical Analysis and Computer Programming 48
A simple program
x = int(input('Enter an integer: '))
if x%2 == 0:
print(‘’)
print('Even')
else:
print(‘’)
print('Odd')
06/04/2020 A.J.M. Minhuz Uddin Ahmed, IUT GS 4353: Numerical Analysis and Computer Programming 49
Some observations
• The expression x%2 == 0 evaluates to True when the
remainder of x divided by 2 is 0
• Note that == is used for comparison, since = is reserved
for assignment
• The indentation is important – each indented set of
expressions denotes a block of instructions
– For example, if the last statement were indented, it would
be executed as part of the else block of code
• Note how this indentation provides a visual structure that
reflects the semantic structure of the program
06/04/2020 A.J.M. Minhuz Uddin Ahmed, IUT GS 4353: Numerical Analysis and Computer Programming 50
We can have nested conditionals
if x%2 == 0:
if x%3 == 0:
print('Divisible by 2 and 3’)
else:
print('Divisible by 2 and not by 3’)
elif x%3 == 0:
print('Divisible by 3 and not by 2’)
else:
print('Divisible by neither 2 nor 3’)
06/04/2020 A.J.M. Minhuz Uddin Ahmed, IUT GS 4353: Numerical Analysis and Computer Programming 51
And we can use compound booleans
06/04/2020 A.J.M. Minhuz Uddin Ahmed, IUT GS 4353: Numerical Analysis and Computer Programming 52
What have we added?
06/04/2020 A.J.M. Minhuz Uddin Ahmed, IUT GS 4353: Numerical Analysis and Computer Programming 53
GS 4353
NUMERICAL METHODS AND COMPUTER PROGRAMMING
06/04/2020 A.J.M. Minhuz Uddin Ahmed, IUT GS 4353: Numerical Analysis and Computer Programming 54
Iteration
Need one more concept to
be able to write programs of
arbitrary complexity
- Start with a test
- If evaluates to True, then
execute loop body once, and
go back to reevaluate the test
- Repeat until test evaluates to
False, after which code
following iteration statement
is executed
06/04/2020 A.J.M. Minhuz Uddin Ahmed, IUT GS 4353: Numerical Analysis and Computer Programming 55
An example
x = 3
ans = 0
itersLeft = x
while (itersLeft != 0):
ans = ans + x
itersLeft = itersLeft – 1
06/04/2020 A.J.M. Minhuz Uddin Ahmed, IUT GS 4353: Numerical Analysis and Computer Programming 56
Stepping through this code
x = 3
x ans itersLeft
ans = 0 itersLeft = x
while (itersLeft != 0):
ans = ans + x
itersLeft = itersLeft – 1
print(str(x) + '*' + str(x) +
' = ' + str(ans))
06/04/2020 A.J.M. Minhuz Uddin Ahmed, IUT GS 4353: Numerical Analysis and Computer Programming 57
Iterative code
• Branching structures (conditionals) let us
jump to different pieces of code based on a
test
- Programs are constant time
• Looping structures (e.g., while) let us repeat
pieces of code until a condition is satisfied
- Programs now take time that depends on
values of variables, as well as length of program
06/04/2020 A.J.M. Minhuz Uddin Ahmed, IUT GS 4353: Numerical Analysis and Computer Programming 58
Classes of algorithms
• Iterative algorithms allow us to do more complex things
than simple arithmetic
06/04/2020 A.J.M. Minhuz Uddin Ahmed, IUT GS 4353: Numerical Analysis and Computer Programming 59
Guess and check
06/04/2020 A.J.M. Minhuz Uddin Ahmed, IUT GS 4353: Numerical Analysis and Computer Programming 60
Finding a cube root of an integer
06/04/2020 A.J.M. Minhuz Uddin Ahmed, IUT GS 4353: Numerical Analysis and Computer Programming 61
Some code
x = int(input('Enter an integer: '))
ans = 0
while ans**3 < x:
ans = ans + 1
if ans**3 != x:
print(str(x)+' is not a perfect cube')
else:
print('Cube root of ' + str(x) + ‘ is ‘
+ str(ans))
06/04/2020 A.J.M. Minhuz Uddin Ahmed, IUT GS 4353: Numerical Analysis and Computer Programming 62
Extending scope
06/04/2020 A.J.M. Minhuz Uddin Ahmed, IUT GS 4353: Numerical Analysis and Computer Programming 63
Some code
x = int(input('Enter an integer: '))
ans = 0
while ans**3 < abs(x):
ans = ans + 1
if ans**3 != abs(x):
print(str(x) + ' is not a perfect cube')
else:
if x < 0:
ans = - ans
print('Cube root of ' + str(x) + ' is ‘
+ str(ans))
06/04/2020 A.J.M. Minhuz Uddin Ahmed, IUT GS 4353: Numerical Analysis and Computer Programming 64
Loop characteristics
• Need a loop variable
- Initialized outside loop
- Changes within loop
- Test for termination depends on variable
06/04/2020 A.J.M. Minhuz Uddin Ahmed, IUT GS 4353: Numerical Analysis and Computer Programming 66
Exhausting enumeration
• Guess and check methods can work on problems
with a finite number of possibilities
06/04/2020 A.J.M. Minhuz Uddin Ahmed, IUT GS 4353: Numerical Analysis and Computer Programming 67
For loops
• While loops generally iterate over a sequence of
choices (ints in cases we have seen)
06/04/2020 A.J.M. Minhuz Uddin Ahmed, IUT GS 4353: Numerical Analysis and Computer Programming 68
For loops
• Identifier bound to first value in sequence
• Code block executed
• Identifier bound to next value
• Code block executed
• Continues until sequence exhausted or a break
statement is executed
• To generate a sequence of integers, use
– range(n) = [0, 1, 2, 3, …, n-1]
– range(m,n) = [m, m+1, …, n-1]
06/04/2020 A.J.M. Minhuz Uddin Ahmed, IUT GS 4353: Numerical Analysis and Computer Programming 69
A cleaned up cube root finder
x = int(input('Enter an integer: '))
for ans in range(0, abs(x)+1):
if ans**3 == abs(x):
break
if ans**3 != abs(x):
print(str(x) + ' is not a perfect cube')
else:
if x < 0:
ans = - ans
print('Cube root of ' + str(x) + ' is
' + str(ans))
06/04/2020 A.J.M. Minhuz Uddin Ahmed, IUT GS 4353: Numerical Analysis and Computer Programming 70
Dealing with floats
• Floats approximate real numbers, but useful to
understand how
• Decimal number: Remember: ** is Python’s
exponentiation operator
– 302 = 3*10**2 + 0*10**1 + 2*10**0
• Binary number
– 10011 = 1*2**4 + 0*2**3 + 0*2**2 + 1*2**1 + 1*2**0
– (which in decimal is 16 + 2 + 1 = 19)
• Internally, computer represents numbers in
binary
06/04/2020 A.J.M. Minhuz Uddin Ahmed, IUT GS 4353: Numerical Analysis and Computer Programming 71
Converting decimal integer to binary
• Consider example of
– x = 1*2**4 + 0*2**3 + 0*2**2 + 1*2**1 + 1*2**0
• If we take remainder relative to 2 (x%2) of this
number, that gives us the last binary bit
• If we then divide x by 2 (x/2), all the bits get
shifted left
– x/2 = 1*2**3 + 0*2**2 + 0*2**1 + 1*2**0 = 1001
• Keep doing successive divisions; now remainder
gets next bit, and so on
• Let’s convert to binary form
06/04/2020 A.J.M. Minhuz Uddin Ahmed, IUT GS 4353: Numerical Analysis and Computer Programming 72
Doing this in Python
num = int(input('Enter a decimal integer: '))
num_new = abs(num)
binary = ''
if num > 0:
while num_new != 0:
binary += str(num_new % 2)
num_new = num_new // 2
print ('binary is ' + binary[::-1])
elif num == 0:
print ('binary is 0')
else:
while num_new != 0:
binary += str(num_new % 2)
num_new = num_new // 2
print ('binary is -' + binary[::-1])
06/04/2020 A.J.M. Minhuz Uddin Ahmed, IUT GS 4353: Numerical Analysis and Computer Programming 73
So what about fractions?
• 3/8 = 0.375 = 3*10**(-1) + 7*10**(-2) + 5*10**(-3)
• First we’ll start with binary as 0. for decimal fraction in
between 0 and 1.
If we multiply by 2 then if the result is less than 1
then add 0 after 0. i.e. 0.0
otherwise
if the result is greater than 1 then add 1 after 0. i.e.
0.1 and subtract 1 from the result to use for next step
• Repeat the same process with the immediate
multiplied result until we get multiplied result exactly
equals to 1
06/04/2020 A.J.M. Minhuz Uddin Ahmed, IUT GS 4353: Numerical Analysis and Computer Programming 74
x = float(input('Enter a decimal number between 0 and 1: '))
result = x
i = ''
while result != 1:
result = result*2
if result > 1:
i += str(1)
result = result-1
else:
i += str(1)
result = result
print('binary is 0.' + i)
06/04/2020 A.J.M. Minhuz Uddin Ahmed, IUT GS 4353: Numerical Analysis and Computer Programming 75
Some implications
• It cannot convert to binary from decimal fraction greater
than 1
06/04/2020 A.J.M. Minhuz Uddin Ahmed, IUT GS 4353: Numerical Analysis and Computer Programming 76
Approximate solutions
• Suppose we now want to find the square root
of any non-negative number?
• Can’t guarantee exact answer, but just look for
something close enough
• Start with exhaustive enumeration
- Take small steps to generate guesses in order
- Check to see if close enough
06/04/2020 A.J.M. Minhuz Uddin Ahmed, IUT GS 4353: Numerical Analysis and Computer Programming 77
Example code
x = 25
epsilon = 0.01
step = epsilon**2
numGuesses = 0
ans = 0.0
while (abs(ans**2 - x)) >= epsilon and ans <= x:
ans += step
numGuesses += 1
print('numGuesses = ' + str(numGuesses))
if abs(ans**2-x) >= epsilon:
print('Failed on square root of ' + str(x))
else:
print(str(ans) + ' is close to the square
root of ' + str(x))
06/04/2020 A.J.M. Minhuz Uddin Ahmed, IUT GS 4353: Numerical Analysis and Computer Programming 78
Some observations
• Step could be any small number
- If too small, takes a long time to find square root
- If make too large, might skip over answer without getting
close enough
• In general, will take x/step times through code to
find solution
• Need a more efficient way to do this
06/04/2020 A.J.M. Minhuz Uddin Ahmed, IUT GS 4353: Numerical Analysis and Computer Programming 79
Bisection search
• We know that the square root of x lies between 0
and x, from mathematics
• Rather than exhaustively trying things starting at
0, suppose instead we pick a number in the
middle of this range
06/04/2020 A.J.M. Minhuz Uddin Ahmed, IUT GS 4353: Numerical Analysis and Computer Programming 80
Bisection search
• If not close enough, is guess too big or too small?
• If g**2 > x, then know g is too big; but now search
• And if this new g is, for example, g**2 < x, then know too
small; so now search
06/04/2020 A.J.M. Minhuz Uddin Ahmed, IUT GS 4353: Numerical Analysis and Computer Programming 81
Example of square root
x = 25
epsilon = 0.01
numGuesses = 0
low = 0.0
high = x
ans = (high + low)/2.0
while abs(ans**2 - x) >= epsilon:
print('low = ' + str(low) + ' high = ' + str(high) + '
ans = ' + str(ans))
numGuesses += 1
if ans**2 < x:
low = ans
else:
high = ans
ans = (high + low)/2.0
print('numGuesses = ' + str(numGuesses))
print(str(ans) + ' is close to square root of ' + str(x))
06/04/2020 A.J.M. Minhuz Uddin Ahmed, IUT GS 4353: Numerical Analysis and Computer Programming 82
Some observations
• Bisection search radically reduces computation
time – being smart about generating guesses is
important
• Should work well on problems with “ordering”
property – value of function being solved varies
monotonically with input value
– Here function is g**2; which grows as g grows
06/04/2020 A.J.M. Minhuz Uddin Ahmed, IUT GS 4353: Numerical Analysis and Computer Programming 83
Newton-Raphson
General approximation algorithm to find roots of a
polynomial in one variable
06/04/2020 A.J.M. Minhuz Uddin Ahmed, IUT GS 4353: Numerical Analysis and Computer Programming 85
Newton-Raphson
• This gives us another way of generating guesses,
which we can check; very efficient
epsilon = 0.01
y = 24.0
guess = y/2.0
while abs(guess*guess - y) >= epsilon:
guess = guess - (((guess**2) - y)/(2*guess))
print(‘Square root of ’ + str(y) + ‘is about ’ +
str(guess))
06/04/2020 A.J.M. Minhuz Uddin Ahmed, IUT GS 4353: Numerical Analysis and Computer Programming 86
Iterative algorithms
• Guess and check methods build on reusing same
code
- Use a looping construct to generate guesses, then
check and continue
• Generating guesses
- Exhaustive enumeration
- Bisection search
- Newton-Raphson (for root finding)
06/04/2020 A.J.M. Minhuz Uddin Ahmed, IUT GS 4353: Numerical Analysis and Computer Programming 87
GS 4353
NUMERICAL METHODS AND COMPUTER PROGRAMMING
06/04/2020 A.J.M. Minhuz Uddin Ahmed, IUT GS 4353: Numerical Analysis and Computer Programming 88
Functions
• So far, have seen numbers, assignments, input/
output, comparisons, looping constructs
- Sufficient to be Turing Complete
• But code lacks abstraction
- Have to reload file every time want to use
- Can’t use same variable names in other pieces of code
- Can quickly get cumbersome to read and maintain
06/04/2020 A.J.M. Minhuz Uddin Ahmed, IUT GS 4353: Numerical Analysis and Computer Programming 89
A simple example
• Suppose we want to z to be the maximum of two
numbers, x and y
if x > y:
z=x
else:
z=y
06/04/2020 A.J.M. Minhuz Uddin Ahmed, IUT GS 4353: Numerical Analysis and Computer Programming 90
Capturing Computation as a function
• Idea is to encapsulate this computation within a scope
such that can treat as primitive
- Use by simply calling name, and providing input
- Internal details hidden from users
• Syntax
def <function name> (<formal parameters>):
<function body>
• def is a keyword
• Name is any legal Python name
• Within parenthesis are zero or more formal parameters
each is a variable name to be used inside function body
06/04/2020 A.J.M. Minhuz Uddin Ahmed, IUT GS 4353: Numerical Analysis and Computer Programming 91
A simple example
def max(x,y):
if x > y:
return x
else:
return y
06/04/2020 A.J.M. Minhuz Uddin Ahmed, IUT GS 4353: Numerical Analysis and Computer Programming 92
Function returns
• Body can consist of any number of legal Python
expressions
06/04/2020 A.J.M. Minhuz Uddin Ahmed, IUT GS 4353: Numerical Analysis and Computer Programming 93
Summary of function call
1. Expressions for each parameter are evaluated,
bound to formal parameter names of function
2. Control transfers to first expression in body of
function
3. Body expressions executed until return keyword
reached (returning value of next expression) or run
out of expressions (returning None)
4. Invocation is bound to the returned value
5. Control transfers to next piece of code
06/04/2020 A.J.M. Minhuz Uddin Ahmed, IUT GS 4353: Numerical Analysis and Computer Programming 94
Environments to understand bindings
06/04/2020 A.J.M. Minhuz Uddin Ahmed, IUT GS 4353: Numerical Analysis and Computer Programming 95
When we call a function
• Want to evaluate <expr0>(<expr1>, …, <exprn>)
06/04/2020 A.J.M. Minhuz Uddin Ahmed, IUT GS 4353: Numerical Analysis and Computer Programming 96
When we call the function
x=5
y=3
06/04/2020 A.J.M. Minhuz Uddin Ahmed, IUT GS 4353: Numerical Analysis and Computer Programming 97
Another simple example
• Suppose we want to compute powers of a
number by successive multiplication
result = 1
06/04/2020 A.J.M. Minhuz Uddin Ahmed, IUT GS 4353: Numerical Analysis and Computer Programming 99
Let’s Define our Procedure
def iterativePower(x, p):
result = 1
for turn in range(p):
print ('iteration: ' + str(turn)\
+'current result: '+ str(result))
result = result * x
return result
06/04/2020 A.J.M. Minhuz Uddin Ahmed, IUT GS 4353: Numerical Analysis and Computer Programming 100
And this creates
06/04/2020 A.J.M. Minhuz Uddin Ahmed, IUT GS 4353: Numerical Analysis and Computer Programming 101
Example
• Call iterativePower(2, 5)
06/04/2020 A.J.M. Minhuz Uddin Ahmed, IUT GS 4353: Numerical Analysis and Computer Programming 102
Example
• and for loop rebinds local variable until exit, when return
statement returns value of result
06/04/2020 A.J.M. Minhuz Uddin Ahmed, IUT GS 4353: Numerical Analysis and Computer Programming 103
Scoping is Local
Imagine we had evaluated
x = 3
p = 4
print(iterativePower(2, 5))
• Then our local environment would have separate
bindings for x and p, which would not be visible
to the environment of the function call
06/04/2020 A.J.M. Minhuz Uddin Ahmed, IUT GS 4353: Numerical Analysis and Computer Programming 104
Example
• Evaluating body of procedure initially causes…
06/04/2020 A.J.M. Minhuz Uddin Ahmed, IUT GS 4353: Numerical Analysis and Computer Programming 105
Example
• But now evaluation of body only sees bindings in E2
06/04/2020 A.J.M. Minhuz Uddin Ahmed, IUT GS 4353: Numerical Analysis and Computer Programming 106
Another Example
• Causes the following to
def f(x):
y = 1 appear in the Python
x = x + y shell
print(‘x = ’ + str(x))
return x
x = 4
x = 3
y = 2 z = 4
z = f(x) x = 3
print(‘z = ’ + str(z))
print(‘x = ’ + str(x)) y = 2
print(‘y = ’ + str(y))
06/04/2020 A.J.M. Minhuz Uddin Ahmed, IUT GS 4353: Numerical Analysis and Computer Programming 107
Let’s see why
f Procedure2
(x)
def f(x): x 3
y=1
y = 1 y 2 x=x+y
x = x + y print …
print(’x = ’ + str(x)) return x
return x
x = 3
y = 2
z = f(x)
print(’z = ’ + str(z))
print(’x = ’ + str(x))
print(’y = ’ + str(y))
06/04/2020 A.J.M. Minhuz Uddin Ahmed, IUT GS 4353: Numerical Analysis and Computer Programming 108
Let’s see why
f Procedure2
(x)
def f(x): x 3
y=1
y = 1 y 2 x=x+y
x = x + y print …
print(’x = ’ + str(x)) return x
return x
x = 3 x 3
y = 2
z = f(x)
print(’z = ’ + str(z))
print(’x = ’ + str(x))
print(’y = ’ + str(y))
06/04/2020 A.J.M. Minhuz Uddin Ahmed, IUT GS 4353: Numerical Analysis and Computer Programming 109
Let’s see why
f Procedure2
(x)
def f(x): x 3
y=1
y = 1 y 2 x=x+y
x = x + y print …
print(’x = ’ + str(x)) return x
return x
x = 3 x 3
y = 2 y 1
z = f(x)
print(’z = ’ + str(z))
print(’x = ’ + str(x))
print(’y = ’ + str(y))
06/04/2020 A.J.M. Minhuz Uddin Ahmed, IUT GS 4353: Numerical Analysis and Computer Programming 110
Let’s see why
f Procedure2
(x)
def f(x): x 3
y=1
y = 1 y 2 x=x+y
x = x + y print …
print(’x = ’ + str(x)) return x
return x
x = 3 x 4
y = 2 y 1
z = f(x)
print(’z = ’ + str(z))
print(’x = ’ + str(x))
print(’y = ’ + str(y))
06/04/2020 A.J.M. Minhuz Uddin Ahmed, IUT GS 4353: Numerical Analysis and Computer Programming 111
Let’s see why
f Procedure2
(x)
def f(x): x 3
y=1
y = 1 y 2 x=x+y
x = x + y print …
z 4
print(’x = ’ + str(x)) return x
return x
x = 3 x 4
y = 2 y 1
z = f(x)
print(’z = ’ + str(z))
print(’x = ’ + str(x))
print(’y = ’ + str(y))
06/04/2020 A.J.M. Minhuz Uddin Ahmed, IUT GS 4353: Numerical Analysis and Computer Programming 112
Let’s see why
f Procedure2
(x)
def f(x): x 3
y=1
y = 1 y 2 x=x+y
x = x + y print …
z 4
print(’x = ’ + str(x)) return x
return x
x = 3
y = 2
Now control reverts to the
z = f(x) global environment, where
print(’z = ’ + str(z)) the values of x, y and z are
print(’x = ’ + str(x))
print(’y = ’ + str(y)) visible
06/04/2020 A.J.M. Minhuz Uddin Ahmed, IUT GS 4353: Numerical Analysis and Computer Programming 113
Some observations
• Each function call creates a new environment,
which scopes bindings of formal parameters and
values, and of local variables (those created with
assignments within body)
06/04/2020 A.J.M. Minhuz Uddin Ahmed, IUT GS 4353: Numerical Analysis and Computer Programming 114
Another example
def square(x):
return x*x
def twoPower(x, n):
while n > 1:
x = square(x)
n = n/2
return x
06/04/2020 A.J.M. Minhuz Uddin Ahmed, IUT GS 4353: Numerical Analysis and Computer Programming 115
Let’s try it out
square
Procedure3
def square(x): twoPower (x)
return x*x
return x*x x 5
def twoPower(x, n): n 1
while n > 1: Procedure4
x = square(x) (x,n)
n = n/2 while n>1:
x = square(x)
return x n = n/2
return x
x = 5
n = 1
print(twoPower(2,8))
06/04/2020 A.J.M. Minhuz Uddin Ahmed, IUT GS 4353: Numerical Analysis and Computer Programming 116
Let’s try it out
square
Procedure3
def square(x): twoPower (x)
return x*x
return x*x x 5
def twoPower(x, n): n 1
while n > 1: Procedure4
x = square(x) (x,n)
n = n/2 while n>1:
x = square(x)
return x n = n/2
x 2
return x
x = 5 n 8
n = 1
print(twoPower(2,8))
06/04/2020 A.J.M. Minhuz Uddin Ahmed, IUT GS 4353: Numerical Analysis and Computer Programming 117
Let’s try it out
square
Procedure3
def square(x): twoPower (x)
return x*x
return x*x x 5
def twoPower(x, n): n 1
while n > 1: Procedure4
x = square(x) (x,n)
n = n/2 while n>1:
x = square(x)
return x n = n/2
x 2
return x
x = 5 n 8
n = 1
x 2
print(twoPower(2,8))
06/04/2020 A.J.M. Minhuz Uddin Ahmed, IUT GS 4353: Numerical Analysis and Computer Programming 118
Let’s try it out
square
Procedure3
def square(x): twoPower (x)
return x*x
return x*x x 5
def twoPower(x, n): n 1
while n > 1: Procedure4
x = square(x) (x,n)
n = n/2 while n>1:
x = square(x)
return x n = n/2
x 4
return x
x = 5 n 4
n = 1
x 2
print(twoPower(2,8))
06/04/2020 A.J.M. Minhuz Uddin Ahmed, IUT GS 4353: Numerical Analysis and Computer Programming 119
Let’s try it out
square
Procedure3
def square(x): twoPower (x)
return x*x
return x*x x 5
def twoPower(x, n): n 1
while n > 1: Procedure4
x = square(x) (x,n)
n = n/2 while n>1:
x = square(x)
return x n = n/2
x 4
return x
x = 5 n 4
n = 1
x 4
print(twoPower(2,8))
06/04/2020 A.J.M. Minhuz Uddin Ahmed, IUT GS 4353: Numerical Analysis and Computer Programming 120
Let’s try it out
square
Procedure3
def square(x): twoPower (x)
return x*x
return x*x x 5
def twoPower(x, n): n 1
while n > 1: Procedure4
x = square(x) (x,n)
n = n/2 while n>1:
x = square(x)
return x n = n/2
x 16
return x
x = 5 n 2
n = 1
x 4
print(twoPower(2,8))
06/04/2020 A.J.M. Minhuz Uddin Ahmed, IUT GS 4353: Numerical Analysis and Computer Programming 121
Let’s try it out
square
Procedure3
def square(x): twoPower (x)
return x*x
return x*x x 5
def twoPower(x, n): n 1
while n > 1: Procedure4
x = square(x) (x,n)
n = n/2 while n>1:
x = square(x)
return x n = n/2
x 16
return x
x = 5 n 2
n = 1
x 16
print(twoPower(2,8))
06/04/2020 A.J.M. Minhuz Uddin Ahmed, IUT GS 4353: Numerical Analysis and Computer Programming 122
Let’s try it out
square
Procedure3
def square(x): twoPower (x)
return x*x
return x*x x 5
def twoPower(x, n): n 1
while n > 1: Procedure4
x = square(x) (x,n)
n = n/2 while n>1:
x = square(x)
return x n = n/2
x 256
return x
x = 5 n 1
n = 1
x 16
print(twoPower(2,8))
06/04/2020 A.J.M. Minhuz Uddin Ahmed, IUT GS 4353: Numerical Analysis and Computer Programming 123
Let’s try it out
square
Procedure3
def square(x): twoPower (x)
return x*x
return x*x x 5
def twoPower(x, n): n 1
while n > 1: Procedure4
x = square(x) (x,n)
n = n/2 while n>1:
x = square(x)
return x n = n/2
x 256
return x
x = 5 n 1
n = 1
print(twoPower(2,8))
256
06/04/2020 A.J.M. Minhuz Uddin Ahmed, IUT GS 4353: Numerical Analysis and Computer Programming 124
Some observations
• Notice how each call to square created a new
frame, with a local binding for x
• The value of x in the global environment was
never confused with values within frames from
function calls
• The value of x used by the call to square is
different from the binding for x in the call to
twoPower
• The rules we described can be followed
mechanically to determine scoping of variables
06/04/2020 A.J.M. Minhuz Uddin Ahmed, IUT GS 4353: Numerical Analysis and Computer Programming 125
Capturing a more interesting example
def findRoot1(x, power, epsilon):
low = 0
high = x
ans = (high+low)/2.0
while abs(ans**power - x) > epsilon:
if ans**power < x:
low = ans
else: findRoot1(25.0, 2, .001)
high = ans 4.99992370605
ans = (high+low)/2.0 findRoot1(27.0, 3, .001)
return ans 2.99998855591
findRoot1(-27.0, 3, .001)
06/04/2020 A.J.M. Minhuz Uddin Ahmed, IUT GS 4353: Numerical Analysis and Computer Programming 129
Capturing a more interesting example
def findRoot3(x, power, epsilon):
if x < 0 and power%2 == 0:
return None
# can't find even powered root of negative number
low = min(-1, x)
high = max(1, x)
ans = (high+low)/2.0
while abs(ans**power - x) > epsilon:
if ans**power < x:
low = ans findRoot3(25.0, 2, .001)
else: 4.99992370605
high = ans findRoot3(27.0, 3, .001)
ans = (high+low)/2.0 2.99998855591
return ans findRoot3(-27.0, 3, .001)
-2.99998855591
findRoot3(0.25, 2, .001)
0.5
findRoot3(0.25, 2, .001)
-0.5
06/04/2020 A.J.M. Minhuz Uddin Ahmed, IUT GS 4353: Numerical Analysis and Computer Programming 130
Adding a specification
def findRoot3(x, power, epsilon):
“““x and epsilon int or float, power an int
epsilon > 0 & power >=1
returns a float y s.t. y**power is within epsilon of x.
If such a float does not exist, it returns None”””
if x < 0 and power%2 == 0:
return None
# can't find even powered root of negative number
low = min(-1, x)
high = max(1, x)
ans = (high+low)/2.0
while abs(ans**power - x) > epsilon:
if ans**power < x:
low = ans
else:
high = ans
ans = (high+low)/2.0
return ans
06/04/2020 A.J.M. Minhuz Uddin Ahmed, IUT GS 4353: Numerical Analysis and Computer Programming 131
Specifications
• Are contract between implementer of function
and user
06/04/2020 A.J.M. Minhuz Uddin Ahmed, IUT GS 4353: Numerical Analysis and Computer Programming 132
Functions close the loop
• Can now create new procedures and treat as
if Python primitives
• Properties
- Decomposition: Break problems into modules
that are self-contained, and can be reused in
other settings
06/04/2020 A.J.M. Minhuz Uddin Ahmed, IUT GS 4353: Numerical Analysis and Computer Programming 134
Example
Be sure to save this code in
pi = 3.14159 a file called circle.py
def area(radius):
return pi*(radius**2)
def circumference(radius):
return 2*pi*radius
def sphereSurface(radius):
return 4.0*area(radius)
def sphereVolume(radius):
return (4.0/3.0)*pi*(radius**3)
06/04/2020 A.J.M. Minhuz Uddin Ahmed, IUT GS 4353: Numerical Analysis and Computer Programming 135
Example
import circle
pi = 3.0
print (pi)
print (circle.pi)
print (circle.area(3))
print (circle.circumference(3))
06/04/2020 A.J.M. Minhuz Uddin Ahmed, IUT GS 4353: Numerical Analysis and Computer Programming 137
GS 4353
NUMERICAL METHODS AND COMPUTER PROGRAMMING
Reference:
Introduction to Computation and Programming Using Python
by
John V. Guttag
06/04/2020 A.J.M. Minhuz Uddin Ahmed, IUT GS 4353: Numerical Analysis and Computer Programming 138