Newton-Raphson的方法用户输入和数字输出问题

时间:2017-02-24 22:18:50

标签: python python-3.x scipy sympy newtons-method

我一直在尝试创建一个脚本,允许用户输入一个等式并返回该等式的根。但是我遇到了一个问题而且我注意到在运行程序时它需要输入并在循环中运行它,但它并没有将变量分配给函数。

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.lines as lines
from matplotlib import style
from scipy.misc import derivative
import sympy as sp

symx = sp.Symbol('x')

def f(symx):
    tmp = sp.sympify(input("Input your function here: "))
    return tmp;

def fprime(symx):
    tmp = sp.diff(f(symx))
    return tmp;

def newtons_method(f, fprime, symx):   
    guess = int(input("Enter an initial guess: ")) # Convert to an int immediately.
    for i in range(1,10):
        nextGuess = guess - f(guess)/fprime(guess)
        print(nextGuess)
        guess = nextGuess

def main():
    newtons_method(f, fprime, symx)
if __name__ == "__main__":
    main()

这是脚本输出的内容;

Enter an initial guess: 2
Input your function here: 2*x**3 + 2*x**2
Input your function here: 2*x**3 + 2*x**2
2 - (2*x**3 + 2*x**2)/(6*x**2 + 4*x)
Input your function here: 2*x**3 + 2*x**2 
Input your function here: 2*x**3 + 2*x**2 
2 - 2*(2*x**3 + 2*x**2)/(6*x**2 + 4*x)

非常感谢任何有关改进的帮助,但您是否也可以深入解释任何错误和改进,谢谢。

2 个答案:

答案 0 :(得分:1)

除了没有必要传递函数的名称之外,你不应该在最初的时刻调用input函数。

import sympy as sp

x = sp.symbols('x')

def f(symx):
    tmp = sp.sympify(symx)
    return tmp

def fprime(symx):
    tmp = sp.diff(f(symx))
    return tmp;

def newtons_method():   
    guess = sp.sympify(float(input("Enter an initial guess: "))) # Convert to an int immediately.
    symx = input("Input your function here: ")
    div = f(symx)/fprime(symx)

    for i in range(1, 10):
        print(guess.evalf())
        nextGuess = guess - div.subs(x, guess)
        guess = nextGuess


def main():
    newtons_method()
if __name__ == "__main__":
    main()

测试:

Enter an initial guess: 2
Input your function here: 2*x**3 + 2*x**2
2.00000000000000
1.25000000000000
0.760869565217391
0.448024718605164
0.254024574811046
0.138693453631666
0.0733275286119194
0.0378747932767810
0.0192767426403216

Enter an initial guess: 2
Input your function here: x**2-2
2.00000000000000
1.50000000000000
1.41666666666667
1.41421568627451
1.41421356237469
1.41421356237310
1.41421356237309
1.41421356237310
1.41421356237309

答案 1 :(得分:0)

我不熟悉sympy模块,因此我使用.replacex替换为猜测,并使用eval()来计算结果

这些是我改变的功能:

def f(value):
    eq1 = eq.replace("x", str(value))
    tmp = eval(eq1) # sympy uses eval anyway
    return tmp;

def fprime(value):
    eq2 = str(sp.diff(eq).replace("x", str(value)))
    tmp = eval(eq2) 
    return tmp;

def newtons_method(f, fprime, symx):
    global eq
    eq = input("Input your function here: ") # ask for function first :)
    guess = int(input("Enter an initial guess: ")) # Convert to an int immediately.
    for i in range(1,10):
        nextGuess = guess - f(guess)/fprime(guess)
        print(nextGuess)
        guess = nextGuess

这是给出的输出:

>>> 
Input your function here: x**2 - 16
Enter an initial guess: 3
4.166666666666667
4.003333333333333
4.000001387732445
4.000000000000241
4.0
4.0
4.0
4.0
4.0
>>> 

希望这可以帮助你:)