题目描述
解法:牛顿法(Python)
令 f ( x ) = x 3 − a f(x)=x^3-a f(x)=x3−a,求解 f ( x ) = x 3 − a = 0 f(x)=x^3-a=0 f(x)=x3−a=0
泰勒展开
f ( x ) = f ( x 0 ) + f ′ ( x 0 ) ( x − x 0 ) = x 0 3 − a + 3 x 0 2 ( x − x 0 ) = 0 f(x)=f(x_0)+f'(x_0)(x-x_0)=x_0^3-a+3x_0^2(x-x_0)=0 f(x)=f(x0)+f′(x0)(x−x0)=x03−a+3x02(x−x0)=0
解得
x = x 0 − x 0 3 − a 3 x 0 2 x=x_0-\frac{x_0^3-a}{3x_0^2} x=x0−3x02x03−a
while True:
try:
a = float(input())
e = 0.0001
res = a
while abs(res**3 - a) > e:
res = res - (res**3 - a) * 1.0 / (3 * res**2)
print("%.1f" % res)
except: break