爱意满满的作品展示区。
ototsuyume

用python写了个解方程的脚本

  •  
  •   ototsuyume · Apr 29, 2012 · 5388 views
    This topic created in 5133 days ago, the information mentioned may be changed or developed.
    地址:https://github.com/ototsuyume/function-s-root

    先把输入的方程解析成语法树,然后求导数,再用牛顿迭代法循环求解,例如:
    ./parse.py "x^2-20000"
    ['x', '^', '2', '-', '20000']
    <expr.sub_expr object at 0x108301510>
    ('before differention:', '(x^(2))-20000')
    ('Differention:', '1*2*(x^(1))-0')
    ('Simplify:', '2*x')
    Iterating times:1 value:10000.500000
    Iterating times:2 value:5001.249950
    Iterating times:3 value:2502.624475
    Iterating times:4 value:1255.308043
    Iterating times:5 value:635.620194
    Iterating times:6 value:333.542762
    Iterating times:7 value:196.752544
    Iterating times:8 value:149.201536
    Iterating times:9 value:141.624207
    Iterating times:10 value:141.421502
    Iterating times:11 value:141.421356
    The root of the function is:141.421356

    存在一个问题是不知道怎么找出第一次迭代时比较好的x值,用随机数发现很容易超出python的整数范围,于是设了初始值为1,看来对大部分的方程都有效,hmmmmmm

    比较少用python,代码风格或许不好,望轻喷
    No Comments Yet
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   1006 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 22:16 · PVG 06:16 · LAX 15:16 · JFK 18:16
    ♥ Do have faith in what you're doing.