Instantly share code, notes, and snippets.

# robbrit/gist:1530380 Created Dec 28, 2011

What would you like to do?
World oil production/consumption
 # oil production per day by country production = read.csv("production.csv", header = T) # proven oil reserves by country reserves = read.csv("reserves.csv", header = T) # oil consumption by country consumption = read.csv("consumption.csv", header = T) # the total amount of oil production per year total_production = sum(production\$Production) * 365 # total world proven reserves total_reserves = sum(reserves\$Reserves) # total world consumption per year total_consumption = sum(consumption\$Consumption) * 365 # derivation of time formula # # Take the simple exponential growth function # q is initial quantity, r is growth rate, t is time # # f(t) = q * exp(r * t) # # Total usage over time is the integral of f(t): # # V(T) = int(f(t), 0, T) # = int(q * exp(r * t), 0, T) # = q / r * (exp(r * T) - 1) # # See when this hits some limit L # # L = V(T) # = q / r * (exp(r * T) - 1) # # Solve for T # # T = ln(L * r / q + 1) / r # initial quantity is current production q = total_production # set growth rate to 1.18% r = 0.0118 # set limit equal to current proved reserves L = total_reserves print(sprintf("Current reserves are %e, current consumption is %e, current production is %e.", total_reserves, total_consumption, total_production)) # this is when we will hit that limit t = log(L * r / q + 1) / r print(sprintf("Assuming no production growth, we will hit L in %f years.", total_reserves / total_production)) print(sprintf("Assuming a growth rate of %f%% per year, we will hit L in %f years.", round(r * 100, 2), t)) # now for consumption q = total_consumption t = log(L * r / q + 1) / r print(sprintf("Assuming no consumption growth, we will hit L in %f years.", total_reserves / total_consumption)) print(sprintf("Assuming a growth rate of %f%% per year, we will hit L in %f years.", round(r * 100, 2), t))