Everything You Need To Know
my blog

Exploring Roots: A Python Journey with Newton-Raphson Method
What Is A Newton-Raphson Method?
The Newton-Raphson method is a numerical technique used to find the roots of a mathematical equation through an iterative approach. It is considered interesting due to its high convergence rate and efficiency in finding the roots, especially when the initial guess is close enough to the true root.
This method employs an iterative approach to find the root of a given equation. Starting with an initial guess for the root, the method produces a sequence of iterations that gradually approach the true root.
The key concept behind the Newton-Raphson method is that the tangent line to a curve at a certain point can provide a good estimation of the location of the root. The method finds the intersection point of the tangent with the x-axis as the next approximation for the root.
In the context of the Newton-Raphson method, if we want to find the root of the equation f(x) = 0, the next iteration can be calculated using the following formula:
xn+1 = xn - f(xn) / f'(xn)
where xn+1 is the next approximation of the root, xn is the current approximation at the nth iteration, f(xn) is the value of the function at xn, and f'(xn) is the first derivative of the function at xn.
The Newton-Raphson method exhibits fast convergence if the initial guess is sufficiently close to the true root. However, if the first derivative f'(xn) approaches zero, convergence may become unstable or even invalid.
Although the Newton-Raphson method has its limitations and may not guarantee convergence to a root, it remains an attractive choice for various applications. It is widely used in physics, engineering, economics, computer science, and other fields. Some practical applications include finding roots of nonlinear equations, solving systems of nonlinear equations, numerical optimization, and other numerical analyses.
Example
The world-renowned mathematician, Professor Mathius, has discovered an enigmatic equation that holds the secrets of ancient civilizations. The equation is given by:
f(x) = x^3 - 2x^2 + 3x - 6
Many scholars and students have attempted to unlock its mysteries, but so far, the equation remains unsolved. Your mission is to use the powerful Newton-Raphson method to find the roots of this intriguing equation.
You are provided with a starting guess of x1 = 3 and a maximum allowable error e = 0.00001. The Newton-Raphson method is a powerful numerical technique that helps you approximate the roots of equations.
Your task is to develop a program that will find the root of the equation with precision up to the given error, thus revealing the hidden secrets of the ancient civilization.
Solved:
Step 1: Define the function and its derivative
The function (f(x) = x^3 - 2x^2 + 3x - 6) and its derivative (f'(x) = 3x^2 - 4x + 3).
Step 2: Start the iteration
Let (x_1 = 3) be the initial guess.
Step 3: Iterate using the Newton-Raphson formula
The Newton-Raphson formula: (x_{n+1} = x_n - {f(x_n)}/{f'(x_n)}).
Repeat the following steps until (|x_{n+1} - x_n| < e), where (e) is the tolerance value (0.00001 in this case).
-
a.Calculate (f(x_n)).
b.Calculate (f'(x_n)).
c.Calculate the next approximation (x_{n+1}) using the formula above.
Step 4: Finalize the result
Once the iteration converges and the tolerance is met, the value of (x_{n+1}) is the approximate root of the equation.
Now, let's do the calculations:
Iteration 1:
(x_1 = 3) (f(x_1) = 3^3 - 2(3)^2 + 3(3) - 6 = 27 - 18 + 9 - 6 = 12) (f'(x_1) = 3(3)^2 - 4(3) + 3 = 27- 12 + 3 = 18) (x_2 = x_1 - {f(x_1)}/{f'(x_1)} = 3 - {12}/{18} = 2,33333)
Since the result of (x_2) is the same as (x_1), we can stop the iteration here as the tolerance is already met.
Step 5: Final Result
After several iterations, the newton-raphson method will converge, and you will find an approximate root within the tolerance error. The approximate root of the equation x^3 - 2x^2 + 3x - 6 using the newton-raphson method with the given initial guesses x1 = 3 approximately:
x ≈ 2.000001
Congratulations! You have successfully solved the problem and found the hidden root using the newton-raphson method.
In the usage of Microsoft Excel:

Source Code:
# Mencetak Judul Metode print("\nMETODE NEWTON-RAPHSON \n") #! Lambda merupakan function anonymous dalam python {tidak memiliki nama, seperti def fungsi() } #* f = lambda x : x**3-2*x**2+3*x-6 ----> Secara matematis f(x) = x**3-2*x**2+3*x-6 #? Keunggulan lambda, memiliki banyak argumen, namun memiliki satu ekspresi. #TODO Berguna dalam fungsi untuk melakukan proses subsitusi, dan sangat efisiensi dalam koding # Misalnya f(x) = x**3-2*x**2+3*x-6, ketika memanggil fungsi f(x1) menjadi f(x1)=x1**3-2*x1**2+3*x1-6 untuk dilakukan subsitusi f = lambda x**3-2*x**2+3*x-6 fdiff = lambda 3*x**2-4*x+3 #* Menentukan nilai x1, serta nilai acuan toleransi (eror) x1 = 3 e = 0.00001 #! Hitung nilai (Subsitusi) f(x1) dan f'(x1) fx1 = f(x1) fx1diff = fdiff(x1) #! Hitung nilai x2 dan f(x2) x2 = x1 - (fx1/fx1diff) fx2 = f(x2) #TODO Melakukan pengulangan, dengan iterasi maksimal 100 for i in range(1, 100): # Mencetak semua proses pengulangan, mulai dari iterasi, nilai x1, f(x1), f'(x1), f(x2), dan |f(x2)| print("Iterasi ", i) print("x1 \t\t=", x1) print("f(x1) \t\t=", fx1) print("f'(x1) \t\t=", fx1diff) print("f(x2) \t\t=", fx2) print("f(x2) \t\t=", fx2) print("|f(x2)| \t=", abs(fx2)) ##? Cek, jika memenuhi maka akan berhenti if abs(fx2) <= e: print("Keterangan \t= Berhenti\n") break #? Cek, jika tidak memenuhi akan melanjutkan perhitungan iterasi selanjutnya maka nilai x1 baru adalah x2 else: print("Keterangan \t= Lanjutkan Iterasi \n") x1 = x2 #TODO Perhitungan nilai f(x1), f'(x1), x2, dan f(x2) dalam pengulangan fx1 = f(x1) fx1diff = fdiff(x1) x2 = x1 - (fx1/fx1diff) fx2 = f(x2)
Program Result:
METODE NEWTON-RAPHSON Iterasi 1 x1 = 3 f(x1) = 12 f'(x1) = 18 x2 = 2.3333333333333335 f(x2) = 2.814814814814815 |f(x2)| = 2.814814814814815 Keterangan = Lanjutkan Iterasi Iterasi 2 x1 = 2.3333333333333335 f(x1) = 2.814814814814815 f'(x1) = 10.000000000000002 x2 = 2.0518518518518523 f(x2) = 0.37385683076766973 |f(x2)| = 0.37385683076766973 Keterangan = Lanjutkan Iterasi Iterasi 3 x1 = 2.0518518518518523 f(x1) = 0.37385683076766973 f'(x1) = 7.422880658436217 x2 = 2.0014863875569664 f(x2) = 0.010413553574590395 |f(x2)| = 0.010413553574590395 Keterangan = Lanjutkan Iterasi Iterasi 4 x1 = 2.0014863875569664 f(x1) = 0.010413553574590395 f'(x1) = 7.011897728499639 x2 = 2.0000012612790594 f(x2) = 8.82895977838416e-06 |f(x2)| = 8.82895977838416e-06 Keterangan = Berhenti[Done] exited with code=0 in 0.523 seconds