Programming-Notes
Naomi Tague
January 28, 2016
Code from inclass examples
Working with data types and functions
# create a function that computes power generated from a reservoir
power_gen = function(height, flow, rho=1000, g=9.8, Keff=0.8) {
result = rho * height * flow * g * Keff
return(result)
}
# examples of function use
power_gen(20,1)
## [1] 156800
power_gen(height=20, flow=1)
## [1] 156800
power.estimate = power_gen(height=20,flow=1)
power.estimate
## [1] 156800
Scoping
Nicely documented function
#' Power Required by Speed
#'
#' This function determines the power required to keep a vehicle moving
#' a given speed
#' @param cdrag coefficient due to drag default=0.3
#' @param crolling coefficient due to rolling/friction default=0.015
#' @param v vehicle speed (m/2)
#' @param m vehicle mass (kg)
#' @param A area of front of vehicle (m2)
#' @param g acceleration due to gravity (m/s) default=9.8
#' @param pair (kg/m3) default =1.2
#' @return power (W)
power = function(cdrag=0.3, crolling=0.015,pair=1.2,g=9.8,V,m,A) {
P = crolling*m*g*V + 1/2*A*pair*cdrag*V**3
return(P)
}
v=seq(from=0, to=100, by=10)
plot(v, power(V=0.447*v, m=31752, A=25))
lines(v, power(V=0.447*v, m=61752, A=25))
## Data types in R (that you can exchange with functions)
mth.names=c("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct"
,"Nov","Dec")
reservoir.operation = data.frame(month=mth.names)
reservoir.operation$height = seq(from=32, to=10, by=-2)
reservoir.operation$flowrate = rnorm(n=12, mean=3, sd=0.25)
barplot( power_gen(height=reservoir.operation$height, flow=reservoir.operation$flow), names=reservoir.operation$month)
Using other functions within a function
#'
#' This function computes total power generation from a reservoir given its height and flow rate into turbines and number of days (and secs) within those days that the turbines are in operation
#' @param rho Density of water (kg/m3) Default is 1000
#' @param g Acceleration due to gravity (m/sec2) Default is 9.8
#' @param Keff Turbine Efficiency (0-1) Default is 0.8
#' @param height height of water in reservoir (m)
#' @param flow flow rate (m3/sec)
#' @param number of days
#' @param secs in days Default is 86400
#' @author Naomi
#' @examples power_gen(20, 1, 10)
#' @return Power generation (MW)
power_gen_total = function(height, flow, days, secs=86400, rho=1000, g=9.8, Keff=0.8) {
result = rho * height * flow * g * Keff
result = result * days * secs
total = sum(result)/1e6
return(total)
}
power_gen_total(reservoir.operation$height, reservoir.operation$flowrate, days=30)
## [1] 15138407
Using lists to return multiple data types from a function
#' computes profit from price for forest plot and Mg/C in that plot
#' @param price ($)
#' @param carbon (MgC)
#' @return list with mean, min, and max prices
compute_carbonvalue = function(price, carbon) {
cost.per.carbon = price/carbon
a = mean(cost.per.carbon)
b = max(cost.per.carbon)
c = min(cost.per.carbon)
result = list(avg=a, min=c, max=b)
return(result)
}
# example application, using some data that we 'generate'
obs=data.frame(prices=c(23,44,60,4,2,33,59), forestc=c(59,88,100,10,8,79,300) )
obs
## prices forestc
## 1 23 59
## 2 44 88
## 3 60 100
## 4 4 10
## 5 2 8
## 6 33 79
## 7 59 300
forestvalue = compute_carbonvalue(obs$prices, obs$forestc)
forestvalue
## $avg
## [1] 0.3934598
##
## $min
## [1] 0.1966667
##
## $max
## [1] 0.6