Search⌘ K
AI Features

A Problem Solved: The Root of an Equation

Explore how to use the Newton-Raphson algorithm to find the root of a mathematical equation in Java. Understand how to implement iterative loops, manage precision with epsilon values, prevent infinite loops by limiting iterations, and refine your approach using Boolean variables for better code readability. This lesson helps you practice applying mathematical problem-solving techniques in Java programming.

Problem statement

As a certain particle travels, its velocity in meters per second is given as a function of the time t in seconds by the following formula:
  vt = 4 × et
At what time will the particle be traveling at 2 meters per second? That is, for what value of t is vt equal to 2?

Discussion

We are asked to find a value of t such that 4 × et = 2. An equivalent question asks for the value of t for which 4 × et – 2 is zero. This value of t is said to be the root of the equation:

4 × et – 2 = 0

The Newton-Raphson algorithm is a repetitive technique to find the root of an equation. This algorithm begins with a guess t0 at the root and computes another, hopefully, better, approximation of the root, t1. For this particular equation, the algorithm defines t1 by the following formula, whose derivation appears at the end of this lesson:

t1 = 1 + t0 – [et0] / 2

We then take the value of t1 as a new guess t0 and compute a new t1. If all goes well, we generate a sequence of numbers that get closer and closer to the desired result. If that happens, the approximations themselves will ...