f=function(x) {cat("f");1-exp(x)}; f(root(f,lower=-1,upper=2))
f=function(x) {cat("f");exp(x)-1}; f(root(f,lower=-1,upper=2))
.f = function(x) 1-exp(1*x)
f=function(x) {cat("f");y=.f(x);points(x,y,pch=20,col=rgb(0,0,0,.2));y}
plot(.f,xlim=c(-1,2)); f(root(f,lower=-1,upper=2))
.f = function(x) exp(10*x)-1
f=function(x) {cat("f");y=.f(x);points(x,y,pch=20);y}
plot(.f,xlim=c(-1,2)); f(root(f,lower=-1,upper=2))
.f = function(x) exp(100*x)-1
f=function(x) {cat("f");y=.f(x);points(x,y,pch=20);y}
plot(.f,xlim=c(-1,2)); f(root(f,lower=-1,upper=2))
f=function(x) {cat("f");exp(100*x)-1}; f(root(f,lower=-1,upper=2))
if (FALSE) {
# Quite hard functions to find roots
## Increasing function
## convex
n.f=0
.f = function(x) exp(10*x)-1
f=function(x) {n.f<<-n.f+1;y=.f(x);points(x,y,pch=20);y}
plot(.f,xlim=c(-.1,.2)); f(root(f,lower=-1,upper=2))
print(n.f)
## non-convex
n.f=0
.f = function(x) 1-exp(-10*x)
f=function(x) {n.f<<-n.f+1;y=.f(x);points(x,y,pch=20);y}
plot(.f,xlim=c(-.1,.2)); f(root(f,lower=-1,upper=2))
print(n.f)
# ## Decreasing function
# ## non-convex
n.f=0
.f = function(x) 1-exp(10*x)
f=function(x) {n.f<<-n.f+1;y=.f(x);points(x,y,pch=20,col=rgb(0,0,0,.2));y}
plot(.f,xlim=c(-.1,.2)); f(root(f,lower=-1,upper=2))
print(n.f)
# ## convex
n.f=0
.f = function(x) exp(-10*x)-1
f=function(x) {n.f<<-n.f+1;y=.f(x);points(x,y,pch=20,col=rgb(0,0,0,.2));y}
plot(.f,xlim=c(-.1,.2)); f(root(f,lower=-1,upper=2))
print(n.f)
}
Run the code above in your browser using DataLab