Reader small image

You're reading from  Scientific Computing with Python - Second Edition

Product typeBook
Published inJul 2021
Reading LevelIntermediate
PublisherPackt
ISBN-139781838822323
Edition2nd Edition
Languages
Right arrow
Authors (3):
Claus Führer
Claus Führer
author image
Claus Führer

Claus Führer is a professor of scientific computations at Lund University, Sweden. He has an extensive teaching record that includes intensive programming courses in numerical analysis and engineering mathematics across various levels in many different countries and teaching environments. Claus also develops numerical software in research collaboration with industry and received Lund University's Faculty of Engineering Best Teacher Award in 2016.
Read more about Claus Führer

View More author details
Right arrow

2.6 Exercises

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')
  1. Since the sequence does not converge, the code should print the
     No convergence message. Execute it to see what happens.
  1. 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.

  1. What happens if you replace u=1.0 with u=1 (without a decimal point)? Run the code to check your predictions.
  2. 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 Falseor 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:

Figure 2.3: A half adder circuit

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

Figure 2.4: A full adder circuit

Write a function that implements a half adder and another that implements a full adder. Test these functions.

Previous PageNext Chapter
You have been reading a chapter from
Scientific Computing with Python - Second Edition
Published in: Jul 2021Publisher: PacktISBN-13: 9781838822323
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Authors (3)

author image
Claus Führer

Claus Führer is a professor of scientific computations at Lund University, Sweden. He has an extensive teaching record that includes intensive programming courses in numerical analysis and engineering mathematics across various levels in many different countries and teaching environments. Claus also develops numerical software in research collaboration with industry and received Lund University's Faculty of Engineering Best Teacher Award in 2016.
Read more about Claus Führer