Exercises
Contents
Exercises¶
The following exercises are meant for practice. They are grouped in three difficulty levels: Basic, Intermediate, Advanced. Assess your own abilities and start with exercises that challenge you. If you have problems, try to:
look for helpful examples in the previous chapters
read the documentation of the functions and classes
look for information on the internet
ask fellow students
ask the tutor
from myturtle import Turtle
import numpy as np
import matplotlib.pyplot as plt
Level: Basic¶
Exercise 1: Draw a single step of a stair with turtle.
Exercise 2: Wrap the drawing of a step of a stair in a function. The first argument should be the Turtle
object. Two additional arguments should be the width and the height of the step, both with a default of 1.
Draw a stair with three steps.
Exercise 3: Write a function which draws a stair with an arbitrary number of steps. Reuse the code of the previous exercises.
Exercise 4: Modify the code in a way, that you can draw a staircase, meaning various opposed stairs. Write another function, which draws a house. Draw a house with a three-storied staircase.
Level: Intermediate¶
Exercise 1: Write a function, which returns the absolute gravitational acceleration by a body of mass M
. The gravitational acceleration is given by:
with \(\Gamma = 6,672 \cdot 10^{-11} \frac{\text{m}³}{\text{kg}\,\text{s}²}\) and \(r\) is the distance between the two masses.
Exercise 2: Rewrite the function in such a way, that it returns
Exercise 3: Write a program, which simulates the flight of a canonball. You can (but do not have to) assume a constant gravitational acceleration of \(g = 9.80665 \frac{\text{m}}{\text{s}²}\).
Necessary parameters for the computation:
Initial speed and angle. Combining to one vector is advised.
Time increment \(dt\) of the simulation. Use \(dt = 0.1\text{s}\)
Approximate the velocity vector for every time increment \(n\) with \(t = n\cdot dt\) through
The change in position is then given by
Use the method move
from Turtle
to change the position. The current position is obtained by the attribute position
.
Start with a for
-loop with 20 iteration steps. If you have the feeling that the result is right, rewrite the programme in a way, that it aborts when the canonball hits the floor.
Exercise 4: Wrap the program in a function with arguments
Turtle
objectInitial speed
Initial angle
Time increment (Default 0.1 s) (Compute the accuracy of the distance measurement)
Write a program, which computes the shot range depending on the initial angle and velocity. Use angles between 10° and 70° in 1° steps and initial velocities between \(50 \frac{\text{m}}{\text{s}}\) and \(200 \frac{\text{m}}{\text{s}}\) in \(10 \frac{\text{m}}{\text{s}}\) steps. Organize the result in a nested list.
The two following cells contain code to visualize the results.
Tip: Turn off the graphical output of Turtle
with the method set_draw
before running the simulation.
# speed: sequence containing the velocities
# angle: sequence containg the angles
# distance: nested list containing the shot ranges (shape: n_angle x n_speed)
plt.contourf(speed, angle, distance)
plt.colorbar()
plt.ylabel('angle [°]')
plt.xlabel('speed [m/s]')
plt.title('Shooting distance [m]');
plt.plot(angle, distance[:, -1])
plt.ylabel('shooting distance [m]')
plt.xlabel('angle [°]')
plt.title('Speed {} [m/s]'.format(speed[-1]));
Level: Advanced¶
Exercise 1: Expand the class Turtle
by the functionality of the Basic level exercises. Objects of this class should be able to draw houses and staircases.
Add a method, which draws houses with staircases. The argument should be the number of stories.
Draw a city (~ 5-10 multilevel houses of different size).
Exercise 2:
Expand the class Turtle
:
add the attributes velocity (list) and mass (float)
Write a method, which computes the gravitational acceleration of an object from another object of the same type (see Intermediate level Exercise 2).
Write a method, which sums the gravitational acceleration from a list of objects.
Write a method to compute the velocity at the next time step (see Intermediate level Exercise 3)
Consider the following objects:
sun:
Position: (0, 0)
Velocity: (0, 0)
Mass: 1.989e30
earth:
Position relative to the sun: (0, 1.496e11)
Velocity relative to the sun: (2.9780e4, 0)
Mass: 5.9722e24
moon:
Position relative to the earth: (0, 3.844e8)
Velocity relative to the earth: (1.023e3, 0)
Mass: 7.349e22
First simulate the orbit of the moon around the earth in geocentric coordinates. You can assume the earth to be fixed in space. Use a time increment of \(dt = 5000s\) and simulate 30 days.
Simulate the orbit of the moon around the earth, while it is orbiting around the sun.
Try to simulate a “gravitational slingshot”.