updown: Solve a simple equation using a step halving algorithm.

View source: R/lmfor.R

updownR Documentation

Solve a simple equation using a step halving algorithm.

Description

Solves equations of form f(x)=0, for scalar x (l<=x<=u) using a simple step halving algorithm, where f(x) is a monotonic continuous function. Initial finite upper and lower bounds for x are required. The algorithm first computes f for x=u and x=l. If the sign was different then another call is performed at the midpoint x=(u+l)/2, and the midpoint is taken as a new upper or lower bound, according to the location of sign change. The upper or lower bound are repeatedly updated until the absolute value of f at the midpoint is below a specified criteria.

Usage

updown(l, u, fn, crit = 6)

Arguments

l

The initial lower bound

u

The initial upper bound

fn

R-function for f(x)

crit

The convergence criteria (Maximum accepted value of f at the solution is 10^{-\code{crit}}).

Value

A scalar giving the value of x at the solution. If the sign did not change between l and u, NA is returned.

Warning

May lead to infinite loop for non-continuous functions. Works only with monotonic functions.

Author(s)

Lauri Mehtatalo <lauri.mehtatalo@uef.fi>

Examples

## Compute the median of Weibull distibution
fn<-function(x) pweibull(x,5,15)-0.5
updown(1,50,fn)

lmfor documentation built on April 30, 2022, 1:08 a.m.