常微分方程的python代码
时间: 2023-10-12 20:58:08 浏览: 118
常微分方程的数值解法(1)
5星 · 资源好评率100%
常微分方程的Python代码可以使用sympy库来实现。下面是几个例子:
1. 一阶常微分方程:
```python
from sympy import symbols, Function, Eq, diff, dsolve, pprint
x = symbols('x')
f = symbols('f', cls=Function)
eq = Eq(diff(f(x),x,1),f(x))
solution = dsolve(eq, f(x))
pprint(solution)
```
2. 二阶常微分方程:
```python
from sympy import symbols, Function, Eq, diff, dsolve, pprint
x, p, q = symbols('x p q')
f = symbols('f', cls=Function)
eq = Eq(diff(f(x),x,2) + p*diff(f(x),x,1) + q*f(x),0)
solution = dsolve(eq, f(x))
pprint(solution)
```
3. 不可求解解析解的常微分方程:
```python
from sympy import symbols, Function, Eq, diff, dsolve, pprint, sin
x, g, l = symbols('x g l')
f = symbols('f', cls=Function)
eq = Eq(diff(f(x),x,2) + g*sin(f(x))/l,0)
solution = dsolve(eq, f(x))
pprint(solution)
```
希望这些代码能够帮助到您。
阅读全文