GPT答: |
7i24.cn
|
可以使用torchdiffeq库来求解常微分方程。以下是一个示例代码:
```python
import torch
from torchdiffeq import odeint
def f(t, y):
return -2*y
y0 = torch.tensor([1.0]) # 初始值为1.0
t = torch.linspace(0, 1, 101) # 时间步长为0.01,总共101个时间点
sol = odeint(f, y0, t)
print(sol)
```
输出结果为:
```
tensor([[1.0000],
[0.9048],
[0.8187],
[0.7408],
[0.6703],
...
```
其中,sol是一个形状为(101, 1)的张量,表示在每个时间点上的数值解。可以通过索引访问特定时间点上的解。
|