Sei sulla pagina 1di 138

GS 4353

NUMERICAL ANALYSIS
AND COMPUTER
PROGRAMMING

Abu Jar Md. Minhuz Uddin Ahmed


Lecturer
Department of CEE, IUT
GS 4353
NUMERICAL METHODS 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

• Why need this knowledge?

– Become skillful at making a computer do what you


want it to do

– Learn computational modes of thinking

– Master the art of computational problem solving

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

100s of gigabytes of storage

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”

• Can you use this to find the square root of a


particular instance of 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

g g*g x/g ½(g+x/g)

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

• Special program (interpreter) executes each instruction in


order
– Use tests to change flow of control through sequence, to stop
when done

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

• Each programming language provides mechanisms


for combining primitives to form more complex,
but legal, expressions

• Each programming language provides mechanisms


for deducing meanings or values associated with
computations or expressions
06/04/2020 A.J.M. Minhuz Uddin Ahmed, IUT GS 4353: Numerical Analysis and Computer Programming 17
Aspects of languages
• Primitive constructs
– Programming language – numbers, strings, simple
operators
– English – words

• Syntax – which strings of characters and symbols


are well-formed
– Programming language – we’ll get to specifics shortly, but
for example 3.2 + 3.2 is a valid Python expression
– English – “cat dog boy” is not syntactically valid, as not in
form of acceptable sentence

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

– English – can be ambiguous


• “I cannot praise this student too highly”

– Programming languages – always has exactly one


meaning
• But meaning (or value) may not be what programmer
intended

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

• Learn how to use those elements to translate


“recipes” for solving a problem into a form that the
computer can use to do the work for us

• Computational modes of thought enable us to use


a suite of methods to solve problems

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

- Programming language defines syntax and


semantics needed to translate our
computational ideas into mechanical steps

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

Low level language uses instructions similar to internal control


unit:
- Move data from one location to another
- Execute a simple ALU operation
- Jump to new point in sequence based on test
Checker confirms syntax, static semantics correct
Interpreter just follows sequence of simple instructions

06/04/2020 A.J.M. Minhuz Uddin Ahmed, IUT GS 4353: Numerical Analysis and Computer Programming 26
Options for programming languages

- A high level language uses more abstract terms


invert a matrix, compute a function
- In a compiled language, those abstractions are
converted back into low level instructions, then
executed
06/04/2020 A.J.M. Minhuz Uddin Ahmed, IUT GS 4353: Numerical Analysis and Computer Programming 27
Options for programming languages

• In an interpreted language, special program


converts source code to internal data structure,
then interpreter sequentially converts each step
into low level machine instruction and executes

• We are going to use Python, which belongs to


this class of 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

• Command (or statement) instructs interpreter to


do something

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

• Each object has a type that defines the kinds of


things programs can do to it

• 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

• The syntax for most simple expressions is:


– <object> <operator> <object>

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

• i>j – returns True if strictly greater than


• i>=j – returns True if greater than or equal
• i<j – returns True if strictly less than
• i<=j – returns True if less than or equal
• i==j – returns True if equal
• i!= j – returns True if not equal

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

• a or b is True if at least one is True

• not a is True if a is False; it is False if a is


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 =

• Think of each assignment statement


as creating a binding between a
name and a value stored
somewhere in the computer

• We can retrieve the value associated


with a name or variable by simply
invoking that name, e.g., pi

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

• The simplest of these are strings, objects of type


str

• Literals of type string can be written using single or


double quotes
‘abc’
“abc”
‘123’ – this is a string of characters, not the number

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

• Executing an expression from a script will not


produce any output; for that we need statements
(not expressions), such as
– print(‘ab’)
– print(‘3’*3)

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.

• We use the Python function input, as in:


>>> name = input(‘Enter your name: ’)
Enter your name: Eric Grimson
>>> print(‘Are you ’ + name + ‘?’)
Are you Eric Grimson?

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)

Then we observe the following behavior (where I type a 4


below)
9
Enter a number: 4
16.0

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

- A test (expression that


evaluates to True or False)
- A block of code to execute if
the test is True
- An optional block of code to
execute if the test is False

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')

print('Done with conditional')

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

if x < y and x < z:


print('x is least’)
elif y < z:
print('y is least’)
else:
print('z is least’)

06/04/2020 A.J.M. Minhuz Uddin Ahmed, IUT GS 4353: Numerical Analysis and Computer Programming 52
What have we added?

• Branching programs allow us to make choices and


do different things
• But still the case that at most, each statement
gets executed once.
• So maximum time to run the program depends
only on the length of the program
• These programs run in constant time

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

SOME SIMPLE NUMERICAL


PROGRAMS

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

print(str(x) + '*' + str(x) + ' = ' + str(ans))


This code squares the value of x by repetitive addition.

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))

Some properties of iteration loops:


• need to set an iteration variable outside the loop
• need to test that variable to determine when done
• need to change that variable within the loop, in
addition to other work

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

• We can repeat a sequence of steps multiple times based


on some decision; leads to new classes of algorithms

• One useful example are “guess and check” methods

06/04/2020 A.J.M. Minhuz Uddin Ahmed, IUT GS 4353: Numerical Analysis and Computer Programming 59
Guess and check

• Remember our “declarative” definition of square root of


x

• If we could guess possible values for square root (call it


g), then can use definition to check if g*g = x

• We just need a good way to generate guesses

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

• One way to use this idea of generating guesses in


order to find a cube root of x is to first try
0**3, then 1**3, then 2**3, and so on

• Can stop when reach k such that k**3 > x

• Only a finite number of cases to try

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

• Only works for positive integers


• Easy to fix by keeping track of sign, looking for
solution to positive case

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

• Useful to think about a decrementing function


- Maps set of program variables into an integer
- When loop is entered, value is non-­negative
- When value is <= 0, loop terminates, and
- Value is decreased every time through loop

• Here we use abs(x) – ans**3


06/04/2020 A.J.M. Minhuz Uddin Ahmed, IUT GS 4353: Numerical Analysis and Computer Programming 65
What happens if we miss a condition?

• Suppose we don’t initialize the variable?

• Suppose we don’t change the variable inside the


loop?

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

• Exhaustive enumeration is a good way to


generate guesses in an organized manner

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)

• Python has a specialized mechanism for this case, called a


for loop
for <identifier> in <sequence>:
<code block>

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

elif result < 1:


i += str(0)
result = result

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

• It cannot convert to binary from negative decimal


fraction

• Combination of the previous code of integer decimal and


code of fraction decimal of now can be used for making
it universal

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

• If we are lucky, this answer is close enough

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

• At each stage, reduce range of values to search by half

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

Want to find r such that p(r) = 0


For example, to find the square root of 24, find the root of
p(x) =
Newton showed that if g is an approximation to the root,
then
g – p(g)/p’(g)
is a better approximation; where p’ is derivative of p
06/04/2020 A.J.M. Minhuz Uddin Ahmed, IUT GS 4353: Numerical Analysis and Computer Programming 84
Newton-Raphson
 
Simple case:
First derivative: 2cx
So if polynomial is , then derivative is 2x
Newton-Raphson says given a guess g for root, a
better guess is

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

FUNCTIONS, SCOPING AND


SPECIFICATIONS

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

• Functions give us abstraction – allow us to capture


computation and treat as if primitive

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

• A simple script would be

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

We can then invoke by


z = max(3, 4)
When we call or invoke max(3, 4), x is bound to 3, y is bound to 4,
and then body expression(s) are evaluated

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

• Expressions are evaluated until


- Run out of expressions, in which case special value
None is returned
- Or until special keyword return is reached, in which case
subsequent expression is evaluated and that value is
returned as value of function call

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

• Environments are formalism for tracking bindings


of variables and values
• Assignments pair name and value in environment
• Asking for value of name just looks up in current
environment
• Python shell is default (or global) environment
• Definitions pair function name with details of
function

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>)

• First evaluate <expr0>, which looks up procedure object in


environment

• Then evaluate each of the other <expri> to get values of


parameters

• Bind parameter names in procedure object to values of arguments


in a new frame, which has as a parent; the environment in which
procedure was defined

• Evaluate body of procedure relative to this new frame

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

def max(x, y):


if x > y:
return x
else:
return y
z = max(3, 4)

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

• Idea would be to keep track of number of


multiplications, plus intermediate product

• Stop when have multiplied number x by


itself p times, and return final product

• Here is simple code


06/04/2020 A.J.M. Minhuz Uddin Ahmed, IUT GS 4353: Numerical Analysis and Computer Programming 98
Computing Powers

x = float(input('Enter a number: '))


p = int(input('Enter an integer power: '))

result = 1

for turn in range(p):


result = result * x
print('iteration: ' + str(turn) + \
'current result: ' + str(result))

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)

• Scoping often called static or lexical because


scope within which variable has value is defined
by extent of code boundaries

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)

Why does this fail on the


third example?
06/04/2020 A.J.M. Minhuz Uddin Ahmed, IUT GS 4353: Numerical Analysis and Computer Programming 126
Capturing a more interesting example
def findRoot2(x, power, epsilon):
if x < 0 and power%2 == 0:
return None
# can't find even powered root of negative number
low = min(0, x)
high = max(0, x)
ans = (high+low)/2.0
while abs(ans**power - x) > epsilon:
if ans**power < x:
low = ans findRoot2(25.0, 2, .001)
4.99992370605
else:
high = ans findRoot2(27.0, 3, .001)
ans = (high+low)/2.0 2.99998855591
return ans findRoot2(-27.0, 3, .001)
-2.99998855591
06/04/2020 A.J.M. Minhuz Uddin Ahmed, IUT GS 4353: Numerical Analysis and Computer Programming 127
Capturing a more interesting example
def findRoot2(x, power, epsilon):
if x < 0 and power%2 == 0:
return None
# can't find even powered root of negative number
low = min(0, x)
high = max(0, x)
ans = (high+low)/2.0
while abs(ans**power - x) > epsilon:
if ans**power < x: findRoot2(25.0, 2, .001)
low = ans 4.99992370605
else: findRoot2(27.0, 3, .001)
high = ans 2.99998855591
ans = (high+low)/2.0 findRoot2(-27.0, 3, .001)
return ans -2.99998855591
findRoot2(0.25, 2, .001)
Why does this fail on the
fourth example?
06/04/2020 A.J.M. Minhuz Uddin Ahmed, IUT GS 4353: Numerical Analysis and Computer Programming 128
Think about our bisection search
• When we call with a fractional argument, like.
25, we are searching

• Which means our first guess will be the average,


or .125
– Our original idea used the fact that the root of x was
between 0 and x, but when x is fractional, the root is
between x and 1

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

₋ Assumptions: conditions that must be met by users of


function. Typically constraints on parameters, such as
type, and sometimes acceptable ranges of values

₋ Guarantees: Conditions that must be met by function,


provided that it has been called in way that satisfies
assumptions

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

- Abstraction: Hide details. User need not know


interior details, can just use as if a black box.
06/04/2020 A.J.M. Minhuz Uddin Ahmed, IUT GS 4353: Numerical Analysis and Computer Programming 133
Using functions in modules

• Modularity suggests grouping functions together


that share common theme
• Place in a single .py file
• Use import command to access

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))

• Will result in 3.0


# value from local env 3.14159
# value from file 28.27431
# uses values from file 18.84954

• The . notation specifies


context from which to read
values
06/04/2020 A.J.M. Minhuz Uddin Ahmed, IUT GS 4353: Numerical Analysis and Computer Programming 136
Example
from circle import *
pi = 0.0
print (pi)
print (area(3))
print (circumference(3))

• Will result in 0.0


# value from local scope 28.27431
# uses values from file, because area not bound locally, but
inherits from circle; however format allows reference as if in
local scope 18.84954

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

Potrebbero piacerti anche