Mathematical models based on difference equations
Interest rates
The factorial as a difference equation
Fibonacci numbers
Growth of a population
Logistic growth
Payback of a loan
The integral as a difference equation
Taylor series as a difference equation
Making a living from a fortune
Newton's method
The inverse of a function
Programming with sound
Writing sound to file
Reading sound from file
Playing many notes
Music of a sequence
Exercises
Exercise 1: Determine the limit of a sequence
Exercise 2: Compute \( \pi \) via sequences
Exercise 3: Reduce memory usage of difference equations
Exercise 4: Compute the development of a loan
Exercise 5: Solve a system of difference equations
Exercise 6: Modify a model for fortune development
Exercise 7: Change index in a difference equation
Exercise 8: Construct time points from dates
Exercise 9: Visualize the convergence of Newton's method
Exercise 10: Implement the secant method
Exercise 11: Test different methods for root finding
Exercise 12: Make difference equations for the Midpoint rule
Exercise 13: Compute the arc length of a curve
Exercise 14: Find difference equations for computing \( \sin x \)
Exercise 15: Find difference equations for computing \( \cos x \)
Exercise 16: Make a guitar-like sound
Exercise 17: Damp the bass in a sound file
Exercise 18: Damp the treble in a sound file
Exercise 19: Demonstrate oscillatory solutions of the logistic equation
Exercise 20: Automate computer experiments
Exercise 21: Generate an HTML report
Exercise 22: Use a class to archive and report experiments
Exercise 23: Explore logistic growth interactively
Exercise 24: Simulate the price of wheat
References
From mathematics you probably know the concept of a sequence, which is nothing but a collection of numbers with a specific order. A general sequence is written as $$ \begin{equation*} x_0,\ x_1,\ x_2,\ \ldots,\ x_n,\ldots, \end{equation*} $$ One example is the sequence of all odd numbers: $$ \begin{equation*} 1, 3, 5, 7, \ldots, 2n+1,\ldots \end{equation*} $$ For this sequence we have an explicit formula for the \( n \)-th term: \( 2n+1 \), and \( n \) takes on the values 0, 1, 2, \( \ldots \). We can write this sequence more compactly as \( (x_n)_{n=0}^\infty \) with \( x_n=2n+1 \). Other examples of infinite sequences from mathematics are $$ \begin{equation} 1,\ 4,\ 9,\ 16,\ 25,\ \ldots\quad (x_n)_{n=0}^\infty,\ x_n=(n+1)^2, \tag{1} \end{equation} $$ $$ \begin{equation} 1,\ \frac{1}{2},\ \frac{1}{3},\ \frac{1}{4},\ \ldots\quad (x_n)_{n=0}^\infty,\ x_n={1\over n+1}\tp \tag{2} \end{equation} $$
The former sequences are infinite, because they are generated from all integers \( \geq 0 \) and there are infinitely many such integers. Nevertheless, most sequences from real life applications are finite. If you put an amount \( x_0 \) of money in a bank, you will get an interest rate and therefore have an amount \( x_1 \) after one year, \( x_2 \) after two years, and \( x_N \) after \( N \) years. This process results in a finite sequence of amounts $$ \begin{equation*} x_0, x_1, x_2,\ldots, x_N,\quad (x_n)_{n=0}^N\tp\end{equation*} $$ Usually we are interested in quite small \( N \) values (typically \( N\leq 20-30 \)). Anyway, the life of the bank is finite, so the sequence definitely has an end.
For some sequences it is not so easy to set up a general formula for the \( n \)-th term. Instead, it is easier to express a relation between two or more consecutive elements. One example where we can do both things is the sequence of odd numbers. This sequence can alternatively be generated by the formula $$ \begin{equation} x_{n+1} = x_{n} + 2\tp \tag{3} \end{equation} $$ To start the sequence, we need an initial condition where the value of the first element is specified: $$ \begin{equation*} x_0 = 1\tp\end{equation*} $$ Relations like (3) between consecutive elements in a sequence is called recurrence relations or difference equations. Solving a difference equation can be quite challenging in mathematics, but it is almost trivial to solve it on a computer. That is why difference equations are so well suited for computer programming, and the present document is devoted to this topic. Necessary background knowledge is programming with loops, arrays, and command-line arguments and visualization of a function of one variable.
The program examples regarding difference equations are found in the folder src/diffeq.
This chapter is taken from the book A Primer on Scientific Programming with Python by H. P. Langtangen, 5th edition, Springer, 2016.