How to Create Multiple Plots of Animation to Estimate Pi using Julia Plots or Luxor? Not with Pluto

Hi all,

I want to try this, but do not want to use Pluto, is there any nice advice for me?

More advice: don’t use Luxor for interactivity- unless you use, er, PlutoUI.

1 Like

I’ve never understood why 3.14 was chosen as “pi day” when the 22nd of July, 22/7, is a better fit.

Apologies for veering off topic.

3 Likes

It is okay, it is a good logic to choose July 22nd

Maybe the code does not work, thus I do not want to use Pluto, rather use REPL instead

look at this warning and error I get for using the codes from the web above:

that is by using Pluto…

Anyway great news I manage to plot it manually without interactivity, which is all I need currently. Interactivity can wait later on. After I get 128 GB of RAM in the future haha…

this is the working code if anyone wants to learn:

using Plots
gr()

function circumference_polygon(sides, radius=1.0)
	# inner angle
	α = 2π / sides
	# angle between side and vertex-center line
	β = (π - α) / 2
	
	# using law of sines
	side_length = radius * sin(α) / sin(β)

	return side_length * sides
end

function estimate_pi(sides)
	circumference = circumference_polygon(sides, 1.0)
	# as the circumference of the circle is 2rπ 
	return circumference / 2
end

function rotate_vector(v, α)
	x, y = v
	x2 = x * cos(α) - y * sin(α)
	y2 = x * sin(α) + y * cos(α)
	return (x2, y2)
end

function polygon_points(n)
	start_point = (0.0, 1.0)
	points = [start_point]
	internal_degree = 2π/n

	for i in 2:n
		last_point = points[end]
		new_point = rotate_vector(last_point, internal_degree)
		push!(points, new_point)
	end

	return points
end

#change the n_sides to any number you desire
n_sides = 3

pi_estimate = round(estimate_pi(n_sides), digits=5)

# plot a unit circle
plot(x -> sqrt(1 - x^2); color=:blue, aspect_ratio=1.0, legend=false)
plot!(x -> -sqrt(1 - x^2), color=:blue)
	
# plot the polygon
plot!(Shape(polygon_points(n_sides)), fillcolor=:green)
	
# annotate with sides and pi estimate
annotate!([(0, 0, ("n=$n_sides \n π̂=$pi_estimate", 20, :white, :center))])

This is not a warning but an error, and imho it is pretty clearly stated plus there are even links you can click to automatically fix things for you - just click on either “Split this cell into 2 cells” or “Wrap all code in a begin … end block”

You’ve wrapped most of your code in begin ... end, but have another line of code (using Plots) in the same cell outside begin ... end