求解微分方程但出现错误“'Add' 对象不可调用”。我正在使用 Jupyter 笔记本
Solving a differential equations but an error appears saying "'Add' object is not callable". I am using Jupyter Notebook
为了更清楚,我想绘制钟摆阻尼振荡的二阶微分方程的解。 Link 维基有关所用方程式的信息:https://en.wikipedia.org/wiki/Harmonic_oscillator
from sympy.interactive import printing
printing.init_printing(use_latex=True)
import numpy as np
import scipy as sp
from sympy import*
mport sympy as syp
`from scipy.integrate import odeint
import matplotlib.pyplot as plt
t=syp.Symbol('t')
x=syp.Function('x')(t)
m=2.0
k=5.0
a=0.5
z=a/(2.0*np.sqrt(m*k))
w=np.sqrt(k/m)
eq=x.diff(t,t)+2.0*z*w*x.diff(t)+w**2.0*x
dsolve(eq,t,0,ics={eq(1.0):0,eq(2.0):5})
您没有按预期构建 ics
参数:
In [6]: dsolve(eq, ics={x.subs(t, 1.0): 0, x.subs(t, 2.0): 5})
Out[6]:
-0.125⋅t
x(t) = (-0.0346285740992263⋅sin(1.57619002661481⋅t) - 6.42012708343871⋅cos(1.57619002661481⋅t))⋅ℯ
如果不使用浮点数,答案会更好(主观地)。此外,我发现将变量 x
保留为函数 x
而不是应用函数 x(t)
更自然,例如:
In [15]: x = Function('x')
In [16]: x
Out[16]: x
In [17]: x(t)
Out[17]: x(t)
In [18]: eq = x(t).diff(t, 2) + x(t).diff(t)/4 + 5*x(t)/2
In [19]: eq
Out[19]:
d
──(x(t)) 2
5⋅x(t) dt d
────── + ──────── + ───(x(t))
2 4 2
dt
In [20]: dsolve(eq, x(t), ics={x(1): 0, x(2): 5})
Out[20]:
⎛ 1/4 ⎛√159⋅t⎞ ⎞ -t
⎜5⋅ℯ ⋅sin⎜──────⎟ ⎟ ───
⎜ ⎝ 8 ⎠ 1/4 ⎛√159⋅t⎞⎟ 8
x(t) = ⎜────────────────── - 5⋅ℯ ⋅cos⎜──────⎟⎟⋅ℯ
⎜ ⎛√159⎞ ⎝ 8 ⎠⎟
⎜ tan⎜────⎟ ⎟
⎝ ⎝ 8 ⎠ ⎠
为了更清楚,我想绘制钟摆阻尼振荡的二阶微分方程的解。 Link 维基有关所用方程式的信息:https://en.wikipedia.org/wiki/Harmonic_oscillator
from sympy.interactive import printing
printing.init_printing(use_latex=True)
import numpy as np
import scipy as sp
from sympy import*
mport sympy as syp
`from scipy.integrate import odeint
import matplotlib.pyplot as plt
t=syp.Symbol('t')
x=syp.Function('x')(t)
m=2.0
k=5.0
a=0.5
z=a/(2.0*np.sqrt(m*k))
w=np.sqrt(k/m)
eq=x.diff(t,t)+2.0*z*w*x.diff(t)+w**2.0*x
dsolve(eq,t,0,ics={eq(1.0):0,eq(2.0):5})
您没有按预期构建 ics
参数:
In [6]: dsolve(eq, ics={x.subs(t, 1.0): 0, x.subs(t, 2.0): 5})
Out[6]:
-0.125⋅t
x(t) = (-0.0346285740992263⋅sin(1.57619002661481⋅t) - 6.42012708343871⋅cos(1.57619002661481⋅t))⋅ℯ
如果不使用浮点数,答案会更好(主观地)。此外,我发现将变量 x
保留为函数 x
而不是应用函数 x(t)
更自然,例如:
In [15]: x = Function('x')
In [16]: x
Out[16]: x
In [17]: x(t)
Out[17]: x(t)
In [18]: eq = x(t).diff(t, 2) + x(t).diff(t)/4 + 5*x(t)/2
In [19]: eq
Out[19]:
d
──(x(t)) 2
5⋅x(t) dt d
────── + ──────── + ───(x(t))
2 4 2
dt
In [20]: dsolve(eq, x(t), ics={x(1): 0, x(2): 5})
Out[20]:
⎛ 1/4 ⎛√159⋅t⎞ ⎞ -t
⎜5⋅ℯ ⋅sin⎜──────⎟ ⎟ ───
⎜ ⎝ 8 ⎠ 1/4 ⎛√159⋅t⎞⎟ 8
x(t) = ⎜────────────────── - 5⋅ℯ ⋅cos⎜──────⎟⎟⋅ℯ
⎜ ⎛√159⎞ ⎝ 8 ⎠⎟
⎜ tan⎜────⎟ ⎟
⎝ ⎝ 8 ⎠ ⎠