Java 中的微分方程

发布于 2024-10-05 16:31:57 字数 1009 浏览 5 评论 0原文

我正在尝试用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); 
    }
}

我将非常感谢任何帮助,非常感谢

I am trying to create a simple simulation program of SIR-epidemics model in java.

Basically, SIR is defined by a system of three differential equations:
S'(t) = - l(t) * S(t)
I'(t) = l(t) * S(t) - g(t) * I(t)
R'(t) = g(t) * I(t)

S - susceptible people, I - infected people, R - recovered people.

l(t) = [c * x * I(t)] / N(T)

c - number of contacts, x - infectiveness (probability to get sick after contact with sick person), N(t) - total population (which is constant).

How can I solve such differential equations in Java? I don't think I know any useful way to do that, so my implementation produces rubbish.

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); 
    }
}

}

I would greatly appreciate any help, many thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

っ左 2024-10-12 16:31:58

时间序列微分方程可以通过取 dt = 一个小数,并使用几个 数值积分技术 例如欧拉方法,或龙格-库塔。欧拉方法可能很原始,但它对于某些方程来说效果很好,而且很简单,您可以尝试一下。例如:

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 等数值软件,因为它们包含 ODE 求解器,您不需要自己费尽心思。但是,如果您需要将其作为较大 Java 应用程序的一部分来执行此操作,至少首先使用数学软件进行尝试,然后了解步长是什么以及求解器的工作原理。

祝你好运!

Time-series differential equations can be simulated numerically by taking dt = a small number, and using one of several numerical integration techniques e.g. Euler's method, or Runge-Kutta. Euler's method may be primitive but it works OK for some equations and it's simple enough that you might give it a try. e.g.:

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;
}

The tough part is figuring out how many steps to use. You should read one of the articles I have linked to. More sophisticated differential equation solvers use variable step sizes that adapt to accuracy/stability for each step.

I would actually recommend using numerical software like R or Mathematica or MATLAB or Octave, as they include ODE solvers and you wouldn't need to go to all the trouble yourself. But if you need to do this as part of a larger Java application, at least try it out first with math software, then get a sense of what the step sizes are and what solvers work.

Good luck!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文