You are on page 1of 18

Engineering Systems Design 2

Lecture P06 Iteration

Adrian Pearce

Engineering Systems Design 2


Lecture P06 Iteration

Adrian Pearce

Engineering Systems Design 2

Melbourne School of Engineering

Outline

Introduction

Example: Modelling population growth

Iteration in games

Engineering Systems Design 2

Melbourne School of Engineering

Outline

Introduction

Example: Modelling population growth

Iteration in games

Engineering Systems Design 2

Melbourne School of Engineering

Iteration

In this lecture, we will investigate how loops, operators and functions can be used to iteratively solve problems.

Engineering Systems Design 2

Melbourne School of Engineering

Outline

Introduction

Example: Modelling population growth

Iteration in games

Engineering Systems Design 2

Melbourne School of Engineering

Modelling population growth


A model for how the the size of a population changes over time. change in population percentage growth = population change in year year or: Pi = gP (ti ) t where Pi is the size of the population in year i , and g is the annual growth rate measured as a percentage of the current population. Writing this as a difference equation: P (ti +1 ) P (ti ) = gP (ti ) t P (ti +1 ) = P (ti ) + gP (ti )t Pi +1 = Pi + gPi t

Engineering Systems Design 2

Melbourne School of Engineering

Modelling population growth


Given that the world population was approximately 2.56 billion in 1950, with a growth rate of 1.8% per year: step 0 year t0 1950 population (billions) P0 2.56

Engineering Systems Design 2

Melbourne School of Engineering

Modelling population growth


Given that the world population was approximately 2.56 billion in 1950, with a growth rate of 1.8% per year: step 0 1 year t0 1950 t1 = t0 + t 1950 + 1 1951 population (billions) P0 2.56 P1 = P0 + gP0 t 2.56 + (0.018)(2.56)(1) 2.60

Engineering Systems Design 2

Melbourne School of Engineering

Modelling population growth


Given that the world population was approximately 2.56 billion in 1950, with a growth rate of 1.8% per year: step 0 1 year t0 1950 t1 = t0 + t 1950 + 1 1951 t2 = t1 + t 1951 + 1 1952 population (billions) P0 2.56 P1 = P0 + gP0 t 2.56 + (0.018)(2.56)(1) 2.60 P2 = P1 + gP1 t 2.60 + (0.018)(2.60)(1) 2.65

Engineering Systems Design 2

10

Melbourne School of Engineering

Modelling population growth


Given that the world population was approximately 2.56 billion in 1950, with a growth rate of 1.8% per year: step 0 1 year t0 1950 t1 = t0 + t 1950 + 1 1951 t2 = t1 + t 1951 + 1 1952 population (billions) P0 2.56 P1 = P0 + gP0 t 2.56 + (0.018)(2.56)(1) 2.60 P2 = P1 + gP1 t 2.60 + (0.018)(2.60)(1) 2.65

Lets get MATLAB to compute this instead!

Engineering Systems Design 2

11

Melbourne School of Engineering

Describing the algorithm in pseudocode


The population and year accumulation can be stored in vectors:

Pseudocode
Set initial values for the year, population and growth rate FOR the number of years to be simulated Calculate and append next value to year vector Calculate and append next value to population vector Plot the results

Engineering Systems Design 2

12

Melbourne School of Engineering

Describing the algorithm in pseudocode


The population and year accumulation can be stored in vectors:

Pseudocode
Set initial values for the year, population and growth rate FOR the number of years to be simulated Calculate and append next value to year vector Calculate and append next value to population vector Plot the results

Activity
Have a go at writing the code to compute the world population from 1950 to 1999, given that the population at 1950 was 2.56 109 (P1 = 2.56 109 , t1 = 1950) and the growth rate g = 0.018.

Engineering Systems Design 2

13

Melbourne School of Engineering

Computing the year of a population


Rather than computing the population for the next n years, how can we compute the time it takes to reach a population of p?

Pseudocode
Set initial values for the year, population and growth rate WHILE the population is less than the desired population Calculate and append next value to year vector Calculate and append next value to population vector Plot the results

Engineering Systems Design 2

14

Melbourne School of Engineering

Computing the year of a population


Rather than computing the population for the next n years, how can we compute the time it takes to reach a population of p?

Pseudocode
Set initial values for the year, population and growth rate WHILE the population is less than the desired population Calculate and append next value to year vector Calculate and append next value to population vector Plot the results

Activity
Have a go at writing the code to compute the year that the world population will reach 1 1012 , given that the population at 1950 was 2.56 109 (P1 = 2.56 109 , t1 = 1950) and the growth rate g = 0.018.

Engineering Systems Design 2

15

Melbourne School of Engineering

Outline

Introduction

Example: Modelling population growth

Iteration in games

Engineering Systems Design 2

16

Melbourne School of Engineering

Example: Pac Man

Example (Three dimensional Pac Man)


In 2009, we all programmed a different (three dimensional) version of the retro game pacman.

Engineering Systems Design 2

17

Melbourne School of Engineering

Next Lecture

Games

Engineering Systems Design 2

18

Melbourne School of Engineering

You might also like