# 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.  year-to-year problem solution The number of iterations is one and the percent increase per iteration is 20%. clear; P(1) = 1; for i = [1:1] P(i+1) = P(i) + (0.2/1.0)*P(i); end P(2) % Will display 1.2000  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 1.2000  month-to-month problem solution The number of iterations executed is increased by a factor of 12 and the percentage increase in P is smaller by a factor of 12. clear; P(1) = 1; for i = [1:12] P(i+1) = P(i) + (0.2/12)*P(i); end P(13) % Will display 1.2194  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 1.2194  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:1] P(i+1) = P(i) + (0.2/1.0)*P(i); end P(2) % Will display 1.2000  clear; P(1) = 1; for i = [1:12] P(i+1) = P(i) + (0.2/12)*P(i); end P(13) % Will display 1.2194  clear; P(1) = 1; for i = [1:24] P(i+1) = P(i) + (0.2/24)*P(i); end P(25) % Will display 1.2204  clear; P(1) = 1; for i = [1:100] P(i+1) = P(i) + (0.2/100)*P(i); end P(101) % Will display 1.2212  clear; P(1) = 1; for i = [1:1000] P(i+1) = P(i) + (0.2/1000)*P(i); end P(1001) % Will display 1.2214  clear; P(1) = 1; for i = [1:10000] P(i+1) = P(i) + (0.2/10000)*P(i); end P(10001) % Will display 1.2214  ### 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.

## 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.

1. Write a program that uses the first equation to compute the population in year 10 and prints out its value.
2. Write a program that uses the second equation to compute the population in the first month of year 10 and prints out its value.
3. 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)


## 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.)