Home > other >  To ask a question about sympy related, Newton's method to calculate the function
To ask a question about sympy related, Newton's method to calculate the function

Time:12-12



 # - * - coding: utf-8 - * - 
The from sympy import *
Import the random

X=symbols (" x ")
Func=cos (x) - * exp x (x) # x - (cos (x) - * exp x (x))/(sin (x) - * * exp (1 + x) (x))
Ffunc=diff (func, x)
The begin=1
End=2
MAXSTEP=100

Step_count=0

X0=random uniform (begin and end)
Temp=func. Subs (x, x0)

While step_count & lt; MAXSTEP and abs (temp) & gt; 1 e - 6:
X0=x0 - (temp/(ffunc. Subs (x, x0)))
Temp=func. Subs (x, x0)
Step_count +=1
Print 'k=' step_count
Print 'Xk=' x0


So calculate if not
K=5
Xk=0.517757527209265
The correct answer should be
K=5
XK=0.567126

CodePudding user response:

Program that's right, solve the root of it, you said that the results of this generation in the correct answer is not zero,
  • Related