**R-exercises**, and kindly contributed to R-bloggers)

In the first exercise of simple spatial modeling here, we learned to create a model that considers more spatial variability. However, it relies on an isolated system where we set the q1 and q6 as zero. In this exercise, we try to bring the model into a more realistic space by adding some boundary conditions, as well as considering its numerical instability.

Here, the boundary condition is referred to as water level and/or water flow as the two main variables that can be played. There are basically two options to do that; Dirchlet and von Neuman boundary conditions. The Dirchlet method is just simply defined as q1 and q6 to any number. It shows that the flows q1 and q6 are remaining constant over the simulation. Von Neuman allows the model to define the q1 and q6 based on any other factors that might influence. For example, the level of a river here is represented as “tank water level.”

Answers to these exercises are available here. If you obtained a different (correct) answer than those listed on the solutions page, please feel free to post your answer as a comment on that page.

**Dirchlet Boundary Conditions**

**Exercise 1**

Try to modify your latest model with a new step: defining the q1 and q6 as a constant value. The model script will look exactly the same as the previous one, just add a constant value declaration of q1 and q6 as the first and the sixth on the q matrix output.

**Exercise 2**

Plot the model simulation.

**Von Neuman Boundary Conditions**

**Exercise 3**

Instead of defining the q1 and q6, try to define the water level on tank 1 and 5. Pay attention while running the models; you only need to calculate water storage for bucket 2 to 5.

**Exercise 4**

Plot the data.

**Numerical Instability**

As we learned, rounding off and truncation might generate some errors in the final result of the model. Therefore, it is important to maintain a relatively small time-step. Furthermore, shortening the time-step will give more useful information. The exercises below will try to simulate how to run the model for 500 time-steps, but only outputting every 10th.

**Exercise 5**

Set initial information and output matrix q and H for calculated water storage. Set all the initial values, including H and k. Use the Dirchlet boundary conditions value on the model script above.

**Exercise 6**

This is the tricky part. Take a deep breath. The purpose of this step is to get the model output and save the data every time-step. Since we will only output for every 10th, we need to create a matrix output that defines the rule.

**Exercise 7**

Create an output array and size equals number outputs by the number of tanks.

**Exercise 8**

Another consideration is that we need to tell the model that only the 10th calculation will be outputted by creating a loop for output time.

**Exercise 9**

Tell the model which row the data should be stored; it is increased by one each time. Also, define the output timer that will reset back to zero when the number of time-steps equal the number when water levels outputted (10th).

**Exercise 10**

Run the model and add conditional formatting (`if function`

)to the output section. Ask the script whether the increased output timer equals the threshold output value (10). If so, then increase our output count by 1 and output data in the next row. Then, reset our output timer back to zero so that the calculation will repeat again.

**Exercise 11**

Plot the data.

**leave a comment**for the author, please follow the link and comment on their blog:

**R-exercises**.

R-bloggers.com offers

**daily e-mail updates**about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...