ReZero's Utopia.

Sympy 解方程

字数统计: 306阅读时长: 1 min
2017/02/08 Share
  1. 解普通方程

    from sympy import *
    x = Symbol('x')
    y = Symbol('y')
    print solve([2 * x - y - 3, 3 * x + y - 7],[x, y])
* * *

![test](http://img.blog.csdn.net/20170208213138869?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvZ2l0aHViXzM1OTU3MTg4/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)

* * *
  1. 解微积分
    微积分

    from sympy import *
    n = Symbol('n')
    s = ((n+3)/(n+2))**n
    
    #无穷为两个小写o
    
    print limit(s, x, oo)
  2. 求定积分
    定积分

    from sympy import *
    t = Symbol('t')
    x = Symbol('x')
    m = integrate(sin(t)/(pi-t),(t,0,x))
    n = integrate(m,(x,0,pi))
    print n

    这里写图片描述

  3. 解微分方程

#y' = 2xy  的通解

from sympy import *
f = Function('f')
x = Symbol('x')
print dsolve(diff(f(x),x) - 2*f(x)*x,f(x))


#说明:

f = Function('f')
x = Symbol('x')

#表示f(x)的导:

diff(f(x), x, index)    
>>> diff(sin(x), x, 1)
cos(x)

dsolve(eq, f(x))
    #第一个参数为微分方程(要先将等式移项为右端为0的形式)
    #第二个参数为要解的函数(在微分方程中)
  1. 矩阵化简 这里写图片描述

    from sympy import *
    x1,x2,x3 = symbols('x1 x2 x3')
    a11,a12,a13,a22,a23,a33 = symbols('a11 a12 a13 a22 a23 a33')
    m = Matrix([[x1,x2,x3]])
    n = Matrix([[a11,a12,a13],[a12,a22,a23],[a13,a23,a33]])
    v = Matrix([[x1],[x2],[x3]])
    f = m * n * v
    f[0] 化简, subs代入计算
    print f[0].subs({x1:1, x2:1, x3:1})
CATALOG