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")

Battle of Trafalgar problem (Problem 3 on page 54)

In analyzing the battle of Trafalgar in 1805, we saw that if the two forces simply engaged head-on, the British lost the battle and approximately 24 ships, whereas the French-Spanish force lost approximately 15 ships.We also saw that Lord Nelson could overcome a superior force by employing a divide-and-conquer strategy. An alternative strategy for overcoming a superior force is to increase the technology employed by the inferior force. Suppose that the British ships were equipped with superior weaponry, and that the French-Spanish losses equaled 15% of the number of ships of the opposing force, whereas the British suffered casualties equal to 5% of the opposing force.

a. Formulate a system of difference equations to model the number of ships possessed by each force. Assume the French-Spanish force starts with 33 ships and the British start with 27 ships.

Let B represent the Number of British ships and F represent the Number of French-Spanish ships

Therefore we can formulate the difference equations as:

\[B_{n+1} = B_{n} - 0.05 F_{n}\] \[F_{n+1} = F_{n} - 0.15 B_{n}\] \[B_{0} = 27\] \[F_{0} = 33\]

  1. Build a numerical solution to determine who wins under the new assumption in a head-on engagement.
B <- vector(length=12)
F <- vector(length=12)

B[1] <- 27
F[1] <- 33 

for(i in 2:12)
{
  B[i] <- B[i-1] - 0.05 * F[i-1]
  F[i] <- F[i-1] - 0.15 * B[i-1]
  
}


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

library(ggplot2)
library(reshape)
library(data.table)

df <- data.frame(British = B, French_Spanish = F)

df <- melt(df)
## Using  as id variables
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="Trafalgar Battle Simulation",x="Time unit",y="Ships")

Therefore British army would win.

  1. Build a numerical solution for the three battles employing Nelson’s divide-and-conquer strategy coupled with the superior weaponry of the British ships.

The Nepolean army is divided into 3 groups A (with 3 ships), B (with 17 ships) and C(with 13 ships). Lord Nelson’s (British Army) strategy was to engage force A with 13 British ships (holding 14 in reserve). He then planned to combine those ships that survived the skirmish against force A with the 14 ships in reserve to engage force B. Finally, after the battle with force B, he planned to use all remaining ships to engage force C. (As given in the book)

Case-A

13 ships of British will fight with 3 ships (Group A) of French-Spanish ships:

B <- vector(length=4)
F <- vector(length=4)

B[1] <- 13
F[1] <- 3 

for(i in 2:4)
{
  B[i] <- B[i-1] - 0.05 * F[i-1]
  F[i] <- F[i-1] - 0.15 * B[i-1]
  
}


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

library(ggplot2)
library(reshape)
library(data.table)

df <- data.frame(British = B, French_Spanish = F)
df
##    British French_Spanish
## 1 13.00000       3.000000
## 2 12.85000       1.050000
## 3 12.79750      -0.877500
## 4 12.84137      -2.797125
df <- melt(df)
## Using  as id variables
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="Trafalgar Battle Simulation",x="Time unit",y="Ships")

Hence British will win. The British will have 12.85 ships, while the French will have just 1.05 ships in the second Time Unit.

Case-B

26.85 ships (12.85+14) ships of British will fight with 18.05 (17+1.05) ships (Group A remaining ships + Group B ships) of French-Spanish army:

B <- vector(length=10)
F <- vector(length=10)

B[1] <- 26.85
F[1] <- 18.05

for(i in 2:10)
{
  B[i] <- B[i-1] - 0.05 * F[i-1]
  F[i] <- F[i-1] - 0.15 * B[i-1]
  
}


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

library(ggplot2)
library(reshape)
library(data.table)

df <- data.frame(British = B, French_Spanish = F)
df
##     British French_Spanish
## 1  26.85000      18.050000
## 2  25.94750      14.022500
## 3  25.24638      10.130375
## 4  24.73986       6.343419
## 5  24.42269       2.632440
## 6  24.29106      -1.030962
## 7  24.34261      -4.674622
## 8  24.57634      -8.326014
## 9  24.99264     -12.012465
## 10 25.59327     -15.761362
df <- melt(df)
## Using  as id variables
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="Trafalgar Battle Simulation",x="Time unit",y="Ships")

The British wins again with 24.7 ships remaining, while French-Spanish are left with 2.6 ships.

Case-C

24.42 ships ships of British will fight with 15.6 (13+2.6) ships (Group A remaining ships + Group B ships + Group C ships) of French-Spanish army:

B <- vector(length=10)
F <- vector(length=10)

B[1] <- 24.4
F[1] <- 15.6

for(i in 2:10)
{
  B[i] <- B[i-1] - 0.05 * F[i-1]
  F[i] <- F[i-1] - 0.15 * B[i-1]
  
}


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

library(ggplot2)
library(reshape)
library(data.table)

df <- data.frame(British = B, French_Spanish = F)
df
##     British French_Spanish
## 1  24.40000      15.600000
## 2  23.62000      11.940000
## 3  23.02300       8.397000
## 4  22.60315       4.943550
## 5  22.35597       1.553078
## 6  22.27832      -1.800318
## 7  22.36833      -5.142066
## 8  22.62544      -8.497316
## 9  23.05030     -11.891132
## 10 23.64486     -15.348678
df <- melt(df)
## Using  as id variables
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="Trafalgar Battle Simulation",x="Time unit",y="Ships")

The British wins finally with 22.27 number of ships survived.