1
import math
import numpy as np
import matplotlib.pyplot as plt
def F(x):
    return x**5/(np.exp(x)-1)

def deriv(x):
    a=0.0001
    return (F(x+a)-F(x))/a

def newton_step(xi):
    return (xi-F(xi)/deriv(xi))

def newton(x0):
    list_of_approx=[]
    error=1
    while error>pow(10,-6):
        xnext=newton_step(x0)
        error=abs(xnext-x0)
        list_of_approx.append(xnext)
        xo=xnext
        
    return list_of_approx

def main():
    step=1
    initial_guess=float(input("Please give initial guess:"))
    list_of_approx=newton(initial_guess)
    for i in list_of_approx:
        print("Approx"+str(step)+" = "+str(i))
        step=step+1
        
main()

My code is to find the root of the first derivative of F(x) with newton's method.When I input the initial guess of 5,it cannot print out the result.Plz help me to fix My code!Thx!

john chong
  • 35
  • 3
  • Can you explain what "cannot print out the result" means? Are you getting an error? Or just nothing happens? – Timothy G. Apr 17 '22 at 04:21
  • when I input the result, nothing happen – john chong Apr 17 '22 at 04:33
  • Graph your function. The slope is nearly zero, and negative. The updated guess puts you way out near `x = 150` and the slop there is practically zero. Your guesses will trend toward infinity. Do you understand how newton's method works? You will never reach a root with an initial guess of `x = 5` with this function. – ddejohn Apr 17 '22 at 04:35
  • That said, you're using `xo` and `x0` as variable names. Your guesses never actually update: `xo` should be `x0`; this is just a typo. – ddejohn Apr 17 '22 at 04:40

0 Answers0