Warming-Up Exercises: Training Assignment 3 2015-04-09 NUMA22: Computational Mathematics
Warming-Up Exercises: Training Assignment 3 2015-04-09 NUMA22: Computational Mathematics
Warming-Up Exercises: Training Assignment 3 2015-04-09 NUMA22: Computational Mathematics
The purpose of this training assignment is to repeat list comprehensions, to give some
examples for slicing, work more with functions and investigate sets in Python.
This assignment has 8 tasks.
Warming-up Exercises
You should not run the code before you have written down what it will do when executed.
Task 1
Task 2
# and this?
print(f)
1
# what about:
y = 2*pi
print(f(y))
Task 3
Can you write another function which has the same output?
What happens if you instead use integer division? That is, replacing in your code
/ by //.
Programming Exercises
Task 4
Let a distance table between some villages be given by the following data
0 20 30 40
20 0 50 60
30 50 0 70
40 60 70 0
Construct a list (of lists) which contains this data. Call this list distance.
Construct from this list a list reddistance which contains only the relevant
data in the following form
20
30 50
40 60 70
Use slicing for this task. Solve this task in two ways:
1. by using for-loops
2. by using only list comprehension and slicing (no for loops at all)
Task 5
2
Task 6
Study also other operations on sets. You find a complete list of those by using
the “tab”-key in Spyder / IPython after A., where A is a previously defined set in
Python. In particular study the method update and intersection_update.
Explain what the difference between the two commands intersection and
intersection_update is.
Task 7
Test in Python the statement that the empty set is a subset of any set.
3
Bisection Method
A continuous function which changes its sign in an interval [a, b] has at least
one root in this interval. Such a root can be found by the bisection method.
This method starts from the given interval. Then it investigates the sign
changes in the subintervals [a, a+b a+b
2 ] and [ 2 , b]. If the sign changes in the
first subinterval, b is redefined to be
a+b
b :=
2
otherwise a is redefined in the same manner to be
a+b
a :=
2
and the process is repeated until b − a is less than a given tolerance.
Note, a sign change is characterized by the condition
Task 8
Task 9
In the next Exercise we will work more with functions. A nice thing in Python is
that functions are objects like everything else, and can be passed to other functions
as input. Try changing your bisec method so that it also has f as an input
argument, i.e. the first line could look like
def bisec(f, interval, tol):