Ex. 1: Check whether is a zero of the function:
Ex. 2: According to de Moivre's formula, the following holds:
Choose numbers n and x and verify that formula in Python.
Ex. 3: Complex numbers. Verify Euler's formula in the same way:
Ex. 4: Suppose we are trying to check the convergence of a diverging sequence (here, the sequence is defined by the recursive relation and ):
u = 1.0 # you have to use a float here!
uold = 10.
for iteration in range(2000):
if not abs(u-uold) > 1.e-8:
print('Convergence')
break # sequence has converged
uold = u
u = 2*u
else:
print('No convergence')
- Since the sequence does not converge, the code should print the
No convergence message. Execute it to see what happens.
- What happens if you replace the line
if not abs(u-uold) > 1.e-8:
with
if abs(u-uold) < 1.e-8:
It should give exactly the same result, shouldn't it? Run the code again to see what happens.
- What happens if you replace u=1.0 with u=1 (without a decimal point)? Run the code to check your predictions.
- Explain the unexpected behavior of this code.
Ex. 5: An implication C = (A ⇒ B) is a Boolean expression that is defined as
- C is True if A is False, or A and B are both True
- C is False otherwise
Write a Python function implication(A, B).
Ex. 6: This exercise is to train Boolean operations. Two binary digits (bits) are added by using a logical device called a half adder. It produces a carry bit (the digit of the next higher value) and the sum as defined by the following table, and half adder circuit:
p
|
q
|
sum
|
carry
|
1
|
1
|
0
|
1
|
1
|
0
|
1
|
0
|
0
|
1
|
1
|
0
|
0
|
0
|
0
|
0
|
Definition of the half adder operation:
A full adder consists of two half adders and sums two bits and an additional carry bit on the input (see also the following figure):
Write a function that implements a half adder and another that implements a full adder. Test these functions.