Newton's Method for Optimization

Lesson Introduction

Welcome to our lesson on Newton's Method for Optimization! This method helps us find the lowest point in a valley (minimum) or the highest peak on a mountain (maximum). By the end of this lesson, you'll understand Newton's Method, how it works, and how to use it in Python.

Imagine you're on a hike, looking for the lowest point in a valley. Newton's Method will guide you step-by-step to this point.

Task Setup: Function to Minimize

We're starting with a function f(x)f(x) that we want to minimize. Let's use:

f(x)=x4−3x3+2f(x) = x^4 - 3x^3 + 2

Here's a plot of this function:

This plot shows the function's landscape with multiple local minima and maxima.

General Approach with Initial Guess

To minimize this function using Newton's Method, we start with an initial guess. Let's choose x0=3x_0 = 3. The choice here is simply random.

The red point shows our starting point. We will update this guess step by step, moving closer to the minimum.

Updating the Guess Using Newton's Method

Newton's Method updates our guess using the first and second derivatives. The update formula is:

xn+1=xn−f′(xn)f′′(xn)x_{n+1} = x_n - \frac{f'(x_n)}{f''(x_n)}

For our function f(x)=x4−3x3+2f(x) = x^4 - 3x^3 + 2:

  • f′(x)=4x3−9x2f'(x) = 4x^3 - 9x^2
  • f′′(x)=12x2−18xf''(x) = 12x^2 - 18x

Important note: the Newton's method is designed to find the critical point. It could be a minimum, maximum or a saddle point.

Python Implementation and Optimization Path: Part 1

Let's implement Newton's Method in Python and see it in action.

Python
def f_prime(x):
    return 4*x**3 - 9*x**2

def f_double_prime(x):
    return 12*x**2 - 18*x

def newtons_method(f_prime, f_double_prime, x0, max_iterations=10, tolerance=1e-6):
    x = x0
    steps = [x]  # Track the optimization path
    for _ in range(max_iterations):
        f_prime_value = f_prime(x)
        f_double_prime_value = f_double_prime(x)
        
        if abs(f_prime_value) < tolerance:
            break  # Convergence criterion
        
        x = x - f_prime_value / f_double_prime_value
        steps.append(x)
        
    return x, steps

Here, we take multiple steps according to the formula above. We stop once the first derivative is very close to zero, indicating the minimum is reached. The code also defines the maximum amount of iterations. It is needed in case the minimum of the function doesn't exist or won't be found because the process will stuck in a loop.

Our function keeps track of all the steps, so we can plot it later.

Sign up

Join the 1M+ learners on CodeSignal

Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal