Programming with Sound

Sound on a computer is nothing but a sequence of numbers. As an example, consider the famous A tone at 440 Hz. Physically, this is an oscillation of a tunefork, loudspeaker, string or another mechanical medium that makes the surrounding air also oscillate and transport the sound as a compression wave. This wave may hit our ears and through complicated physiological processes be transformed to an electrical signal that the brain can recognize as sound. Mathematically, the oscillations are...

Write A Program That Simulates A Traffic Light Using Python

Make a program that simulates flipping a coin N times. Print out tail or head for each flip and let the program count the number of heads. Hint Use r random.random and define head as r lt 0.5 or draw an integer among 1, 2 with r random.randint 1,2 and define head when r is 1. Name of program file flip_coin.py. o Exercise 8.2. Compute a probability. What is the probability of getting a number between 0.5 and 0.6 when drawing uniformly distributed random numbers from the interval 0,1 To answer...

Class Hierarchy for Geometric Shapes

Oop Geometric Shapes

Our next examples concern drawing geometric shapes. We know from Chapter 4 how to draw curves y f x , but the point now is to construct some convenient software tools for drawing squares, circles, arcs, springs, wheels, and other shapes. With these tools we can create figures describing physical systems, for instance. Classes are very suitable for implementing the software because each shape is naturally associ ated with a class, and the various classes are related to each other through a...

Example Solution of Differential Equations

An ordinary differential equation ODE , where the unknown is a function u t , can be written in the generic form In addition, an initial condition, u 0 u0, must be associated with this ODE to make the solution of 7.3 unique. The function f reflects an expression with u and or t. Some important examples of ODEs and their corresponding forms of f are given below. 1. Exponential growth of money or populations where a is a given constant expressing the growth rate of u. 2. Logistic growth of a...

Random Walk in Two Space Dimensions

A random walk in two dimensions performs a step either to the north, south, west, or east, each one with probability 1 4. To demonstrate this process, we introduce x and y coordinates of np particles and draw random numbers among 1, 2, 3, or 4 to determine the move. The positions of the particles can easily be visualized as small circles in an xy coordinate system. The algorithm described above is conveniently expressed directly as a complete working program def random_walk_2D np, ns, plot_step...

Monte Carlo Integration

Montecarlo Integration Error

One of the earliest applications of random numbers was numerical computation of integrals, that is, a non-random deterministic problem. Here we shall address two related methods for computing fa f x dx. 8.5.1 Standard Monte Carlo Integration Let be uniformly distributed random numbers between a is an approximation to the integral fb f x dx. This method is usually referred to as Monte Carlo integration. It is easy to interpret 8.7 . A well-known result from calculus is that the integral of a...

Random Walk in One Space Dimension

In this section we shall simulate a collection of particles that move around in a random fashion. This type of simulations are fundamental in physics, biology, chemistry as well as other sciences and can be used to describe many phenomena. Some application areas include molecular motion, heat conduction, quantum mechanics, polymer chains, population genetics, brain research, hazard games, and pricing of financial instruments. Imagine that we have some particles that perform random moves, either...

Polynomial Dict1.py

Make a very simple class. Make a class Simple with one attribute i, one method double, which replaces the value of i by i i, and a constructor that initializes the attribute. Try out the following code for testing the class s2 Simple 'Hello' s2.double s2.double print s2.i s2.i 100 print s2.i Before you run this code, convince yourself what the output of the print statements will be. Name of program file Simple.py. o Exercise 7.3. Extend the class from Ch. 7.2.1. Add an attribute...

Exercises

Make a Fahrenheit-Celsius conversion table. Modify the c2f_table_while.py program so that it prints out a table with Fahrenheit degrees 0,10, 20, , 100 in the first column and the corresponding Celsius degrees in the second column. Name of program file c2f_table_while.py. O Write a program that generates all odd numbers from 1 to n. Set n in the beginning of the program and use a while loop to compute the numbers. Make sure that if n is an even number, the largest generated odd...