R 基于循环计算行

R 基于循环计算行,r,loops,iteration,R,Loops,Iteration,我试图实现一种迭代方法,使用霍顿渗透方程计算径流降雨量。下面描述的代码实现了第一行(time=0) #入口 f0=6 f1=1 k=2 dt=0.25 f=0 时间=顺序(从=0到=2,由=dt) inc_降雨量=c(0.3,0.4,0.5,0.6,0.7,0.8,0.4,0.6,0.6) 降雨量=inc\u降雨量/dt #足球俱乐部 gfc=功能(fc){ f-((f0-fc)/k)+((f1/k)*log((fc-f1)/(f0-f1))} fc=uniroot(gfc,interval=c

我试图实现一种迭代方法,使用霍顿渗透方程计算径流降雨量。下面描述的代码实现了第一行(
time=0

#入口
f0=6
f1=1
k=2
dt=0.25
f=0
时间=顺序(从=0到=2,由=dt)
inc_降雨量=c(0.3,0.4,0.5,0.6,0.7,0.8,0.4,0.6,0.6)
降雨量=inc\u降雨量/dt
#足球俱乐部
gfc=功能(fc){
f-((f0-fc)/k)+((f1/k)*log((fc-f1)/(f0-f1))}
fc=uniroot(gfc,interval=c(f0,f1))
fc=fc$root
#F'
fl=如果(fc>降雨量[1])和(f,包括降雨量[1]),则为0
#fc'
gfcl=功能(fcl){
fl-((f0-fcl)/k)+((f1/k)*log((fcl-f1)/(f0-f1))}
fcl=uniroot(gfcl,区间=c(f0,f1))
fcl=fcl$根目录
#Fp ou Fs

fporfs=ifelse(fc这应该能让你更接近。我把你的方程放在一个函数中,我在for循环中使用了这个函数。我不得不修改你的一些公式,使它工作起来。我对水文学一无所知,但在浏览了你链接的书和一个小维基百科之后,这些修改是有意义的。这些修改还导致数据更接近于t在表中。查找
#!!!!!!!!!!!!!!!!!!!!!!!!
以查看我更改了什么

在第五次迭代/行中,公式
ftdt=ifelse(to==0,fl,f1*(到时间)+(f0-f1)/k*(1-exp(-k*(到时间)))
产生了错误的结果,这会破坏其他结果。我将
f1
替换为
fc
,因为在阅读了本书的相关页面后,它是有意义的,并且结果更接近您想要的结果。我唯一不确定的是时间变量t,即公式中的
time-to
,即时间mi新加坡国立大学时间偏移。我认为这可能是问题所在

compute\u horton->->使用horton渗透方程计算径流降雨量
#常数
f0=6
f1=1
k=2
#足球俱乐部
gfc=功能(fc){
f-((f0-fc)/k)+((f1/k)*log((fc-f1)/(f0-f1)))
}
fc=uniroot(gfc,interval=c(f0,f1))
fc=fc$root
#F'
fl=如果(fc>降雨量)和(f,包括降雨量)其他0
#fc'
gfcl=功能(fcl){
fl-((f0-fcl)/k)+((f1/k)*log((fcl-f1)/(f0-f1)))
}
fcl=uniroot(gfcl,区间=c(f0,f1))
fcl=fcl$根目录
#Fp ou Fs

fporfs=ifelse(fc感谢您的贡献。在此基础上,我清除了代码,并在以前没有的地方插入了
ifelse
函数

但是,在循环中使用第一行中计算的
ftdt
值启动第二行仍然有困难

按照设定的代码进行操作。我曾想过对每一行执行
,但我想不出一种有效的方法来保持代码的整洁

#Constants
f0=6
f1=1
k=2
dt=0.25
f=0
time= seq(from=0, to=2, by=dt)
inc_rainfall=c(0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.4, 0.6, 0.6)
rainfall_int=inc_rainfall/dt

horton=data.frame(matrix(ncol = 14, nrow = 9))
names(horton)=c("Time", "Incremental Rainfall", "Rainfall Intensity", "F", "fc",
                "Fl", "Fcl", "Fp or Fs", "dt", "ts", "to", "Ftdt", "Infiltration",
                "Runoff")

#g(Fc)
gfc=ifelse (f==0, 

  function(fc) {
  f - ((f0 - fc) / k) + ((f1 / k) * log((fc - f1) / (f0 - f1)))}, 

  function(fc) {
  ftdt[i-1] - ((f0 - fc) / k) + ((f1 / k) * log((fc - f1) / (f0 - f1)))})

fc=uniroot(gfc, interval = c(f0, f1))
fc=fc$root

#F'
fl=ifelse(fc>rainfall_int, f+inc_rainfall, 0)

#fc'
gfcl= ifelse (fl==0,

  0,

  function(fcl) {
  fl - ((f0 - fcl) / k) + ((f1 / k) * log((fcl - f1) / (f0 - f1)))})

fcl=uniroot(gfcl, interval = c(f0, f1))
fcl=fcl$root

#Fp or Fs
fporfs=ifelse(fc<rainfall_int[1],f,
              ifelse(fcl<rainfall_int[1],
                     (f0-rainfall_int[1])/k-
                       f1/k*log((rainfall_int[1]-f1)/(f0-f1)), 0))

#dt'
dtl=ifelse((fc>rainfall_int[1]), 
           ifelse(fcl<rainfall_int[1],
                  (fporfs-f)/rainfall_int[1],0),0)

#ts
ts=ifelse(fc<rainfall_int[1],time[1],
          ifelse(fcl<rainfall_int[1],dtl+time[1], 0))

#to
hto=function(to) {
  fporfs-f1*(ts-to)-(f0-f1)/k*(1-exp(-k*(ts-to)))}

to=uniroot(hto, interval = c(0, 1))
to=to$root

#Ft+Dt
ftdt=ifelse(to==0, fl, f1*(time-to)+(f0-f1)/k*(1-exp(-k*(time-to)))) #Value that starts next row at "gFc"

#Infiltration
infiltr=ifelse(to==0,ftdt-f, ftdt[i]-ftdt[i-1])

runoff=ifelse(time==0, inc_rainfall[i]-(ftdt),  inc_rainfall[i]-(ftdt[i-1]-ftdt[i]))


horton$Time=time
horton$`Incremental Rainfall`=inc_rainfall
horton$`Rainfall Intensity`=rainfall_int
horton$F[1]=f
horton$fc[1]=fc
horton$Fl[1]=fl
horton$Fcl[1]=fcl
horton$`Fp or Fs`[1]=fporfs
horton$dt[1]=dtl
horton$ts[1]=ts
horton$to[1]=to
horton$Ftdt[1]=ftdt
horton$Infiltration[1]=infiltr
horton$Runoff[1]=runoff

horton
#常数
f0=6
f1=1
k=2
dt=0.25
f=0
时间=顺序(从=0到=2,由=dt)
inc_降雨量=c(0.3,0.4,0.5,0.6,0.7,0.8,0.4,0.6,0.6)
降雨量=inc\u降雨量/dt
horton=data.frame(矩阵(ncol=14,nrow=9))
名称(霍顿)=c(“时间”、“增量降雨”、“降雨强度”、“F”、“fc”,
“Fl”、“Fcl”、“Fp或Fs”、“dt”、“ts”、“to”、“Ftdt”、“渗透”,
“径流”)
#g(Fc)
gfc=ifelse(f==0,
功能(fc){
f-((f0-fc)/k)+((f1/k)*log((fc-f1)/(f0-f1))},
功能(fc){
ftdt[i-1]-((f0-fc)/k)+((f1/k)*log((fc-f1)/(f0-f1)))
fc=uniroot(gfc,interval=c(f0,f1))
fc=fc$root
#F'
fl=ifelse(fc>降雨量,f+inc降雨量,0)
#fc'
gfcl=ifelse(fl=0,
0,
功能(fcl){
fl-((f0-fcl)/k)+((f1/k)*log((fcl-f1)/(f0-f1)))
fcl=uniroot(gfcl,区间=c(f0,f1))
fcl=fcl$根目录
#Fp或Fs

fporfs=ifelse(你有没有试过把所有的东西都放在一个函数中,然后在一个循环中对变量进行迭代?我对代码做了一些修改,如下所述。@DiegoStähelin我恐怕不能在计算的细节方面帮你很多忙,因为我对水文学一无所知。不过,上面的代码工作得很好,只不过是calc计算有些不对劲。我建议调整上述代码中的方程式,以获得所需的结果。@DiegoStähelin还有一件事。您看过R包吗?它有一个名为
SShorton
的函数,返回霍顿水渗透模型。
#Constants
f0=6
f1=1
k=2
dt=0.25
f=0
time= seq(from=0, to=2, by=dt)
inc_rainfall=c(0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.4, 0.6, 0.6)
rainfall_int=inc_rainfall/dt

horton=data.frame(matrix(ncol = 14, nrow = 9))
names(horton)=c("Time", "Incremental Rainfall", "Rainfall Intensity", "F", "fc",
                "Fl", "Fcl", "Fp or Fs", "dt", "ts", "to", "Ftdt", "Infiltration",
                "Runoff")

#g(Fc)
gfc=ifelse (f==0, 

  function(fc) {
  f - ((f0 - fc) / k) + ((f1 / k) * log((fc - f1) / (f0 - f1)))}, 

  function(fc) {
  ftdt[i-1] - ((f0 - fc) / k) + ((f1 / k) * log((fc - f1) / (f0 - f1)))})

fc=uniroot(gfc, interval = c(f0, f1))
fc=fc$root

#F'
fl=ifelse(fc>rainfall_int, f+inc_rainfall, 0)

#fc'
gfcl= ifelse (fl==0,

  0,

  function(fcl) {
  fl - ((f0 - fcl) / k) + ((f1 / k) * log((fcl - f1) / (f0 - f1)))})

fcl=uniroot(gfcl, interval = c(f0, f1))
fcl=fcl$root

#Fp or Fs
fporfs=ifelse(fc<rainfall_int[1],f,
              ifelse(fcl<rainfall_int[1],
                     (f0-rainfall_int[1])/k-
                       f1/k*log((rainfall_int[1]-f1)/(f0-f1)), 0))

#dt'
dtl=ifelse((fc>rainfall_int[1]), 
           ifelse(fcl<rainfall_int[1],
                  (fporfs-f)/rainfall_int[1],0),0)

#ts
ts=ifelse(fc<rainfall_int[1],time[1],
          ifelse(fcl<rainfall_int[1],dtl+time[1], 0))

#to
hto=function(to) {
  fporfs-f1*(ts-to)-(f0-f1)/k*(1-exp(-k*(ts-to)))}

to=uniroot(hto, interval = c(0, 1))
to=to$root

#Ft+Dt
ftdt=ifelse(to==0, fl, f1*(time-to)+(f0-f1)/k*(1-exp(-k*(time-to)))) #Value that starts next row at "gFc"

#Infiltration
infiltr=ifelse(to==0,ftdt-f, ftdt[i]-ftdt[i-1])

runoff=ifelse(time==0, inc_rainfall[i]-(ftdt),  inc_rainfall[i]-(ftdt[i-1]-ftdt[i]))


horton$Time=time
horton$`Incremental Rainfall`=inc_rainfall
horton$`Rainfall Intensity`=rainfall_int
horton$F[1]=f
horton$fc[1]=fc
horton$Fl[1]=fl
horton$Fcl[1]=fcl
horton$`Fp or Fs`[1]=fporfs
horton$dt[1]=dtl
horton$ts[1]=ts
horton$to[1]=to
horton$Ftdt[1]=ftdt
horton$Infiltration[1]=infiltr
horton$Runoff[1]=runoff

horton