# How to plot Newton's method

**URL:** <https://discourse.julialang.org/t/how-to-plot-newtons-method/51160>\
**Category:** Optimization (Mathematical)\
**Tags:** question, plotting, math\
**Created:** [December 3, 2020, 12:59am UTC](https://discourse.julialang.org/t/how-to-plot-newtons-method/51160 "2020-12-03T00:59:22Z")\
**Posts on this page:** 20\
**Page:** 2

<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:** [December 6, 2020, 10:07pm UTC](https://discourse.julialang.org/t/how-to-plot-newtons-method/51160/21 "2020-12-06T22:07:44Z")

</div>

Don’t know if this is helpful but:

> **[GitHub - mthelm85/newton-raphson-plot](https://github.com/mthelm85/newton-raphson-plot)**
>
> Contribute to mthelm85/newton-raphson-plot development by creating an account on GitHub.

Also, see this:

> [@Visualizing Newton-Raphson](https://discourse.julialang.org/t/visualizing-newton-raphson/37164):
>
> 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] Next is a case where the algorithm doesn’t converge and it just oscillates around: [Newton-Raphson2] Finally, this starting point eventually …

---

<div class="post-metadata">

**Author:** ![brett\_knoss](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brett_knoss/32/13050_2.png) [@brett\_knoss](https://discourse.julialang.org/u/brett_knoss)\
**Post date:** [December 6, 2020, 10:18pm UTC](https://discourse.julialang.org/t/how-to-plot-newtons-method/51160/22 "2020-12-06T22:18:03Z")

</div>

Thanks, not what I was thinking,but an interesting approach. I’ll think about it.

---

<div class="post-metadata">

**Author:** ![j\_verzani](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/j_verzani/32/8551_2.png) [@j\_verzani](https://discourse.julialang.org/u/j_verzani)\
**Post date:** [December 6, 2020, 10:34pm UTC](https://discourse.julialang.org/t/how-to-plot-newtons-method/51160/23 "2020-12-06T22:34:25Z")

</div>

You might need the master version `using Pkg; Pkg.add("SimplePlots#master")`

---

<div class="post-metadata">

**Author:** ![brett\_knoss](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brett_knoss/32/13050_2.png) [@brett\_knoss](https://discourse.julialang.org/u/brett_knoss)\
**Post date:** [December 6, 2020, 10:36pm UTC](https://discourse.julialang.org/t/how-to-plot-newtons-method/51160/24 "2020-12-06T22:36:46Z")

</div>

> [@j\_verzani](#):
>
> Pkg; Pkg.add(“SimplePlots#master”)

I’m told that’s not a valid package name. What version of Julia are you using?

---

<div class="post-metadata">

**Author:** ![brett\_knoss](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brett_knoss/32/13050_2.png) [@brett\_knoss](https://discourse.julialang.org/u/brett_knoss)\
**Post date:** [December 6, 2020, 10:42pm UTC](https://discourse.julialang.org/t/how-to-plot-newtons-method/51160/25 "2020-12-06T22:42:42Z")

</div>

> [@j\_verzani](#):
>
> `using Pkg; Pkg.add("SimplePlots#master"`

now I get

```julia
MethodError: no method matching getindex(::typeof(f), ::Int64)

Stacktrace:
 [1] (::SimplePlots.var"#16#17")(::Function) at C:\Users\Brett\.julia\packages\SimplePlots\6y94O\src\plots\plots.jl:16
 [2] plot!(::SimplePlots.SimplePlot, ::Function, ::Vararg{Any,N} where N; kwargs::Base.Iterators.Pairs{Symbol,Tuple{Real,Real},Tuple{Symbol,Symbol},NamedTuple{(:xlim, :ylim),Tuple{Tuple{Float64,Float64},Tuple{Int64,Int64}}}}) at C:\Users\Brett\.julia\packages\SimplePlots\6y94O\src\plots\plots.jl:23
 [3] plot(::Function, ::Vararg{Any,N} where N; kwargs::Base.Iterators.Pairs{Symbol,Tuple{Real,Real},Tuple{Symbol,Symbol},NamedTuple{(:xlim, :ylim),Tuple{Tuple{Float64,Float64},Tuple{Int64,Int64}}}}) at C:\Users\Brett\.julia\packages\SimplePlots\6y94O\src\plots\plots.jl:3
 [4] (::var"#21#22")(::Float64) at .\In[12]:9
 [5] top-level scope at C:\Users\Brett\.julia\packages\SimplePlots\6y94O\src\interact\gui\make_gui_block.jl:13
 [6] top-level scope at C:\Users\Brett\.julia\packages\SimplePlots\6y94O\src\interact\gui.jl:40
 [7] include_string(::Function, ::Module, ::String, ::String) at .\loading.jl:1091

```

---

<div class="post-metadata">

**Author:** ![j\_verzani](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/j_verzani/32/8551_2.png) [@j\_verzani](https://discourse.julialang.org/u/j_verzani)\
**Post date:** [December 6, 2020, 10:50pm UTC](https://discourse.julialang.org/t/how-to-plot-newtons-method/51160/26 "2020-12-06T22:50:32Z")

</div>

Ooops, that `Plots` syntax is something I have loaded privately. Add these two lines and comment out the offending one.

```julia
ts = range(a, b, length=250)
plot(ts, f.(ts), xlim=(a,b), ylim=(-5,5))
#plot(f, a, b, xlim=(a,b), ylim=(-5,5))

```

---

<div class="post-metadata">

**Author:** ![brett\_knoss](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brett_knoss/32/13050_2.png) [@brett\_knoss](https://discourse.julialang.org/u/brett_knoss)\
**Post date:** [December 6, 2020, 11:00pm UTC](https://discourse.julialang.org/t/how-to-plot-newtons-method/51160/27 "2020-12-06T23:00:50Z")

</div>

> [@j\_verzani](#):
>
> ```julia
> ts = range(a, b, length=250)
> plot(ts, f.(ts), xlim=(a,b), ylim=(-5,5))
> #plot(f, a, b, xlim=(a,b), ylim=(-5,5))
> 
> ```

Back to saying scatter not deffined.

I got an eroor trying to install Simple Plots master, and instead did

```julia
(@v1.5) pkg> add SimplePlots#master

```

---

<div class="post-metadata">

**Author:** ![j\_verzani](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/j_verzani/32/8551_2.png) [@j\_verzani](https://discourse.julialang.org/u/j_verzani)\
**Post date:** [December 6, 2020, 11:12pm UTC](https://discourse.julialang.org/t/how-to-plot-newtons-method/51160/28 "2020-12-06T23:12:07Z")

</div>

Not sure, that function is exported here: [https://github.com/djsegal/SimplePlots.jl/blob/master/src/plots/plots.jl#L62](https://github.com/djsegal/SimplePlots.jl/blob/master/src/plots/plots.jl#L62)

I edited the script posted earlier. Try restarting and running that entire script in a cell to make sure all the packages are loading. If that fails, you can do this with `Plots` and `Interact` in the same manner, I just thought this would be easier to get going.

---

<div class="post-metadata">

**Author:** ![brett\_knoss](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brett_knoss/32/13050_2.png) [@brett\_knoss](https://discourse.julialang.org/u/brett_knoss)\
**Post date:** [December 6, 2020, 11:14pm UTC](https://discourse.julialang.org/t/how-to-plot-newtons-method/51160/29 "2020-12-06T23:14:45Z")

</div>

still says scatter! not defined

---

<div class="post-metadata">

**Author:** ![brett\_knoss](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brett_knoss/32/13050_2.png) [@brett\_knoss](https://discourse.julialang.org/u/brett_knoss)\
**Post date:** [December 6, 2020, 11:16pm UTC](https://discourse.julialang.org/t/how-to-plot-newtons-method/51160/30 "2020-12-06T23:16:26Z")

</div>

```julia
function plot(varargs...; kwargs...)
  simple_plot = SimplePlot(; kwargs...)
  plot!(simple_plot, varargs...; kwargs...)
end

function scatter(varargs...; kwargs...)
  simple_plot = SimplePlot(; kwargs...)
  scatter!(simple_plot, varargs...; kwargs...)
end

```

it looks like scatter needs to be defined as a function.

---

<div class="post-metadata">

**Author:** ![j\_verzani](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/j_verzani/32/8551_2.png) [@j\_verzani](https://discourse.julialang.org/u/j_verzani)\
**Post date:** [December 6, 2020, 11:27pm UTC](https://discourse.julialang.org/t/how-to-plot-newtons-method/51160/31 "2020-12-06T23:27:43Z")

</div>

Okay, I edited the script from an earlier post to use `Plots` + `Interact`. `SimplePlots` was only intended to make things easier. You may need to install `Interact`, but try the above after a restart of the kernel.

---

<div class="post-metadata">

**Author:** ![brett\_knoss](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brett_knoss/32/13050_2.png) [@brett\_knoss](https://discourse.julialang.org/u/brett_knoss)\
**Post date:** [December 7, 2020, 12:08am UTC](https://discourse.julialang.org/t/how-to-plot-newtons-method/51160/32 "2020-12-07T00:08:34Z")

</div>

> [@j\_verzani](#):
>
> ```julia
> using Plots, Interact
> using ForwardDiff
> D(f) = x -> ForwardDiff.derivative(f, float(x))
> f(x) = x^5 - x - 1
> a, b = -1.5, 1.5
> nsteps = 20
> @manipulate for x0=slider(a:0.1:b, value=1.0, label="x0")
> ts = range(a, b, length=251)
> plot(ts, f.(ts), xlim=(a,b), ylim=(-5,5), legend=false)
> plot!(ts, f.(ts)) 
> scatter!([x0],[0])
> 
> xs = Float64[]
> ys = Float64[]
> for i in 1:nsteps
> x1 = x0 - f(x0)/D(f)(x0)
> append!(xs, [x0, x0, x1])
> append!(ys, [0,f(x0), 0])
> x0 = x1
> end
> 
> plot!(xs, ys)
> 
> end
> 
> ```

I can’t execute it in the REPL. it seems to be missing a bracket

---

<div class="post-metadata">

**Author:** ![brett\_knoss](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brett_knoss/32/13050_2.png) [@brett\_knoss](https://discourse.julialang.org/u/brett_knoss)\
**Post date:** [December 7, 2020, 12:25am UTC](https://discourse.julialang.org/t/how-to-plot-newtons-method/51160/33 "2020-12-07T00:25:32Z")

</div>

It said I didn’t have Web IO, so I put

```julia
using WebIO
using IJulia

```

but it says unable to load WebIO

---

<div class="post-metadata">

**Author:** ![j\_verzani](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/j_verzani/32/8551_2.png) [@j\_verzani](https://discourse.julialang.org/u/j_verzani)\
**Post date:** [December 7, 2020, 12:27am UTC](https://discourse.julialang.org/t/how-to-plot-newtons-method/51160/34 "2020-12-07T00:27:10Z")

</div>

I’m confused. Interact runs in jupyter, not the REPL. But copy and paste should work without a syntax error.

---

<div class="post-metadata">

**Author:** ![brett\_knoss](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brett_knoss/32/13050_2.png) [@brett\_knoss](https://discourse.julialang.org/u/brett_knoss)\
**Post date:** [December 7, 2020, 12:30am UTC](https://discourse.julialang.org/t/how-to-plot-newtons-method/51160/35 "2020-12-07T00:30:10Z")

</div>

Interact seems to run in both, the plot doesn’t run .

```julia
using WebIO

Unable to load WebIO. Please make sure WebIO works for your Jupyter client. For troubleshooting, please see the WebIO/IJulia documentation. 

```

 ![Screenshot (120)](https://global.discourse-cdn.com/julialang/original/3X/1/f/1f54b488fee40d0fba318a3be839bdd33435dae2.png) ![Screenshot (121)](https://global.discourse-cdn.com/julialang/original/3X/d/c/dcf09aaaedb3a978c12dc875868d426c033e6218.png) ![Screenshot (122)](https://global.discourse-cdn.com/julialang/original/3X/7/1/71676ac45eb5aca94fbd36c12dd53a1c9c86a6a9.png) ![Screenshot (123)](https://global.discourse-cdn.com/julialang/original/3X/6/3/631185b7d6b6110be7878a444ece58b578597b3b.png) ![Screenshot (124)](https://global.discourse-cdn.com/julialang/original/3X/3/b/3b04acb64f6d17b3c602a9058c5b46f817cee6ae.png)

---

<div class="post-metadata">

**Author:** ![brett\_knoss](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brett_knoss/32/13050_2.png) [@brett\_knoss](https://discourse.julialang.org/u/brett_knoss)\
**Post date:** [February 22, 2021, 8:43pm UTC](https://discourse.julialang.org/t/how-to-plot-newtons-method/51160/36 "2021-02-22T20:43:47Z")

</div>

Does makie allow a reaction to pressing a mouse button, or the mouse x and y locations?

---

<div class="post-metadata">

**Author:** ![brett\_knoss](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brett_knoss/32/13050_2.png) [@brett\_knoss](https://discourse.julialang.org/u/brett_knoss)\
**Post date:** [February 22, 2021, 8:54pm UTC](https://discourse.julialang.org/t/how-to-plot-newtons-method/51160/37 "2021-02-22T20:54:53Z")

</div>

It should be AbstractPlotting.scatter! (if I have Plots loaded)

---

<div class="post-metadata">

**Author:** ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)\
**Post date:** [February 22, 2021, 9:04pm UTC](https://discourse.julialang.org/t/how-to-plot-newtons-method/51160/38 "2021-02-22T21:04:14Z")

</div>

Yes it does:

[http://makie.juliaplots.org/stable/interaction.html](http://makie.juliaplots.org/stable/interaction.html)

---

<div class="post-metadata">

**Author:** ![brett\_knoss](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brett_knoss/32/13050_2.png) [@brett\_knoss](https://discourse.julialang.org/u/brett_knoss)\
**Post date:** [February 22, 2021, 9:09pm UTC](https://discourse.julialang.org/t/how-to-plot-newtons-method/51160/39 "2021-02-22T21:09:16Z")

</div>

Cool! I was doing something similar in PyDev, and I would like to do it in Julia.

---

<div class="post-metadata">

**Author:** ![j\_verzani](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/j_verzani/32/8551_2.png) [@j\_verzani](https://discourse.julialang.org/u/j_verzani)\
**Post date:** [February 22, 2021, 10:53pm UTC](https://discourse.julialang.org/t/how-to-plot-newtons-method/51160/40 "2021-02-22T22:53:51Z")

</div>

> [@j\_verzani](#):
>
> ```julia
> using AbstractPlotting
> using AbstractPlotting.MakieLayout
> using GLMakie
> 
> using ForwardDiff
> D(f) = x -> ForwardDiff.derivative(f, float(x))
> 
> function newton(f=x->x^5 - x - 1, x0=1, a=-1, b=1.5, n = 20)
> 
> function add_move!(scene, points, pplot)
> idx = Ref(0); dragstart = Ref(false); startpos = Base.RefValue(Point2f0(0))
> on(events(scene).mousedrag) do drag
> if ispressed(scene, Mouse.left)
> if drag == Mouse.down
> plot, _idx = mouse_selection(scene)
> if plot == pplot
> idx[] = _idx; dragstart[] = true
> startpos[] = to_world(scene, Point2f0(scene.events.mouseposition[]))
> end
> elseif drag == Mouse.pressed && dragstart[] && checkbounds(Bool, points[], idx[])
> pos = to_world(scene, Point2f0(scene.events.mouseposition[]))
>                     
> # we work with components, not vector
> x,y = pos
>                     
> ptidx = idx[]
>                     
> x = clamp(x, a, b)
> y = zero(y)
> points[][idx[]] = [x,y]
>                     
> points[] = points[]
> end
> else
> dragstart[] = false
> end
> return
> end
> end
> 
> start_point = Node(Point2f0[(x0, 0)])
> points = lift(start_point) do x₀
> xs = [x₀[1][1]]
> for i in 1:n
> xᵢ₋₁ = xs[end]
> xᵢ = xᵢ₋₁ - f(xᵢ₋₁) /D(f)(xᵢ₋₁)
> push!(xs, xᵢ)
> end
> xs
> end
> 
> plt_points = lift(points) do pts
> xs = Float64[]
> ys = Float64[]
> for i in 1:length(pts)-1
> pᵢ, pᵢ₊₁ = pts[i], pts[i+1]
> append!(xs, [pᵢ, pᵢ, pᵢ₊₁])
> append!(ys, [0, f(pᵢ), 0])
> end
> (xs, ys)
> end
> 
> scene = Scene()
> lines!(scene, a..b, f, linewidth=5, color=:blue)
> lines!(scene, a..b, zero, color=:red)
> lines!(scene, plt_points, color=:black, linewidth=2)
> scatter!(scene, start_point, color=:red)
> 
> add_move!(scene, start_point, scene[end])
> 
> scene
> end
> 
> ```

The example I have above in this thread (which needs updating for the newest makie, as the layout is kinda bad) shows how one can drag a point around and have the graph be reactive to that action. Something similar could be done for clicking on points. The particular example above is modified from one of the Makie examples, I think a polygon one.

[Previous page](https://discourse.julialang.org/t/how-to-plot-newtons-method/51160.md?page=1)

[Next page](https://discourse.julialang.org/t/how-to-plot-newtons-method/51160.md?page=3)
