A Problem Solved: The Root of an Equation

In this lesson, we will use mathematics and our Java knowledge to find the root of an equation.

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?


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 get closer to each other. Thus, we stop computing when two successive estimates, t0 and t1, are almost the same. That is, you take t1 as the result when
|t1t0| ≤ |t1| × ε

where the vertical bars indicate the absolute value, or magnitude, and ε is a small positive number. The smaller ε is, the closer together t0 and t1 must be to satisfy the inequality.

First-draft code

Here are Java statements that compute the Newton-Raphson sequence for our given equation:

Get hands-on with 1200+ tech skills courses.