# Difference Equations

### From ComputingForScientists

## Contents |

# 1. Introduction

In computational science, we often want to simulate systems that continuously change. Previously we modeled population change and bank balance growth with a **discrete** equation. The mathematical model of this had the form

B(next instant) - B(this instant) = a*B(this instant)

And the time between instances was some fixed time interval, say one month or one year.

# 2. Relationship to Differential Equations

Your bank balance grows at a rate of 20% per year

B(next year) = B(this year) + 0.2*B(this year)

Suppose that we ask what will happen if we divide the growth rate by 12 and change the equation to compute month-to-month values:

B(next month) = B(this month) + (0.2/12)*P(this month)

Will both methods give the same answer for the balance after one year has elapsed (or in the 13th month)? No, but, but the answers are close. We are going to use this fact to reduce the amount of computation that we need to do.

If we are told that the correct problem to solve is 20%/12 per month problem, we can get an approximate answer that requires fewer steps by doing the 20% per year problem.

As shown below, the difference between the answers, assuming a starting balance of $1, is 1.200-1.2194 = 0.006. If we are only concerned about getting the answer correct to two decimals, then it makes sense to solve the year-to-year problem if you want to do less computation - the year-to-year problem only requires 1 calculation to determine the population in the next year whereas the month-to-month problem requires 12 calculations.

The number of iterations is one and the percent increase per iteration is 20%. clear; P(1) = 1; for i = [1: Long-hand version of the above. clear; P(1) = 1; i = 1; P(i+1) = P(i) + 0.2*P(i); P(2) % Will display |
The number of iterations executed is increased by a factor of 12 and the percentage increase in clear; P(1) = 1; for i = [1: Long-hand version of the above. clear; i = 1; P(i+1) = P(i) + (0.2/12)*P(i); i = 2; P(i+1) = P(i) + (0.2/12)*P(i); i = 3; P(i+1) = P(i) + (0.2/12)*P(i); i = 4; P(i+1) = P(i) + (0.2/12)*P(i); i = 5; P(i+1) = P(i) + (0.2/12)*P(i); i = 6; P(i+1) = P(i) + (0.2/12)*P(i); i = 7; P(i+1) = P(i) + (0.2/12)*P(i); i = 8; P(i+1) = P(i) + (0.2/12)*P(i); i = 9; P(i+1) = P(i) + (0.2/12)*P(i); i = 10; P(i+1) = P(i) + (0.2/12)*P(i); i = 11; P(i+1) = P(i) + (0.2/12)*P(i); i = 12; P(i+1) = P(i) + (0.2/12)*P(i); P(13) % Will display |

Consider what happens as we increase the number of iterations and decrease the multiplication factor by the same factor.

When the number of iterations is "large enough", you'll start getting the same final answer to several decimal places.

Importance: Most mathematical models imply that the number of iterations must be **infinite**, which would require and infinite amount of time to get an answer. In this example, we see that if we choose a large number, we'll get an answer that is expected to be close to the answer that we would get if we performed an infinite number of iterations.

clear; P(1) = 1; for i = [1: clear; P(1) = 1; for i = [1: clear; P(1) = 1; for i = [1: |
clear; P(1) = 1; for i = [1: clear; P(1) = 1; for i = [1: clear; P(1) = 1; for i = [1: |

### 2.1. Summary

In computational science, we most often want to simulate systems that requires computation of the form (where `a`

is a decimal number such as 0.2, or -1.0)

for i = 1:HUGE P(i+1) = P(i) + (a/HUGE)*P(i); end

The main problem is that if `HUGE`

is large, many iterations are required, which means that the calculation will take a long time.

This type of program arises from the translation of a mathematical model that has the form of a **differential equation** (DE) to a computational model, which has the form of a **difference equation**. **Ordinary differential equations** (ODEs) are used to mathematically model many natural systems. When translated to a computational model, they typically take the form of a **difference equation**. An example of this translation is given below.

# 3. Problems

## 3.1. Interest

Suppose that you deposit $1000 in your account end of each year and that you started with $100 at the start of the first year.

Would you rather have a bank that offered 5% interest per **year** like this:

B(next year) = B(this year) + 0.05*B(this year) + 1000

or a bank that offered (5/12) percent interest per **month** like this:

B(next month) = B(this month) + (0.05/12)*B(this month) + 1000/12

Answer the following without using a computer program (work it out using pencil, paper, and a calculator). Turn in your work on a sheet of paper at the start of class (or post an image of your work take with a digital camera or scanner).

- For the first equation, write down the balance at the start of year one and year two.
- For the second equation, write down the balance at the start of the the start of each month up to an including month 25.

Write a computer program the does the above calculations. For both questions, a `for`

loop must be used. Copy your code into your wiki page.

Answer |
---|

year 1 B = 100 year 2 B = 100 + 0.05*100 + 1000 = 1105 year 3 (note problem only asked for year 2, but year 3 included here for comparison with other answer) B = 1105 + 0.05*1105 + 1000 = 2160.25 B(1) = 100 for i = 1:2 B(i+1) = B(i) + (0.05/12)*B(i) + 1000/12 end B(3) month 1 B = 100 month 2 B = 100 + (0.05/12)*100 + 1000/12 = 183.3375 (Note - it is month 3 B = 183.3375 + (0.05/12)*183.3375 + 1000/12 = 267.4347 ... month 25 B = 2117.1659 + 2117.1659*(2117.16598644552) + 1000/12 = 2209.32 B(1) = 100 for i = 1:24 B(i+1) = B(i) + (0.05/12)*B(i) + 1000/12 end B(25) Note - the for loop approach should give the same answer as your hand calculation. Also note that the start of year 3 is month 25 which you computed. This is larger than the balance in all of year 3 using the first approach ($2160.25). Therefore, the second equation will result in more money in the account. |

## 3.2. Population Eqn.

Consider the equations

P(next year) = P(this year) + 0.01*P(this year)

P(next month) = P(this month) + (1/12)*0.01*P(this month)

Assume the initial population is 100.

- Write a program that uses the first equation to compute the population in year 10 and prints out its value.
- Write a program that uses the second equation to compute the population in the first month of year 10 and prints out its value.
- Write a program that uses the second equation to compute the population in the last month of year 10 and prints out its value.

When your solutions for each of the above are copied onto the command line, only the requested value must be printed. As an example, the following program prints out only the last value of an array created using a `for`

loop:

for i = [1:5] A(i) = i; end A(5)

Answer |
---|

Note - only need to go till i = 9 to compute value in year 10. if you changed for i = [1:9] to for i = [1:10] and left everything else the same, you would get the correct answer, but you would have done an extra calculation that was not needed. clear; P(1) = 100; for i = [1:9] P(i+1) = P(i) + 0.01*P(i); end P(10) = 109.36 % 9 years x 12 months = 108. clear; P(1) = 100; for i = [1:108] P(i+1) = P(i) + (0.01/12)*P(i); end % Month 109 is first month of year 10 P(109) = 109.41 % 108 + 11 = 119 clear; P(1) = 100; for i = [1:118] P(i+1) = P(i) + (0.01/12)*P(i); end % Month 119 is last month of year 10 P(119) = 110.33 |

## 3.3. Population Eqn.

Consider the following model of population

Every year, population increases a value of 10% of the population in the previous year. However, if the predicted population is over 100, a disease outbreak instantly kills 80% of this predicted population value. For example, if the predicted population is 110, then the next year the population is 0.2*110.

- Write a program that computes the population over a 40-year time span. Assume that the initial population is 20. (Hint: you can do this using an
`if`

statement.)

Answer |
---|

% Change 2400 to 39 to compute population over 40-year time span. clear; P(1) = 20; for i = [1:2400] P(i+1) = P(i) + 0.1*P(i); if (P(i+1) > 100) P(i+1) = 0.2*P(i+1); end end plot(P) xlabel('Year') ylabel('Population') |