Newton Method Kt Test Algorithm

The Newton Method Kt Test Algorithm, also known as the Newton-Raphson method, is an iterative numerical technique used for finding the roots or zeroes of a real-valued function. It is a powerful and widely-used algorithm in various scientific and engineering fields due to its rapid convergence and simplicity. The algorithm is based on the idea of linear approximation, where a function is approximated by its tangent line at a given point, and the root is estimated by the point where the tangent line intersects the x-axis. By repeating this process iteratively, the algorithm refines the estimate and converges to the true root. To apply the Newton-Raphson method, one must have an initial guess for the root, which is then refined through successive iterations. In each iteration, the tangent line is calculated at the current estimate, and the intersection point with the x-axis is found using the derivative of the function. This new point becomes the next estimate, and the process is repeated until the desired accuracy is achieved or a maximum number of iterations is reached. The success of the algorithm depends on the choice of the initial guess, the nature of the function, and the presence of multiple roots, among other factors. In some cases, the method may fail to converge, or it may converge to an incorrect root, necessitating the use of alternative techniques or modifications to the basic algorithm.
/*
 * Copyright (c) 2017 Kotlin Algorithm Club
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

package com.algorithmexamples.math

import org.junit.Test

import org.junit.Assert.*

class NewtonMethodKtTest {
    @Test
    fun sqrt() {
        assertTrue(sqrt(-2.0).isNaN())
        assertTrue(sqrt(-2.0, 0.toDouble()).isNaN())
        assertEquals(2.toDouble(), sqrt(4.toDouble()), 1e-15)
        assertEquals(4.toDouble(), sqrt(16.toDouble()), 1e-12)
        for (i in 7 until 99) {
            for (j in -15 until -1) {
                assertEquals(sqrt(i.toDouble()), sqrt(i.toDouble()), j.toDouble())
            }
        }
    }

    @Test
    fun sqrt1() {
        assertTrue(sqrt(-2).isNaN())
        assertEquals(2.toDouble(), sqrt(4), 1e-15)
        assertEquals(4.toDouble(), sqrt(16), 1e-12)
        for (i in 7 until 99) {
            for (j in -15 until -1) {
                assertEquals(sqrt(i), sqrt(i), j.toDouble())
            }
        }
    }
}

LANGUAGE:

DARK MODE: