#*****************************************************************************80 # ## zero_test() tests zero(). # # Licensing: # # This code is distributed under the MIT license. # # Modified: # # 28 May 2021 # # Author: # # John Burkardt # cat ( date ( ), "\n" ) cat ( "\n" ) cat ( "zero_test():\n" ) cat ( " ", version$version.string, "\n" ) cat ( " zero() seeks the root of a function using Brent's method.\n" ) source ( "/home/burkardt/public_html/r_src/zero/zero.R" ) a <- 1.0 b <- 2.0 t <- 1.0E-07 f <- function ( x ) { sin(x) - x/2.0 } cat ( "\n" ) cat ( " f(x) = sin(x) - x/2\n" ) cat ( " Interval = [", a, ",", b, "]\n" ) cat ( " f(a) =", f(a), ", f(b) = ", f(b), "\n" ) output <- zero ( a, b, t, f ) x = output[1] calls = output[2] cat ( " zero() returns x = ", x, "\n" ); cat ( " f(",x,") = ", f(x), "\n" ) cat ( " The function was evaluated ", calls, " times.\n" ) a <- 0.0 b <- 1.0 t <- 1.0E-07 f <- function ( x ) { 2.0 * x - exp ( - x ) } cat ( "\n" ) cat ( " f(x) = 2 x - exp ( -x )\n" ) cat ( " Interval = [", a, ",", b, "]\n" ) cat ( " f(a) =", f(a), ", f(b) = ", f(b), "\n" ) output <- zero ( a, b, t, f ) x = output[1] calls = output[2] cat ( " zero() returns x = ", x, "\n" ); cat ( " f(",x,") = ", f(x), "\n" ) cat ( " The function was evaluated ", calls, " times.\n" ) a <- -1.0 b <- 0.5 t <- 1.0E-07 f <- function ( x ) { x * exp ( - x ) } cat ( "\n" ) cat ( " f(x) = x * exp ( -x )\n" ) cat ( " Interval = [", a, ",", b, "]\n" ) cat ( " f(a) =", f(a), ", f(b) = ", f(b), "\n" ) output <- zero ( a, b, t, f ) x = output[1] calls = output[2] cat ( " zero() returns x = ", x, "\n" ); cat ( " f(",x,") = ", f(x), "\n" ) cat ( " The function was evaluated ", calls, " times.\n" ) a <- 0.0001 b <- 20.0 t <- 1.0E-07 f <- function ( x ) { exp ( x ) - 1 / ( 100 * x * x ) } cat ( "\n" ) cat ( " f(x) = exp ( x ) - 1 / ( 100 * x * x )\n" ) cat ( " Interval = [", a, ",", b, "]\n" ) cat ( " f(a) =", f(a), ", f(b) = ", f(b), "\n" ) output <- zero ( a, b, t, f ) x = output[1] calls = output[2] cat ( " zero() returns x = ", x, "\n" ); cat ( " f(",x,") = ", f(x), "\n" ) cat ( " The function was evaluated ", calls, " times.\n" ) a <- -5.0 b <- +2.0 t <- 1.0E-07 f <- function ( x ) { (x+3)*(x-1)*(x-1) } cat ( "\n" ) cat ( " f(x) = (x+3)*(x-1)*(x-1)\n" ) cat ( " Interval = [", a, ",", b, "]\n" ) cat ( " f(a) =", f(a), ", f(b) = ", f(b), "\n" ) output <- zero ( a, b, t, f ) x = output[1] calls = output[2] cat ( " zero() returns x = ", x, "\n" ); cat ( " f(",x,") = ", f(x), "\n" ) cat ( " The function was evaluated ", calls, " times.\n" ) # # Terminate. # cat ( "\n" ) cat ( "zero_test():\n" ) cat ( " Normal end of execution.\n" ) cat ( date ( ), "\n" ) quit ( )