首页 > 编程语言 >python0011

python0011

时间:2024-05-22 08:59:01浏览次数:15  
标签:__ 方程 fd print python0011 x0

编写程序,使用牛顿迭代法求方程在x附近的一个实根

def solution(a, b, c, d):
    x = 1.5
    x0 = x
    f = a * x0**3 + b * x0**2 + c * x0 + d
    fd = 3 * a * x0**2 + 2 * b * x0 + c
    h = f / fd
    x = x0 - h
    while abs(x - x0) >= 1e-5:
        x0 = x
        f = a * x0**3 + b * x0**2 + c * x0 + d
        fd = 3 * a * x0**2 + 2 * b * x0 + c
        h = f / fd
        x = x0 - h
 
    return x
 
if __name__ == '__main__':
    print("请输入方程的系数: ")
    a, b, c, d = map(float, input().split())
    print("方程的参数为:", a, b, c, d)
    x = solution(a, b, c, d)
    print("所求方程的根为 x=%.6f" % x)

 

标签:__,方程,fd,print,python0011,x0
From: https://www.cnblogs.com/Lyh3012648079/p/18205362

相关文章