# 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.

## We'll cover the following

## 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:

*v _{t}* = 4 × e

^{–t}

At what time will the particle be traveling at 2 meters per second? That is, for what value of

*t*is

*v*equal to 2?

_{t}## Discussion

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

4 × e^{–t} – 2 = 0

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

*t*_{1} = 1 + *t*_{0} – [e^{t0}] / 2

We then take the value of *t*_{1} as a new guess *t*_{0} and compute a new *t*_{1}. 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, *t*_{0} and *t*_{1}, are almost the same. That is, you take *t*_{1} as the result when

|*t*_{1} – *t*_{0}| ≤ |*t*_{1}| × ε

where the vertical bars indicate the absolute value, or magnitude, and ε is a small positive number.
The smaller ε is, the closer together *t*_{0} and *t*_{1} 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.