Calculate pi from random numbers, wxMaxima code for the approximation and code for the animation.

preview_player
Показать описание
00:25 Discussion of the math idea behind the approximation of pi.
01:25 Coding the approximation of pi using a large number of points, including making an image of the n=20000 pi approximation.
08:02 Coding the animation of the pi approximation, where the number of points grows as the animation runs and the approximation of pi is updated with each frame of the animation.

We calculate pi from random numbers using a circle of radius one inscribed in a square of side length 2. The random numbers are generated inside the square and the fraction of numbers landing inside the circle allow us to approximate the value of pi. The wxMaxima code for the approximation and code for the animation are given below:

wxMaxima code for the animation:

Note that there are two instances where I said (INSERT LESS THAN SYMBOL) because youtube doesn't allow angled brackets in descriptions.

kill(all)$
/*static approximation of pi code*/
/*circle of radius 1 inside square of side length 2*/
/*area of circle is pi, square is 4, so ratio of areas=pi/4*/
/*approximate pi as 4*ratio of areas=4*#of points inside circle/number inside square*/
/*for loop generating a random point in wxmaxima, testing whether or not inside the circle, then
incrementing a counter if the test is passed, finally print the decimal approximation,
in addition append a list of points for later use in wxdraw2d*/

counter:0$

pointlist:[]$

m:20000$

for n from 1 thru m do(

/*random x and y coordinates and distance to the origin*/
randomx:-1+random(2.0),
randomy:-1+random(2.0),
distance:sqrt(randomx^2+randomy^2),

/*test to see if the point is inside or on the circle, increment the counter if yes*/

if distance(INSERT LESS THAN SYMBOL)=1 then counter:counter+1,

/*build a list of points to plot to visualize the approximation*/

pointlist:append(pointlist,[[randomx,randomy]]),

/*at the last value of n, print the approximation of pi as a decimal*/

approximation:float(4*counter/m),

if n=m then

print (concat("approximate value of pi:",approximation))

);
wxplot_size:[1000,1000]$

wxdraw2d(
nticks=1000,
xrange=[-1.3,1.3],
yrange=[-1.3,1.3],
xtics=false,
ytics=false,

color=blue,
point_type=7,
point_size=.5,
points(pointlist),

color=black,
label([concat("n= ",m," approximation of pi: ",approximation),0,1.1]),

color=black,
line_width=3,
parametric(cos(t),sin(t),t,0,2*%pi),
transparent=true,
rectangle([-1,-1],[1,1])

);
kill(all)$

/*for loop modified for animating the process*/
/*we need a running counter and a list of lists storing the new points as we go*/

counter:0$
pointlist:[]$
approxlist:[]$
fpprintprec:5$
m:1000$

for n from 1 thru m do(

/*random x and y coordinates and distance to the origin*/
randomx:-1+random(2.0),
randomy:-1+random(2.0),
distance:sqrt(randomx^2+randomy^2),

/*test to see if the point is inside or on the circle, increment the counter if yes*/
if distance(INSERT LESS THAN SYMBOL)=1 then counter:counter+1,

/*build a list of points to plot to visualize the approximation*/

pointlist:append(pointlist,[[randomx,randomy]]),


/*get the pi approximation at each step and add to the approximation list*/

approximation:float(4*counter/n),

approxlist:append(approxlist,[approximation])

);
/*make a list of lists that grows the number of points at each step to display in the animation*/
/*the nth list element of cumulativepts is the first n elements of pointlist*/
templist:[]$

cumulativepts:[]$

for n from 1 thru m do(

for k from 1 thru n do(

templist:append(templist,
[pointlist[k]])
),

cumulativepts:append(cumulativepts,[templist]),

templist:[]

)$
wxplot_size:[1000,1000]$
wxanimate_framerate:20$
rlist: makelist(i,i,1,100)$

with_slider_draw(

r, rlist,
nticks=1000,
xrange=[-1.3,1.3],
yrange=[-1.3,1.3],
xtics=false,
ytics=false,

color=blue,
point_type=7,
point_size=1,
points(cumulativepts[r]),

color=black,
label([concat("n= ",r," approximation of pi: "),-.5,1.1]),
label([concat(approxlist[r]),.5,1.1]),

color=black,
line_width=3,
parametric(cos(t),sin(t),t,0,2*%pi),
transparent=true,
rectangle([-1,-1],[1,1])

);
Рекомендации по теме
Комментарии
Автор

thanks for the nice video and the detailed code discussion. I think this shows the maxima plotting capabilities well but not the symbolic ones which is the main use of maxima.

osmanfb