# Visualizing Newton-Raphson

**URL:** <https://discourse.julialang.org/t/visualizing-newton-raphson/37164>\
**Category:** Optimization (Mathematical)\
**Tags:** visualization\
**Created:** [April 7, 2020, 1:26pm UTC](https://discourse.julialang.org/t/visualizing-newton-raphson/37164 "2020-04-07T13:26:41Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mthelm85](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mthelm85/32/224164_2.png) [@mthelm85](https://discourse.julialang.org/u/mthelm85)\
**Post date:** [April 7, 2020, 1:26pm UTC](https://discourse.julialang.org/t/visualizing-newton-raphson/37164/1 "2020-04-07T13:26:41Z")

</div>

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:

![Newton-Raphson1](https://global.discourse-cdn.com/julialang/original/3X/b/3/b36273f94a2e296d7fd350cb5388feddae28b46f.gif)

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

![Newton-Raphson2](https://global.discourse-cdn.com/julialang/original/3X/3/7/3770c2c9e8fcf5eef19e0e9013e8b58f032d2a53.gif)

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

![Newton-Raphson3](https://global.discourse-cdn.com/julialang/original/3X/0/3/036426b81374951e55fbe5979bd33d2181cd403f.gif)

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

```julia
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)

```

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [April 7, 2020, 2:32pm UTC](https://discourse.julialang.org/t/visualizing-newton-raphson/37164/2 "2020-04-07T14:32:31Z")

</div>

Thanks!

Another nice thing to try is to use the `Interact` package to have an interactive version of the plot. It’s also nice to plot the tangent line so that you can visualize the Newton step.

I put together an IJulia notebook that does both of these things to illustrate the Newton method: [https://github.com/mitmath/1806/blob/master/lectures/Multidimensional-Newton.ipynb](https://github.com/mitmath/1806/blob/master/lectures/Multidimensional-Newton.ipynb) (note that you’ll have to download it and run it in order to see the interactive plot — nbviewer doesn’t show this). This notebook was pre Julia 1.0, however, so it will need a few tweaks to run now.

---

<div class="post-metadata">

**Author:** ![mthelm85](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mthelm85/32/224164_2.png) [@mthelm85](https://discourse.julialang.org/u/mthelm85)\
**Post date:** [April 7, 2020, 2:34pm UTC](https://discourse.julialang.org/t/visualizing-newton-raphson/37164/3 "2020-04-07T14:34:39Z")

</div>

> I put together an IJulia notebook that does both of these things to illustrate the Newton method: [https://github.com/mitmath/1806/blob/master/lectures/Multidimensional-Newton.ipynb](https://github.com/mitmath/1806/blob/master/lectures/Multidimensional-Newton.ipynb) (note that you’ll have to download it and run it in order to see the interactive plot — nbviewer doesn’t show this). This notebook was pre Julia 1.0, however, so it will need a few tweaks to run now.

This looks fantastic, thanks for sharing!!

---

<div class="post-metadata">

**Author:** ![chakravala](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chakravala/32/6832_2.png) [@chakravala](https://discourse.julialang.org/u/chakravala)\
**Post date:** [April 7, 2020, 3:33pm UTC](https://discourse.julialang.org/t/visualizing-newton-raphson/37164/4 "2020-04-07T15:33:47Z")

</div>

My package [Fatou.jl](https://github.com/chakravala/Fatou.jl) also allows you to visualize the convergence of Newton Raphson iterations, as well as other fixed point iterations.
