Below are the solutions to these exercises on “Simple Spatial Modeling – Part 1.”

rainfall <- read.table("C:/Users/Hanif Kusuma/Documents/R Blogging/spatial/rain.txt") plot(x = (rainfall[,1]), y = rainfall[,2], xlab = "Time (sec)", ylab = "Water level (cm)", main = "Plot of simulated water level in tank")

############### # # # Exercise 1 # # # ############### #define the model parameters and the inital conditions t <- 0 k1 <- 0.04 k2 <- 0.01 k3 <- 0.005 S1 <- 10 S2 <- 30 S3 <- 25 ntimesteps=100 timestep = 1 output <- mat.or.vec(ntimesteps,4) #there are four columns. ############### # # # Exercise 2 # # # ############### for(n in 1:ntimesteps){ dS1 <- k1*S1*timestep # Calculate the flow out of tank dS2 <- k2*S2*timestep dS3 <- k3*S3*timestep S1 <- S1-dS1 # New water level in tank 1 S2 <- S2+dS1-dS2 S3 <- S3+dS2-dS3 t <- t+timestep # New time step output[n,1] <- t #output the data from this time step output[n,2] <- S1 output[n,3] <- S2 output[n,4] <- S3 } #repeat ############### # # # Exercise 3 # # # ############### # plot the model output # calculate range from in water levels within the three tanks g_range <- range(0, output[,2:4]) # Plot water levels in Tank 1 plot(x = (output[,1]), y = output[,2], xlab = "Time (sec)", ylab = "ln water tank level (cm)", main = "Plot of simulated water level in tank",ylim = g_range) # Plot water levels in Tank 2 points(x = (output[,1]), y = output[,3], xlab = "Time (sec)", ylab = "ln water tank level (cm)", main = "Plot of simulated water level in tank",pch = 19) # Plot water levels in tank 3 points(x = (output[,1]), y = output[,4], xlab = "Time (sec)", ylab = "ln water tank level (cm)", main = "Plot of simulated water level in tank",pch = 10)

#TWO WAY INTERACTIONS ############### # # # Exercise 4 # # # ############### rm(list=ls()) # Clear previous values #define new initial conditions t <- 0 ntimesteps <- 400 timestep <- 1 ncell <- 5 # Number of tanks H <- mat.or.vec(ntimesteps+1,ncell) # Creation of an output vector q <- mat.or.vec(1,ncell+1) # Creation of vector to store the calculated water fluxes (q1 to q6) ############### # # # Exercise 5 # # # ############### # External holes are assumed equal to zero H[1,] <- c(1.0,0.8,0.7,0.6,0.5) # Inital water level within the tank k <- c(0,0.01,0.05,0.02,0.06,0) # Model parameter, indicating hole size between each tank ############### # # # Exercise 6 # # # ############### #Sequence function time <- seq(from = 0, by=timestep, length.out=ntimesteps+1) #This produces an array that starts at zero and increase by the stimestep until the total number of timesteps +1 ############### # # # Exercise 7 # # # ############### #this model incoporates nested loops, means that for loops are incoprated within one another for(n in 1:ntimesteps){ for(i in 2:ncell){ # calculate the water flow between cells. # in nested loop we use a different letter to incrument through. This time we have used n. # This runs from *2* upto n cell (previously we ran at a value of 1) Hdiff <- H[n,i-1]-H[n,i] # calculate the difference in water level between two adjacent tanks q[1,i]<- k[i]*Hdiff*timestep # calcuate the flow between the tanks. } #repeat for each tank for(i in 1:ncell){ # water storage update H[n+1,i]<-H[n,i]+q[1,i]-q[1,i+1] # New water store is equal to old water store plus what has flown into # it from the left hand side, minus what has been lost to the right hand side } # repeat for each tank } # This whole processes is repeated for each time step. ############### # # # Exercise 8 # # # ############### # Plotting the data. #always calculate range first g_range <- range(0, H) # Plot water level in first tank: plot(x = time, y = H[,1], xlab = "Time (sec)", ylab = "ln water tank level (cm)", main = "Plot of simulated water level in tank",ylim = g_range) # Plot water level in subsequent tanks using loop for(n in 2:ncell){ points(x = time, y = H[,n], xlab = "Time (sec)", ylab = "ln water tank level (cm)", main = "Plot of simulated water level in tank",col = (20+n*5)) }

```
#the colour number is increased within the loop
```

## Leave a Reply