Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: THEend8_
Preface
This document contains a number of programming exercises made for the course
IN1900. The chapter numbers and titles correspond to the chapters of the book
“A primer on Scientific Programming with Python” by Hans Petter Langtangen.
The exercises are meant to be a supplement to the exercise collection in the book,
and most are motivated by applications in science and applied mathematics.
The exercise collection is used for the first time in 2018, and there may be typos
and small errors. If you find any errors, or have other comments or questions
about the exercises, please send them to Joakim Sundnes: [email protected].
1
Chapter 1
Computing with Formulas
Problem 1.1. Throw a ball
When throwing a ball in the air, the position of the ball can be calculated using
the acceleration of the ball. When neglecting air resistance, the acceleration will
be the negative of the gravitational constant, ?g. The height of the ball relative
to its starting point is
y(t) = v0t ?12gt2,
where v0 is the initial velocity of the ball and t is the time after the throw. The
ball reaches its maximum height at time
tmax =v0g.
Write a program computing the maximum height of the ball, that is y(tmax),
when v0 = 8.2m/s and g = 9.81m/s2. Print the result.
Filename: ball.py
Problem 1.2. Population growth
The growth of a population can often be described by a logistic function
N(t) = B1 + Ce?kt ,
where B is the carrying capacity of the species in the environment, i.e., the
maximum size of the population that the environment can sustain indefinitely.
The constant k tells us something about how fast the population grows, while C
is given by the initial conditions. Let us consider a bacterial colony where we
take the carrying capacity to be B = 50000 and k = 0.2h?1. If the population is
5000 at t = 0, find C and write a code that finds the number of bacteria in the
colony after 24 hours.
Filename: population.py
2
Problem 1.3. Solve the quadratic equation
Given a quadratic equation
ax2 + bx + c = 0,
the two roots are.
Make a program evaluating the roots of.
Print out both roots with two decimals.
Filename: find_roots.py
Problem 1.4. Forces in the hydrogen atom
There are two kinds of forces acting between the proton and the electron in the
hydrogen atom; Coulomb force and gravitational force. The Coulomb force can
be expressed as,
where ke is Coulomb’s constant, e is the elementary charge, and r is the distance
between the proton and the electron.
The gravitational force can be expressed as
FG = Gmpmer2,
where G is the gravitational constant, mp is the mass of the proton, me is the
mass of the electron, and r is the distance between the particles.
We can use these expressions for FC and FG to illustrate the difference in
strength of these two forces, i.e., the electromagnetic force and gravitational force.