Answer to Question #238362 in Quantitative Methods for Harshitha

Question #238362
write coding to find the real root for the equation xe^x-2=0 and correct it to 3 decimal places using Newton's raphson method
1
Expert's answer
2021-09-21T14:44:01-0400
from math import exp

def f(x):
    return x*exp(x) -2

def fp(x):
    return exp(x) * (1+x)

def newton_raphson(x0, f, fp, eps, max_iter=100, show=False):
    iter = 1
    x = x0 - f(x0)/fp(x0)
    if show:
        print(' # |  root')
        print(f'{0:2} | {x0:.4f}')
        print(f'{iter:2} | {x:.4f}')

    while abs(x-x0) > eps:
        iter += 1
        if iter > max_iter:
            print(f'No convergence in {max_iter} iterations')
            break
        x0 = x
        x = x0 - f(x0)/fp(x0)
        if show:
            print(f'{iter:2} | {x:.4f}')
    return x

newton_raphson(1, f, fp, 0.001, show=True)

 # | root

 0 | 1.0000

 1 | 0.8679

 2 | 0.8528

 3 | 0.8526



Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS