Below are the solutions to these exercises on “Simple Numerical Modeling – Part 2.”

############### # # # Exercise 1 # # # ############### #previous work (ignore when you used the same script as previous exercise) datatable <- read.table(file.choose()) t<-0 k <- 0.05 S <- 15 timestep <- 1 ntimesteps <-100 output <- mat.or.vec(ntimesteps,2) for(n in 1:ntimesteps){ # complete loop for n = 1 to 100 dS <- k*S*timestep # for each run through calculate the change in storage S <- S-dS # The calculate the new water level t <- t+timestep # Calculate what the new time is output[n,1] = t # Outpt the time (sec) output[n,2] = S # Output the new water level (cm) } # same model presented # different matrix output t<-0 k <- 0.05 S <- 15 timestep <- 1 ntimesteps <-100 output2 <- mat.or.vec(ntimesteps,2) ############### # # # Exercise 2 # # # ############### #loop calculation for(n in 1:ntimesteps){ dS <- k*S*timestep dS <- round(dS,digits = 1) #the change is storage is rounded to 1 decimal place S <- S-dS t <- t+timestep output2[n,1] <- t output2[n,2] <- S } # end loop ############### # # # Exercise 3 # # # ############### plot(x = (output[,1]), y = output[,2], xlab = "Time (sec)", ylab = "ln water tank level (cm)", main = "Plot of simulated water level in tank") #create new points of the original model plot points(x = (output2[,1]), y = output2[,2], xlab = "Time (sec)", ylab = "ln water tank level (cm)", main = "Plot of simulated water level in tank", pch = 19,col="red")

#modified model obviously created high error on very low and very high water level ############### # # # Exercise 4 # # # ############### # Define new figures on timestep and ntimestep t<-0 k <- 0.05 S <- 15 timestep <- 4 ntimesteps <-25 # Creation new matrix output output3 <- mat.or.vec(ntimesteps,2) output3

## [,1] [,2] ## [1,] 0 0 ## [2,] 0 0 ## [3,] 0 0 ## [4,] 0 0 ## [5,] 0 0 ## [6,] 0 0 ## [7,] 0 0 ## [8,] 0 0 ## [9,] 0 0 ## [10,] 0 0 ## [11,] 0 0 ## [12,] 0 0 ## [13,] 0 0 ## [14,] 0 0 ## [15,] 0 0 ## [16,] 0 0 ## [17,] 0 0 ## [18,] 0 0 ## [19,] 0 0 ## [20,] 0 0 ## [21,] 0 0 ## [22,] 0 0 ## [23,] 0 0 ## [24,] 0 0 ## [25,] 0 0

############### # # # Exercise 5 # # # ############### #re-run loop for(n in 1:ntimesteps){ # complete loop for n = 1 to 100 dS <- k*S*timestep # change in storage calculation S <- S-dS # new water level calculation t <- t+timestep # new time step calculation output3[n,1] = t # Outpt the time (sec) output3[n,2] = S # Output the new water level (cm) } # ending loop ############### # # # Exercise 6 # # # ############### # replot the inital output plot(x = (output[,1]), y = output[,2], xlab = "Time (sec)", ylab = "ln water tank level (cm)", main = "Plot of simulated water level in tank") # plot the reduced time step model points(x = (output3[,1]), y = output3[,2], xlab = "Time (sec)", ylab = "ln water tank level (cm)", main = "Plot of simulated water level in tank",pch = 19) #the smaller timestep, the details of calculation will be #greater timestep creates error on the higher water tank level ############### # # # Exercise 7 # # # ############### #create simple regression and find the one with high adjusted R2 lin.reg <- lm(datatable$V2 ~ datatable$V1) #dependent variable first summary (lin.reg)

## ## Call: ## lm(formula = datatable$V2 ~ datatable$V1) ## ## Residuals: ## Min 1Q Median 3Q Max ## -1.3298 -0.6403 -0.2099 0.4934 2.6282 ## ## Coefficients: ## Estimate Std. Error t value Pr(>|t|) ## (Intercept) 7.828774 0.232879 33.62 <2e-16 *** ## datatable$V1 -0.137814 0.006985 -19.73 <2e-16 *** ## --- ## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 ## ## Residual standard error: 0.8676 on 55 degrees of freedom ## Multiple R-squared: 0.8762, Adjusted R-squared: 0.874 ## F-statistic: 389.3 on 1 and 55 DF, p-value: < 2.2e-16

exp.reg<-lm(log(datatable$V2) ~ (datatable$V1)) summary(exp.reg)

## ## Call: ## lm(formula = log(datatable$V2) ~ (datatable$V1)) ## ## Residuals: ## Min 1Q Median 3Q Max ## -0.45294 -0.09498 0.00494 0.08468 0.41281 ## ## Coefficients: ## Estimate Std. Error t value Pr(>|t|) ## (Intercept) 2.249247 0.053146 42.32 <2e-16 *** ## datatable$V1 -0.038397 0.001594 -24.09 <2e-16 *** ## --- ## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1 ## ## Residual standard error: 0.198 on 55 degrees of freedom ## Multiple R-squared: 0.9134, Adjusted R-squared: 0.9118 ## F-statistic: 580.3 on 1 and 55 DF, p-value: < 2.2e-16

#exponential regression with log transformation on dependent variable create high adjusted R2 #arrange the equation and plot curve (15*exp(-0.05*x), from = 1 , to = 100, add = TRUE, xlab = "Time (sec)", ylab = "ln water tank level (cm)", main = "Plot of simulated water level in tank",pch = 19,col="Red")

#regression is a very quick analysis to know a relationship between dependent and independent variables even though we don't know what the actual variables are #regression method in creating models need to be treated carefully as it might behave differently in other location or on the evry extreme values of water level #here regression analysis is just for comparing the mechanistic model that we have created to the mathematical based model #please be careful that loop will do iteration from previous calculation so that we need to declare parameter before run the loop, otherwise it will calculate from 101-200 and so on

## Leave a Reply