March 21, 2018
for(){}
, while(){}
if(){}
, if(){} else {}
fac(N) = N * fac(N-1)
plot()
, segments()
read.fasta()
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] }
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] }
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] }
Dynamical System means that the values of each part will change
We need to choose two names:
For each thing X
the change will be d_X
Instead of growth
we write d_ncells
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] }
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)) }
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:
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
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] }
Create a water_formation
function.
Inputs are:
N
: Number of steps in the simulationH_ini
: initial amount of hydrogen, default 1O_ini
: initial amount of oxygen, default 1W_ini
: initial amount of water, default 0k1
: reaction 1 rate, default 0.1k2
: reaction 2 rate, default 0.1One 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