uninewton: Univariate Newton method

Description Usage Arguments Value Examples

View source: R/uninewton.R

Description

Find the root of a function using Newton's method.

Usage

1
2
3
uninewton(f, df, x0, tol = 10 * .Machine$double.eps, maxit = 100L)

simple_uninewton(f, df, x0, tol = 10 * .Machine$double.eps, maxit = 100L)

Arguments

f

function

df

function; derivative of f

x0

initial value

tol

tolerance, defaults to 10*.Machine$double.eps

maxit

maximum number of iterations

Value

a list

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
f  <- function(x) x^2 - 2
df <- function(x) 2*x
x0 <- 2
uninewton(f, df, x0)
simple_uninewton(f, df, x0)

(out <- uninewton(f, df, x0))

curve(f(x), col = "red", from = .9, to = 2.1)
with(out$evals, points(x, fx))
for(k in 1:out$n_evals) {
  with(out$evals, abline(a = fx[k] - dfx[k]*x[k], b = dfx[k], col = "blue"))
  Sys.sleep(1)
}





f <- sin
df <- cos
x0 <- 2
(out <- uninewton(f, df, x0))

curve(f(x), col = "red", from = 0, to = 2*pi)
with(out$evals, points(x, fx))
for(k in 1:out$n_evals) {
  with(out$evals, abline(a = fx[k] - dfx[k]*x[k], b = dfx[k], col = "blue"))
  Sys.sleep(1)
}




f <- log
df <- function(x) 1/x
x0 <- .40
(out <- uninewton(f, df, x0))

curve(f(x), col = "red", from = .25, to = 1.5)
with(out$evals, points(x, fx))
for(k in 1:out$n_evals) {
  with(out$evals, abline(a = fx[k] - dfx[k]*x[k], b = dfx[k], col = "blue"))
  Sys.sleep(1)
}



f <- function(x) (x-.24) * (x - .51) * (x - .76)
df <- function(x) 3*x^2 - 3.02*x + .6924
x0 <- .40
(out <- uninewton(f, df, x0))

curve(f(x), col = "red", from = .30, to = .70)
with(out$evals, points(x, fx))
for(k in 1:out$n_evals) {
  with(out$evals, abline(a = fx[k] - dfx[k]*x[k], b = dfx[k], col = "blue"))
  Sys.sleep(1)
}

dkahle/kumerical documentation built on May 27, 2019, 11:49 p.m.