In this post I will shortly explain what Newton’s method is and how it works.
What is it:
- Newton’s method is an algorithm to find zeropoints of a function
- Because you cannot always calculate the exact zeropoint we can use this algorithm to approximate the zeros of the function
How does it work:
- Start at a random point A of the function
- Calculate the tangent T at this point A
- Calculate the intersection between the x-axis and the tangent T
- Take the x-value of the intersection point ( x-axis and the tangent T ), calculate the y-value ( f(x) ) → new Point B at (x|y)
- Repeat the steps 1- 4 (like 500 times) → approximation of the zeropoint
Important sidenote:
You should try to start with a nearby point to the actual zeropoint, because it might be that you would get a solution which is just wrong. This might be the case if you would get to a point with the slope of a tangent being 0, which does not have one intersection point with the x-axis (saddle point).
Interesting fact – Approximate square root numbers:
With this approximation we can also calculate/approximate square roots, like √2 (irrational number).
To calculate the square root of 2 with Newton’s method we would first create a function (polynomial) where f(√2) = 0 (zeropoint at √2).
How to achive this:
- x = √2| ^2
- x² = 2 | – 2
- x² – 2 = 0
Step by step math for Newton’s method:
- Start at point A and calculate the slope
- linear function is y=m*x+b → we got m (the calculated slope) and the point A throught which the line goes → calculate b: b=y-(m*x)
- To calculate the intersection of the tangent and the x-axis set y to 0: 0=m*x+b → we got m and b, so we need to calculate x → x = (y-b)/m = (0-b)/m
- Calculate the new point B of the function with f(x) and repeat the steps
Python code example to approximate √2 with Newton’s method: (github.com/Heuristic-Analyst/…)
def newtons_method(iterations:int):
x = 2
for i in range(iterations):
y = function_one(x)
m = function_one_derivative(x)
b = y-(m*x)
x=(0-b)/m
return x
def function_one(x:float):
return x**2-2
def function_one_derivative(x:float):
return 2*x
print(newtons_method(5))
>> Output: 1.414213562373095