【问题标题】:Newton-Raphson's method user input and numerical output problemsNewton-Raphson 方法用户输入和数值输出问题
【发布时间】:2017-07-15 21:52:22
【问题描述】:

我一直在尝试创建一个脚本,允许用户输入一个方程式并返回该方程式的根。但是我遇到了一个问题,我注意到在运行程序时它会接受输入并通过循环运行它,但它不会将变量分配给函数。

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)

非常感谢您对改进的任何帮助,但您能否深入解释任何错误和改进,谢谢。

【问题讨论】:

  • 你为什么要在f(symx) 中要求输入函数,在你让他们猜测之前你肯定想问函数吗?
  • 是的,这更合乎逻辑。我想我并没有真正考虑它,因为这不是我的主要问题,但我已经改变了它并且它解决了循环问题,但我仍然无法实现对根的分析解决方案。

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


【解决方案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

【讨论】:

  • 感谢您的回答,有没有办法绘制这个函数?
  • 你想绘制2*x**3+2*x** 2吗?
  • 我要绘制输入的函数,不一定是那个函数
  • 在你的情况下:sp.plotting.plot(f(symx))
【解决方案2】:

我对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
>>> 

希望对你有帮助:)

【讨论】:

    猜你喜欢
    • 2018-07-16
    • 1970-01-01
    • 2015-05-08
    • 1970-01-01
    • 2015-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多