title: “Lab 4” author: “John King” date: “May 10th, 2024” This script analyzes data from the infiltration lab and plots infiltration rate as a function of time using Horton’s equation.

# Reading excel file with lab data
datafile <- "Lab4.xlsx"
infilt_data <- readxl::read_excel(datafile)

# Setting up function for Horton equation
horton <- function(time, fc, f0, k) {
  fc + (f0 - fc) * exp(-k * time)
}
horton_fit <- stats::nls(Rate ~ horton(Time, fc, f0, k), data = infilt_data, 
                         start = list(fc=12, f0=60, k=0.1))
coef(horton_fit)
##           fc           f0            k 
##  47.66834734 199.69383948   0.05865991
# Plotting results of Horton equation
infilt_data$Rate_Horton <- predict(horton_fit, list(x = infilt_data$Time))
plot(infilt_data$Time,infilt_data$Rate, pch=1, 
     xlab = "Time, min", ylab = "Infiltration rate, cm/hr")
lines(infilt_data$Time, infilt_data$Rate_Horton, lty = 2)
abline(h=66.18)
legend("topright", legend = c("Obs.","Horton","Constant"), pch = c(1, NA, NA), lty = c(NA, 2, 1))