-
-
Notifications
You must be signed in to change notification settings - Fork 4.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
KeyError in dsolve matching system of 1st order nonlinear ODEs #16910
Comments
Hi! I am newbie in GSOC. So, I would like to work on this issue. I am a math major student and have some experience with Python. I hope I can handle it. Let me know if it still open. |
I'm getting the same error with a basic SIR model. Any ideas? |
Can you show the equations that you are using? Note that "basic" equations like this very often don't have any analytic solution so although there is a bug here there isn't necessarily any reason to expect a useful output. |
That might be the reason, this is my code: from sympy import *
from sympy.abc import t
# Contact rate, beta, and mean recovery rate, gamma, (in 1/days).
beta, gamma = 0.2, 1./10
S = Function('S')(t)
I = Function('I')(t)
R = Function('R')(t)
Sdt = S.diff(t)
Idt = I.diff(t)
Rdt = R.diff(t)
Sdt_right = -1 * beta * S * I
Idt_right = beta * S * I - gamma * I
Rdt_right = gamma * I
eq1 = Eq(Sdt, Sdt_right)
eq2 = Eq(Idt, Idt_right)
eq3 = Eq(Rdt, Rdt_right)
ODEs = [eq1,eq2,eq3]
dsolve(ODEs, [S,I,R]) |
The ODEs are In [21]: eq1
Out[21]:
d
──(S(t)) = -0.2⋅I(t)⋅S(t)
dt
In [22]: eq2
Out[22]:
d
──(I(t)) = 0.2⋅I(t)⋅S(t) - 0.1⋅I(t)
dt
In [23]: eq3
Out[23]:
d
──(R(t)) = 0.1⋅I(t)
dt There are parametric solutions given here but they seem fairly complex:
In [46]: sol = dsolve(nsimplify(eq4))
In [47]: sol
Out[47]:
⎛ 2⋅i⎞
-W⎝C₁⋅ℯ ⎠
s(i) = ────────────
2
In [48]: solve(sol.subs(s(i), S), i)
Out[48]:
⎡ ⎛ -2⋅S(t) ⎞⎤
⎢ ⎛ _________________⎞ ⎜-2⋅S(t)⋅ℯ ⎟⎥
⎢ ⎜ ╱ -2⋅S(t) ⎟ log⎜─────────────────⎟⎥
⎢ ⎜ ╱ -S(t)⋅ℯ ⎟ log(2) ⎝ C₁ ⎠⎥
⎢log⎜- ╱ ─────────────── ⎟ + ──────, ──────────────────────⎥
⎣ ⎝ ╲╱ C₁ ⎠ 2 2 ⎦
In [49]: eq1p = eq1.subs(I, _[1])
In [50]: eq1p
Out[50]:
⎛ -2⋅S(t) ⎞
d ⎜-2⋅S(t)⋅ℯ ⎟
──(S(t)) = -0.1⋅S(t)⋅log⎜─────────────────⎟
dt ⎝ C₁ ⎠
In [51]: dsolve(eq1p)
Out[51]:
S(t)
⌠
⎮ 1
⎮ ─────────────────────────── dy = C₂ - 0.1⋅t
⎮ ⎛ ⎛ -2⋅y ⎞ ⎞
⎮ ⎜ ⎜-y⋅ℯ ⎟ ⎟
⎮ y⋅⎜log⎜─────────⎟ + log(2)⎟
⎮ ⎝ ⎝ C₁ ⎠ ⎠
⌡ That just ends up with the result given implicitly which is presumably similar to the parametric solutions from wikipedia. |
This example comes from stackoverflow:
https://stackoverflow.com/questions/16909779/any-way-to-solve-a-system-of-coupled-differential-equations-in-python
The system of ODEs is
and the result:
Working on this a bit I get another error:
The text was updated successfully, but these errors were encountered: