March 21, 2018

Are you ready?

What do you need to know?

  • Computational Thinking
    • Decomposition
    • Pattern Recognition
    • Abstraction
    • Algorithm Design
  • Not memory
  • Problem solving

Key ideas

  • Loops: for(){}, while(){}
  • Conditional: if(){}, if(){} else {}
  • Recursive functions:
    • fac(N) = N * fac(N-1)
  • Cumulative values: today = yesterday + change

Practical tools

  • Turtle graphics
  • Normal graphics: plot(), segments()
  • Reading DNA sequences read.fasta()

The most important part

Do you want to pass this class?

Are you lazy?

Are you a hard worker?

If you are here, you are smart

Do you have the will to succeed?

Did you do all the homework?

Google is your friend

Did you find new exercises?

Dynamical Systems

System of cells and food

Last class we did this

ncells <- growth      <- rep(NA, 100)
food   <- food_change <- rep(NA, 100)
ncells[1]      <- 2
growth[1]      <- 0
food[1]        <- 1E6
food_change[1] <- 0

for(i in 2:100) {
  growth[i] <- ncells[i-1]*food[i-1]/1E6
  food_change[i] <- -ncells[i-1]
  ncells[i] <- ncells[i-1] + growth[i]
  food[i]   <- food[i-1] + food_change[i]
}

But cells cannot eat when there is no food

ncells <- growth      <- rep(NA, 100)
food   <- food_change <- rep(NA, 100)
ncells[1]      <- 2
growth[1]      <- 0
food[1]        <- 1E6
food_change[1] <- 0

for(i in 2:100) {
  growth[i] <- ncells[i-1]*food[i-1]/1E6
  food_change[i] <- -ncells[i-1]*food[i-1]/1E6
  ncells[i] <- ncells[i-1] + growth[i]
  food[i]   <- food[i-1] + food_change[i]
}

Abstraction: avoid fixed numbers

N <- 100
ncells <- growth      <- rep(NA, N)
food   <- food_change <- rep(NA, N)
ncells[1]      <- 2
growth[1]      <- 0
food[1]        <- 1E6
food_change[1] <- 0

for(i in 2:N) {
  growth[i] <- ncells[i-1]*food[i-1]/food[1]
  food_change[i] <- -ncells[i-1]*food[i-1]/food[1]
  ncells[i] <- ncells[i-1] + growth[i]
  food[i]   <- food[i-1] + food_change[i]
}

Pattern recognition: Always change

Dynamical System means that the values of each part will change

We need to choose two names:

  • the part of the system
  • the change of the part of the system

For each thing X the change will be d_X

Instead of growth we write d_ncells

Applying the pattern

N <- 100
ncells   <- d_ncells <- rep(NA, N)
food     <- d_food   <- rep(NA, N)
ncells[1]   <- 2
food[1]     <- 1E6
d_ncells[1] <- d_food[1]   <- 0

for(i in 2:N) {
  d_ncells[i] <- ncells[i-1]*food[i-1]/food[1]
  d_food[i]  <- -ncells[i-1]*food[i-1]/food[1]
  ncells[i] <- ncells[i-1] + d_ncells[i]
  food[i]   <- food[i-1]   + d_food[i]
}

Algorithm design: write as function

cell_culture <- function(N=100, food_ini=1E6) {
  ncells <- d_ncells <- rep(NA, N)
  food   <- d_food   <- rep(NA, N)
  ncells[1]   <- 2
  food[1]     <- food_ini
  d_ncells[1] <- d_food[1] <- 0
  
  for(i in 2:N) {
    d_ncells[i] <- ncells[i-1]*food[i-1]/food_ini
    d_food[i]  <- -ncells[i-1]*food[i-1]/food_ini
    ncells[i] <- ncells[i-1] + d_ncells[i]
    food[i]   <- food[i-1]   + d_food[i]
  }
  return(data.frame(ncells, d_ncells, food, d_food))
}

Another system: Water

A chemical reaction combines hydrogen and oxygen to produce water

To make it simple we will assume that the reaction is this: \[2H+O\leftrightarrow H_2O\]

Our system has 3 parts:

  • Hydrogen
  • Oxygen
  • Water

The simple system

Reactions are bidirectional

The parts of the system

We will measure the number of atoms and molecules in moles

The initial quantities are: 1 mole oxygen, 1 mole hydrogen and 0 mole water

We represent hydrogen by the vector H, oxygen by O, and water by W.

Each vector has the number of atoms or molecules for different times

The rules are the following:

  • There are two reactions at the same time:
    1. from hydrogen and oxygen into water (that is, left to right),
    2. from water into hydrogen and oxygen (that is, right to left).
  • The speed of reaction1 depends on the number of oxygen atoms and the square of the number of hydrogen atoms \[r_1 = k_1 H^2O\]
  • The speed of reaction2 depends on the number of water molecules \[r_2=k_2 W\]
  • The constants \(k_1\) and \(k_2\) depend on temperature and other conditions

More rules

  • The number water molecules increase with the first reaction and decreases with the second reaction \[d\_W = r1 - r2\]
  • The number of oxygen atoms reduces with the first reaction and increases with the second \[d\_O = -r1 + r2\]
  • The number of hydrogen atoms reduces two times with the first reaction and increases two times with the second \[d\_H = 2*(-r1 + r2)\]

The reactions

Then the main part of the simulation is

for(i in 2:N) {
    d_W[i] <-    k1*H[i-1]*H[i-1]*O[i-1] - k2*W[i-1]
    d_O[i] <-   -k1*H[i-1]*H[i-1]*O[i-1] + k2*W[i-1]
    d_H[i] <- -2*k1*H[i-1]*H[i-1]*O[i-1] + 2*k2*W[i-1]

    W[i] <- W[i-1] + d_W[i]
    O[i] <- O[i-1] + d_O[i]
    H[i] <- H[i-1] + d_H[i]
}

Exercise: finish it

Create a water_formation function.

Inputs are:

  • N: Number of steps in the simulation
  • H_ini: initial amount of hydrogen, default 1
  • O_ini: initial amount of oxygen, default 1
  • W_ini: initial amount of water, default 0
  • k1: reaction 1 rate, default 0.1
  • k2: reaction 2 rate, default 0.1

Exercise 2: conservation of mass

One atom of oxygen has 16 times the mass of one atom of hydrogen

Therefore one molecule of water has mass 18

Show that the total mass never changes