我正在尝试求解色散方程:
w^2 = k*g * tanh(kh)
我有一个w的向量(25x1元素)输入,想要一个k的向量输出。我在下面尝试过,但是高度依赖于uniroot.all的公差值:
g = 9.81 #m/s^2
h = 8 #m
w = c(0.1,0.2,0.3) # 3 element vector for ease
dispersion <- function(k) { 0 == k*g*tanh(k*h)^0.5-w }
k1 <- uniroot.all(function(k) dispersion(1e4), c(-10,10), tol = 1e-100, maxiter = 1000)
您可以使用apply函数,如下所示:
# Parameters
g <- 9.81
h <- 8
w <- c(0.1, 0.2, 0.3)
# Function to find root of
disp_root <- function(k, w) {k * g * tanh(k * h) - w^2 }
# Apply for each w
res <- sapply(w, function(x)rootSolve::uniroot.all(disp_root, c(-1,1), w = x))
# Repackage results
df_res <- data.frame(w, t(res))
# Fix names
names(df_res)[2:3] <- c("first_root", "second_root")
# Examine results
df_res
#> w first_root second_root
#> 1 0.1 -0.01126658 0.01126658
#> 2 0.2 -0.02272161 0.02272161
#> 3 0.3 -0.03419646 0.03419646
由reprex软件包(v0.3.0)创建于2020-01-31
谢谢。为完整起见,它将是(k * g * tanh(k * h))^ 0.5-w
很高兴知道
uniroot.all