library(ggplot2)
library(tidyr)
library(lpSolve)

Q2-P13

The following table gives the elongation e in inches per inch (in./in.) for a given stress S on a steel wire measured in pounds per square inch (lb/in^2). Test the model e=c1S by plotting the data. Estimate c1 graphically.

Construct dataframe and plot data points

df<-data.frame(
  S = c(5,10,20,30,40,50,60,70,80,90,100),
  e = c(0, 19, 57, 94, 134, 173, 216, 256, 297, 343, 390)
)

df$S<-df$S*10^-3
df$e<-df$e*10^5

ggplot(df,aes(x=S,y=e))+
  geom_point()+
  geom_line()+
  labs(title=" S vs e", x="S", y="e")

From the nearly linear graph, we could find the slope by using two points on the curve

C1 = (e7-e6) / (S7-S6)

b = e6 - C1 x S6
c1<-(df$e[7]-df$e[6])/(df$S[7]-df$S[6])
b=df$e[6]-c1*df$S[6]

df$y1<-c1*df$S+b

fy=paste0("e_predict = ",round(c1,2),"s ",round(b,2))

ggplot() +
  geom_line(data = df, aes(x = S, y = e, colour = "red")) +
  geom_line(data = df, aes(x = S, y = y1, colour ="green"))+
  xlab('S (lb/in^2)') +
  ylab('e & e_predict')+
  ggtitle(fy)+
  scale_color_discrete(name = "e series", labels = c("e", "e_predict"))

Using the least square build in lm function

fit <- lm(e ~ S, data=df)
print(fit)
## 
## Call:
## lm(formula = e ~ S, data = df)
## 
## Coefficients:
## (Intercept)            S  
##    -2540713    406933045
c1<-fit$coefficients[2]
b <-fit$coefficients[1]
df$y2<-c1*df$S+b

fy=paste0("y_pre = ",round(c1,2),"s ",round(b,2))

ggplot() +
  geom_line(data = df, aes(x = S, y = e, colour = "red")) +
  geom_line(data = df, aes(x = S, y = y2, colour ="green"))+
  xlab('S') +
  ylab('y & y_prediction')+
  ggtitle(fy)+
  scale_color_discrete(name = "Y series", labels = c("Y", "Y_pre"))

The least square fits perfectly the original data

Q2.a -P121

For each of the following data sets, formulate the mathematical model that minimizes the largest deviation between the data and the line y=ax+b. If a computer is available, solve for the estimates of a and b.
df<-data.frame(
x= c(1.0, 2.3, 3.7, 4.2, 6.1, 7.0),
y= c(3.6, 3.0, 3.2, 5.1, 5.3, 6.8)
)

ggplot(df,aes(x=x,y=y))+
  geom_point()+
  geom_line()+
  labs(title=" x vs y", x="x", y="ey")

initial <- c(1, 0, 0)

# build matrix of coefficients 
cr <- rep(1, 12)
ca <- c(1, -1, 1, -1, 1, -1, 1, -1, 1, -1, 1, -1)
cx <- c(1, -1, 2.3, -2.3, 3.7, -3.7, 4.2, -4.2, 6.1, -6.1, 7, -7)
matrix <- cbind(cr, ca, cx)

# the y vector
y <- c(3.6, -3.6, 3, -3, 3.2, -3.2, 5.1, -5.1, 5.3, -5.3, 6.8, -6.8)

# solve linear program 
slp <- lp("min", initial, matrix, rep(">=", 12), y)

# print coefficients r, a, and b, respectively
result<-slp$solution

print(paste0("y = ",round(result[3],3),"x + ",round(result[2],3)))
## [1] "y = 0.533x + 2.147"

Use the linear equation to predict the y values

df$y1<-result[3]*df$x+result[2]

fy=paste0("y_predict = ",round(result[3],3),"x + ",round(result[2],3))

ggplot() +
  geom_line(data = df, aes(x = x, y = y, colour = "green")) +
  geom_line(data = df, aes(x = x, y = y1, colour ="red"))+
  xlab('x') +
  ylab('y & y_predict')+
  ggtitle(fy)+
  scale_color_discrete(name = "Y series", labels = c("Y", "Y_predict"))

The slope produces a good result with minimum deviation