The idea of Newton’s method

Suppose we want to find a root of the function

using Plots
f = x -> x*exp(x) - 2

plot(f,0,1.5,label="function",grid=:y,xlabel="x",ylabel="y",legend=:topleft)

From the graph, it is clear that there is a root near \(x=1\). So we call that our initial guess, \(x_1\).

x1 = 1
f1 = f(x1)
scatter!([x1],[f1],label="initial point")

Next, we can compute the tangent line at the point \(\bigl(x_1,f(x_1)\bigr)\), using the derivative.

dfdx = x -> exp(x)*(x+1)
slope1 = dfdx(x1)
tangent1 = x -> f1 + slope1*(x-x1)

plot!(tangent1,0,1.5,l=:dash,label="tangent line",ylim=[-2,4])

In lieu of finding the root of \(f\) itself, we settle for finding the root of the tangent line approximation, which is trivial. Call this \(x_2\), our next approximation to the root.

@show x2 = x1 - f1/slope1
scatter!([x2],[0],label="tangent root")
x2 = x1 - f1 / slope1 = 0.8678794411714423
f2 = f(x2)
0.06716266657572145

The residual (value of \(f\)) is smaller than before, but not zero. So we repeat the process with a new tangent line based on the latest point on the curve.

plot(f,0.8,0.9,label="function",
    xlabel="x", ylabel="y", title="Second iteration", legend=:topleft)

scatter!([x2],[f2],label="starting point")

slope2 = dfdx(x2)
tangent2 = x -> f2 + slope2*(x-x2)
plot!(tangent2,0.8,0.9,l=:dash,label="tangent line")

@show x3 = x2 - f2/slope2
scatter!([x3],[0],label="tangent root")
x3 = x2 - f2 / slope2 = 0.8527833734164099
f3 = f(x3)
0.0007730906446230534

We appear to be getting closer to the true root each time.