Variables and for loops in Pluto.jl

I’m trying to run a loop in Pluto, but I’m having a problem with multiple deffinitions, from the way that Pluto passes on variables.

begin
	f(x)=x^2-x-20
	plot(f,4:.001:12,legend=false)
	x=11 # initial value of x # may replace with a @bind later

	for i in 1:10
		n(x)=x-(f(x)/ForwardDiff.derivative(f,x))# Newton-Rapheson formula
		plot!([x,n(x)],[0,f(n(x))],legend=false)
		plot!([n(x),n(x)],[0,f(n(x))],legend=false)# plots each itteration
		x=n(x)# recursion is used to reset x
	end
	x=11
	plot!([x,x],[0,f(x)]);vline([5]);hline([0])
end

What’s the error message that you’re getting?

2 Likes
x not defined

Sorry, I wasn’t clear on that. As you can see x is deffined, but if there are multiple deffinitions of a variable, then Pluto will throw the error

Multiple definitions for x.

Combine all definitions into a single reactive cell using a `begin ... end` block.

Although, I have x in a begining end block

Just use a let block or a function rather than begin.

3 Likes