
# -*- coding: utf-8 -*-
from sympy import *
import random
x = symbols("x")
func = cos(x) - x * exp(x) # x - (cos(x) - x * exp(x)) / (sin(x) - (1+x)**exp(x))
ffunc = diff(func, x)
begin = 1
end = 2
MAXSTEP = 100
step_count = 0
x0 = random.uniform(begin, end)
temp = func.subs(x, x0)
while step_count < MAXSTEP and abs(temp) > 1e-6:
x0 = x0 - (temp / (ffunc.subs(x, x0)))
temp = func.subs(x, x0)
step_count += 1
print 'k =', step_count
print 'Xk =', x0
這樣算出來好像不對啊
k = 5
Xk = 0.517757527209265
正確答案應該是
K=5
XK=0.567126
uj5u.com熱心網友回復:
程式沒錯,解出來的根也沒錯,你說的這個正確答案代進去結果不是零。轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/233222.html
