rm(list=ls())
f = function(x) { # f(x)
return(x + sin(x) - 3)
}
f.prime = function(x) { # f'(x)
return(1 + cos(x))
}
curve(f, from=0, to=6, xlim=c(0, 6), ylim=c(-.5, 1.5), # Plot function.
main="Newton's method for finding a root\n
f(x) = x + sin(x) - 3")
abline(h=0) # x axis
abline(v=0) # y axis
up.color = "black"
tangent.color = "red"
up.lty = "dotted"
tangent.lty = "dashed"
legend(x="top",
legend=c("Move from (x, 0) up to (x, f(x)),",
"and back to x axis along tangent."),
lty=c(up.lty, tangent.lty), col=c(up.color, tangent.color))
# Here is the search code without any graphing.
x = 5 # starting point
for (i in seq_len(6)) {
y = f(x)
tangent.slope = f.prime(x)
# The point-slope form of the line through (x0, y0) with slope m is
# y - y0 = m(x - x0).
# Plug in y = 0 to see the x-intercept is x = x0 - y0/m.
tangent.x.intercept = x - y/tangent.slope
x = tangent.x.intercept # Prepare to repeat the process.
cat(sep="", "x_", i, "=", x, ", f(x)=", y, "\n")
}
# Here is the search code again, this time code added to make a graph
# and show the progress of the search.
x = 5 # starting point
for (i in seq_len(6)) {
y = f(x)
tangent.slope = f.prime(x)
# The point-slope form of the line through (x0, y0) with slope m is
# y - y0 = m(x - x0).
# Plug in y = 0 to see the x-intercept is x = x0 - y0/m.
tangent.x.intercept = x - y/tangent.slope
text(x=x+.09, y=.05, labels=bquote(x[.(i)])) # x_i label
segments(x0=x, y0=0, x1=x, y1=y, col=up.color, lty=up.lty) # vertical line
points(x, y, pch=19) # point on curve
scan(what=character(), n=1, quiet=TRUE) # Require "Enter" to move.
segments(x0=x, y0=y, x1=tangent.x.intercept, y1=0, col=tangent.color, lty=tangent.lty) # tangent
x = tangent.x.intercept # Prepare to repeat the process.
cat(sep="", "x_", i, "=", x, ", f(x)=", y, "\n")
}