我正在尝试用java创建一个简单的SIR-流行病模型的模拟程序。
基本上,SIR是由三个微分方程系统定义的:
S'(t) =- l(t) * S(t)
I'(t) = l(t) * S(t) - g(t) * I(t)
R'(t) = g(t) * I(t)
S易感人群,I感染人群,R恢复人群。
l(t) =c*x* I(t) / N(T)
C-接触数,x-传染性(接触病人后发病的概率),N(t) -总人口(不变)。
我如何在Java中求解这样的微分方程?我不认为我知道任何有用的方法来做到这一点,所以我的实现产生了垃圾。
public class Main {
public static void main(String[] args) {
int tppl = 100;
double sppl = 1;
double hppl = 99;
double rppl = 0;
int numContacts = 50;
double infectiveness = 0.5;
double lamda = 0;
double duration = 0.5;
double gamma = 1 / duration;
for (int i = 0; i < 40; i++) {
lamda = (numContacts * infectiveness * sppl) / tppl;
hppl = hppl - lamda * hppl;
sppl = sppl + lamda * hppl - gamma * sppl;
rppl = rppl + gamma * sppl;
System.out.println (i + " " + tppl + " " + hppl + " " + sppl + " " + rppl);
}
}}
我会非常感谢任何帮助,非常感谢提前!
发布于 2010-12-05 11:11:20
时间序列微分方程可以通过取dt =一个小数字,并使用几个numerical integration techniques之一,例如Euler's method或Runge-Kutta来进行数值模拟。Euler的方法可能是原始的,但它对某些方程工作得很好,而且它足够简单,你可以尝试一下。例如:
S'(t) =- l(t) * S(t)
I'(t) = l(t) * S(t) - g(t) * I(t)
R'(t) = g(t) * I(t)
int N = 100;
double[] S = new double[N+1];
double[] I = new double[N+1];
double[] R = new double[N+1];
S[0] = /* initial value */
I[0] = /* initial value */
R[0] = /* initial value */
double dt = total_time / N;
for (int i = 0; i < 100; ++i)
{
double t = i*dt;
double l = /* compute l here */
double g = /* compute g here */
/* calculate derivatives */
double dSdt = - I[i] * S[i];
double dIdt = I[i] * S[i] - g * I[i];
double dRdt = g * I[i];
/* now integrate using Euler */
S[i+1] = S[i] + dSdt * dt;
I[i+1] = I[i] + dIdt * dt;
R[i+1] = R[i] + dRdt * dt;
}困难的部分是弄清楚要使用多少个步骤。你应该读一读我链接的一篇文章。更复杂的微分方程解算器使用可变步长,以适应每一步的精度/稳定性。
实际上,我会推荐使用数值软件,如R或Mathematica或MATLAB或Octave,因为它们包含常微分方程求解器,您不需要自己去做所有的麻烦。但是,如果您需要在较大的Java应用程序中执行此操作,至少首先使用数学软件进行尝试,然后了解步长和求解器的工作方式。
祝好运!
https://stackoverflow.com/questions/4357061
复制相似问题