find_function_1param_value_divideby2: find_function_1param_value_divideby2

Description Usage Arguments Value Examples

Description

Given f(x) = y where f is a monotonic function, find x for a given y.
Use a naive iterative approach dividing and mulitplying steps by 2 until a solution is found within desired precision.
Non-monotic functions are supported when the search range is limited to a monotonic subset of the function's range.
WARNING: This algorithm is naive, i.e. performance-wise suboptimal.

Usage

1
2
3
4
find_function_1param_value_divideby2(f, y_target_value, x_first_guess = NULL,
  x_first_step = NULL, x_search_limit_min = NULL,
  x_search_limit_max = NULL, y_precision = NULL, max_iteration = NULL,
  verbosity = NULL, ...)

Arguments

f

A function receiving a numeric value as its first parameter followed by ...

y_target_value

The output of f(x) for which we want to find x.

x_first_guess

(Conditional) An initial best-effort first guess of what x could be.

x_first_step

(Conditional) An initial step length to find better better values.

x_search_limit_min

(Conditional) A strict limit below which x will not be searched.

x_search_limit_max

(Conditional) A strict limit above which x will not be searched.

y_precision

(Conditional) An acceptable degree of precision. Default: .01.

max_iteration

(Conditional) The maximum number of search loops performed by the function. This is a safeguard to avoid infinite loops. Default: 2048.

verbosity

(Conditional) When verbosity > 0 detailed information will be output to the console.

Value

Numeric vector. x such that f(x) = y_target_value.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
precision <- .0001
y_target <- runif(n = 1, min = -1000000, max = +1000000)
c <- runif(n = 1, min = -1000000, max = +1000000)
m <- runif(n = 1, min = -1000000, max = +1000000)
f <- function(x) { return(c + m * x) }
x_found <- find_function_1param_value_divideby2(
  f = f,
  y_target_value = y_target,
  y_precision = precision,
  verbosity = verbosity)
y_found <- f(x_found)
delta <- abs(y_target - y_found)
label <- paste0(
  "f(x): c + m * x",
  "\nc: ", fn(c,8),
  "\nm: ", fn(m,8),
  "\ny_target: ", fn(y_target,8),
  "\nx_found: ", fn(x_found,8),
  "\ny_found: ", fn(y_found,8),
  "\ndelta: ", fn(delta,8))
message(label)

daviddoret/GRCRToolkit documentation built on May 23, 2019, 7:31 a.m.