# sheng/Ordinary differential equation

### From CDS 130

## Contents |

# 1. Objectives

- To introduce the concept of a
**difference equation** - To introduce the concept of an
**ordinary differential equation**

# 2. Motivation

- Most science models have a mathematical representation that has the form of an
**ordinary differential equation**.

# 3. Pre-questions

- You won $100 and want to invest it. Which bank offers the better deal?
- Bank A: 12% interest per year compounded yearly
- Bank B: 1% interest per month compounded monthly
- Bank C: (1/365.2425)% interest compounded daily

"Compounded yearly/monthly/daily" means that at the end of one year/month/day the interest is added to your balance.

So, after one

- year you have $112 dollars
- month you have $101 dollars
- day you have 100+(1/365.2425) dollars

# 4. Slides

## 4.1. Introduction

In computational science, we most often want to simulate systems that continuously change. Previously we modeled population change with a **discrete** equation. In reality, population continuously changes. The mathematical model of this is

P(next instant)-P(this instant) = -(time between this instant and the next)*a*P(this instant)

where the time between iterations

(time between this instant and the next)

is nearly zero. In a calculus course, you will learn that this is related to limits and derivatives.

To see where the "above time between iterations" factor comes in, consider the following observation, which does not require an understanding of calculus.

## 4.2. Observation

Population grows at a rate of 20% per year

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

Won't you get the same answer if you divide the growth rate by 12 and change the equation to go month-to-month?

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

this can be rewritten as

P(next month) - P(this month) = (time between iterations)*(0.2)*P(this month)

where

(time between iterations) = (1/12)th of a year

## 4.3. Observation cont.

Won't you get the same answer if you divide the growth rate by 12 and change the equation to go month-to-month? No. But close.

P(1) = 1; for i = [1] P(i+1) = P(i) + 0.2*P(i); end P(2) = 1.2000

P(1) = 1; for i = [1:12] P(i+1) = P(i) + (0.2/12)*P(i); end P(13) = 1.2194

## 4.4. Observation cont.

Explore the rule

- Multiply the number of iterations by some factor
- Divide the growth rate by the same factor

P(1) = 1; for i = 1:10 P(i+1) = P(i) + (100*0.001)*P(i); end P(11) = 2.5937

for i = 1:100 P(i+1) = P(i) + (10*0.001)*P(i); end P(101) = 2.7048

for i = 1:1000 P(i+1) = P(i) + (1*0.001)*P(i); end P(1001) = 2.7169

(Note that 2.7169 is fairly close to *e*^{1} = 2.718281828459046.... If you continue the above process, you will get closer and closer to *e*^{1}.)

## 4.5. Observation cont.

Suppose that you were asked to compute

for i = 1:1000 P(i+1) = P(i) + (0.001)*P(i); end P(1001) = 2.7169

it appears that you could do 10 times fewer iterations and get about the same answer by

- Multiplying the number of iterations by some factor
- Dividing the growth rate by the same factor

for i = 1:100 P(i+1) = P(i) + (10*0.001)*P(i); end P(101) = 2.7048

## 4.6. Summary

In computational science, we most often want to simulate systems that have the computational model of

for i = 1:HUGE P(i+1) = P(i) + (SMALL)*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. - In the field of computational mathematics, they study how modify the right-hand-side of the equation for
`P(i+1)`

along with the values of`HUGE`

and`SMALL`

in order to minimize the amount of computation and maximize the accuracy of the calculation.

## 4.7. Definition

- This type of program arises from the translation of a mathematical model that has the form of an
**ordinary differential equations**(ODE) to a computational model. ODEs are used to mathematically model many natural systems.

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

- For example, the
**science model**for a system that experiences continuous growth at a rate that is proportional to the population is an**ordinary differential equation**which has the form

- or

## 4.8. Definition cont.

can be written as

P(next instant)-P(this instant) = (time between this instant and the next)*a*P(this instant)

because by definition

and

The *differential* in **differential equation** comes from the fact that it is a model of a difference in population from one instant to the next for a difference in time that is very small.

## 4.9. Definition cont.

Another situation where an ODE appears is in the mathematical model of how an object cools. The change in temperature, `T`

, of an object as it cools has the **mathematical model** of

where *T*_{a} is the temperature of the room where the object is stored (ambient temperature). This can also be written as

T(next instant)-T(this instant) = -(time between this instant and the next)*q*T(this instant)

where the time between the next instant and this instant, Δ*t*, is nearly zero.

# 5. Questions

## 5.1. Bank Eqn.

Consider the equations for a bank account with 1% interest per year compounded yearly and 1/12 percent interest per month compounded monthly. The initial balance is $100, and no additional deposits are made.

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

For this problem, type `format long`

as the first command in your program or when you first start MATLAB. This tells MATLAB to show more digits when displaying numbers.

- Write a program that uses the first equation to compute the balance after 10 years. Post the program (or a screenshot) to your wiki page.
- Write a program that uses the second equation to compute the balance after 10 years. Post the program (or a screenshot) to your wiki page.
- Compute the balance after 10 years using 1/365 percent interest compounded daily (assume 1 year = exactly 365 days). Enter the value on your wiki page.
- Compute the balance after 10 years using 1/(365*24) percent interest compounded hourly (assume 1 year = exactly 365 days). Enter the value on your wiki page.
- Compute the balance after 10 years using 1/(365*24*60) percent interest compounded every minute (assume 1 year = exactly 365 days). Enter the value on your wiki page.
- Compute the balance after 10 years using 1/(365*24*60*60) percent interest compounded every second (assume 1 year = exactly 365 days). Enter the value on your wiki page.
- (If you did not already) write a program that computes all of the above balances using only two
`for`

loops.

# 6. Other Questions

## 6.1. Heat Eqn.

The equation that describes how the temperature of an object, `T`

changes with time (Newton's Law of Cooling) is

- Use a spreadsheet to determine how the temperature of an object that starts out at 100 degrees C cools when the ambient temperature,
`Ta`

is 0. Assume that q = 0.1 and Δ*t*= 0.1. At approximately what time does the temperature equal one-half of its initial temperature? If Δ*t*= 0.05, at approximately what time does the temperature equal one-half of its initial temperature? - Does the time it takes for the temperature to fall by one-half depend on the initial temperature of the object? Describe in words you how you determined this.

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

- Use Excel to plot population as a function of time for 40 years. Assume that the initial population is 20.
- Try to make a change to the default setting for the plot that makes it clearer or easier to read (in your opinion; there is no "wrong" answer).

## 6.3. Code Example

Consider the following science model: "The starting balance in a bank account is $1,000 and it grows by 5% per year."

The following Matlab/Octave code was written to simulate the month-to-month growth of this bank account for 36 months, and, to print out the balance at the end of months 26, 27 and 28:

B(1) = 1000; for i = [2:35] B(i) = B(i-1) + 0.05*B(i-1); end B(26) B(27) B(28)

When this code is run in Matlab or Octave, we find that B(26) = $3,386.40, B(27) = $3,555.70 and B(28) = $3,733.50, all of which are incorrect. Why?