R Code for the example-1 in 1.4 section in the book “First Course in Mathematical Modeling (5th edition)”

Example 1: Car rental company

o <- vector(length=10)
t <- vector(length=10)

o[1] <- 7000
t[1] <- 0

for(i in 2:10)
{
  o[i] <- 0.6*o[i-1] + 0.3*t[i-1]
  t[i] <- 0.4*o[i-1] + 0.7*t[i-1]
  
}


#install.packages("reshape")
#install.packages("data.table")
#install(ggplot2)

library(ggplot2)
library(reshape)
library(data.table)
## 
## Attaching package: 'data.table'
## 
## The following object is masked from 'package:reshape':
## 
##     melt
df <- data.frame(Orlando = o, Tampa = t)

df <- melt(df)
## Using  as id variables
df[df$variable=="Orlando",]
##    variable    value
## 1   Orlando 7000.000
## 2   Orlando 4200.000
## 3   Orlando 3360.000
## 4   Orlando 3108.000
## 5   Orlando 3032.400
## 6   Orlando 3009.720
## 7   Orlando 3002.916
## 8   Orlando 3000.875
## 9   Orlando 3000.262
## 10  Orlando 3000.079
df$iter <- seq(df$variable=='Orlando')

dt <- data.table(df)

dt[,id:=1:.N,by=variable]

ggplot(dt,aes(x=id,y=value,color=variable))+
  geom_point(size=3)+
  geom_line()+
  labs(title="Equilibrium values plot",x="Day",y="# of Cars")