I have the following code which solves the system of ODEs for some beta and gamma.
RK4 = function(f, a, b, x0, N){
h = ((b-a)/N)
time_seq = seq(a, b, h)
hat_x = matrix(0, ncol = length(x0), nrow = (N+1))
hat_x[1,] = x0
for(i in 1:N){
k1 = h*f(time_seq[i], hat_x[i,])
k2 = h*f(time_seq[i] + h/2, hat_x[i,] + k1/2)
k3 = h*f(time_seq[i] + h/2, hat_x[i,] + k2/2)
k4 = h*f(time_seq[i] + h, hat_x[i,] + k3)
hat_x[i+1,] = hat_x[i,] + (k1 + 2*k2 + 2*k3 + k4)/6
}
return(list(t=time_seq, x=hat_x))
}
SIR2 = function(beta, gamma, initial, time, N){
SIR_f = function(t,x){
f1 = -beta*x[1]*x[2]
f2 = beta*x[1]*x[2] - gamma*x[2]
f3 = gamma*x[2]
return(c(f1,f2,f3))
}
return(RK4(SIR_f,0, time, initial, N))
}
it produces a list of answers for f1,f2 and f3 like so
$x
[,1] [,2] [,3]
[1,] 0.9900000 0.010000000 0.000000000
[2,] 0.9884796 0.010495766 0.001024608
[3,] 0.9868866 0.011013539 0.002099887
[4,] 0.9852179 0.011554028 0.003228073
[5,] 0.9834706 0.012117937 0.004411473
i want to create a simliar program with more parameters as follows,
lockdown = function(beta, gamma, yin, yout, Lintensity, initial, time, N)
So that when the middle column (x[,2]) > yin, beta changes until x[,2] < yout.
Im not sure in what part of the code to put my conditions and what condition i could put so that the above is achieved.
Thanks for the help