Not reaching to the correct answer using Newton Raphson Method

207 Views Asked by At

I am trying to find the inverse of the following function using Newton Raphson Method: (This is the function) by transforming it into this. Here, k is a constant, and we know the value of y hence it also becomes a constant and we want to find the value of x.

I tried to write a python code for the same, with around 20000 iterations but it is not converging to the correct value. Can someone give me any hint where am I going wrong. The values of y and k are 0.64 and 0.583 and the first guess for x is 0.4. The expected value of x is 0.651 but even after number of iterations, I am getting -7.88. This is the first time I am working on such a problem.

Here is the code: (func_ is the differentiation of func)

import math

def NTUCalculator(x,y,k):
    func = (x/(1-math.exp(-x))) + ((k*x)/(1-math.exp(-k*x))) - 1 - 1/y
    func_ = (1-math.exp(-x)) - (x*math.exp(-x)) + (k*(1-math.exp(-k*x))) - (k*k*x*math.exp(-k*x))
    return x - (func/func_)

NTU = 0.4

for i in range(20000):
    NTU = NTUCalculator(NTU,0.64,0.583)

NTU

Graphfxvsx

2

There are 2 best solutions below

0
On

If the equation is $$\frac 1y={\frac{x}{1-e^x}+\frac{k x}{1-e^{-k x}}-1}$$ with $y=0.64$ and $k=0.583$, the solution is $x=4.10048$

Using Newton with $x_0=0$, the iterates are $$\left( \begin{array}{cc} n & x_n \\ 0 & 0.00000 \\ 1 & 3.23752 \\ 2 & 4.08909 \\ 3 & 4.10047 \\ 4 & 4.10048 \end{array} \right)$$

Even if you work with a constant derivative equal to $0.7915$ at $x=0$, you will converge to the same result after $15$ iterations.

0
On

As Jaap Scherphuis has pointed out twice, the first derivative of your function used in the program is wrong. The correct expression for $f'\left(x\right)$ is $$f'\left(x\right) = \cfrac{1}{1-e^x}+ \cfrac{xe^x}{\left(1-e^x\right)^2}+ \cfrac{k}{1-e^{-kx}}- \cfrac{k^2x e^{-kx}}{\left(1-e^{-kx}\right)^2}.$$

If you rectify this error, as Claude Leibovici has shown in his answer, the Newton-Raphson iteration requires mere four steps to converge to the correct answer.