I’ve been studying the Newton-Raphson algorithm and coded up a simple example to show some of its shortcomings with particular functions. I’m posting here simply to share with the community and, as always, I welcome feedback!

First is the case where we choose a good starting point and the algorithm quickly converges to find the root:

Next is a case where the algorithm doesn’t converge and it just oscillates around:

Finally, this starting point eventually converges, but only after taking a long strange trip down the curve (that was for you, deadheads ):

And here’s the simple code to generate these visuals:

```
using ForwardDiff
using Plots
f(x) = 0.05x^3 - 1.2x + 5
range = -8.0:0.01:8
x = -3
xᵢ = []
yᵢ = []
for i in 1:11
if i == 1
push!(xᵢ, x)
push!(yᵢ, f(x))
else
global x = x - (f(x) / ForwardDiff.derivative(f, x))
push!(xᵢ, x)
push!(yᵢ, f(x))
end
end
anim = @animate for i in 1:length(xᵢ)
p = plot(
range,
f.(range),
legend=false,
title="Iteration $(i-1), x = $(round(xᵢ[i], digits=2)), y = $(round(yᵢ[i], digits=2))",
framestyle=:origin
)
scatter!([xᵢ[i]], [yᵢ[i]])
end
gif(anim, "Newton-Raphson3.gif", fps=0.5)
```