www.delorie.com/gnu/docs/calc/calc_63.html   search  
 
Buy the book!


GNU Emacs Calc 2.02 Manual

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

3.7.29 List Tutorial Exercise 11

The circle of unit radius consists of those points (x,y) for which x^2 + y^2 < 1. We start by generating a vector of x^2 and a vector of y^2.

We can make this go a bit faster by using the v . and t . commands.

 
2:  [2., 2., ..., 2.]          2:  [2., 2., ..., 2.]
1:  [2., 2., ..., 2.]          1:  [1.16, 1.98, ..., 0.81]
    .                              .

 v . t .  2. v b 100 RET RET       V M k r

 
2:  [2., 2., ..., 2.]          1:  [0.026, 0.96, ..., 0.036]
1:  [0.026, 0.96, ..., 0.036]  2:  [0.53, 0.81, ..., 0.094]
    .                              .

    1 -  2 V M ^                   TAB  V M k r  1 -  2 V M ^

Now we sum the x^2 and y^2 values, compare with 1 to get a vector of 1/0 truth values, then sum the truth values.

 
1:  [0.56, 1.78, ..., 0.13]    1:  [1, 0, ..., 1]    1:  84
    .                              .                     .

    +                              1 V M a <             V R +

The ratio 84/100 should approximate the ratio pi/4.

 
1:  0.84       1:  3.36       2:  3.36       1:  1.0695
    .              .          1:  3.14159        .

    100 /          4 *            P              /

Our estimate, 3.36, is off by about 7%. We could get a better estimate by taking more points (say, 1000), but it's clear that this method is not very efficient!

(Naturally, since this example uses random numbers your own answer will be slightly different from the one shown here!)

If you typed v . and t . before, type them again to return to full-sized display of vectors.


  webmaster   donations   bookstore     delorie software   privacy  
  Copyright 2003   by The Free Software Foundation     Updated Jun 2003