Length of a Circle: Iterative Numerical Approximation (Ver.0)

The equation of a general circle centered at the point

$$ c = (c_x, c_y)$$

with radious $$ r $$ is

$$ (x-c_x)^2+(y-c_y)^2 = r^2 $$

it is known that its length is $$ L = 2\pi r $$. We want to approach the actual length by an increasing value of the inscrived polygon length.

Contents

Iterative approach

Let's illustrate the idea by the following plot:

imagePlot('circleLength.png',100); %private function to show images, not needed

First version

We are now taken the first approximation: An inscribed square

Create a new sript file and named it as circleLengthV0.m

radious = 1;
%Define a 4x2 points table p_i=[x_i,y_i]
points = [radious,0;
         0,radious;
        -radious,0;
        0,-radious]
[m,n] = size(points)
points =

     1     0
     0     1
    -1     0
     0    -1


m =

     4


n =

     2

m rows (number of points) and n columns (number of coordinates)

Now we compute the length of the closed curve associated to this polygon.

Each edge has length

$$ length_{i+1,i} = \sqrt{(x_{i+1}-x_i)^2 + (y_{i+1}-y_i)^2)}$$

leng1 = 0;
for i = 1:m
    if (i < m)
        leng1 = leng1+sqrt((points(i+1,1)-points(i,1))^2+(points(i+1,2)-points(i,2))^2);
    else %closed curve: joint last point with first one
        leng1 = leng1+sqrt((points(1,1)-points(i,1))^2+(points(1,2)-points(i,2))^2);
    end
end
leng1
leng1 =

    5.6569

Using Matlab functions (in this case norm) we can reach the same result

leng2 = 0;
for i = 1:m
    if (i < m)
        leng2 = leng2+norm(points(i+1,:)-points(i,:));
    else %closed curve: joint last point with first one
        leng2 = leng2+norm(points(1,:)-points(i,:));
    end
end
[leng2, leng1-leng2]
ans =

    5.6569         0

Finally the error obtained by the present approximation is:

actualLength = 2*pi*radious;
absError = abs(leng2-actualLength);
relError = absError/actualLength;
[absError,relError]
ans =

    0.6263    0.0997

First exercise

Pass the computation of the length of the polygon to a function file named it as curveLength.m

function cLength = curveLength(points,numPoints)

Second exercise

As a first exercise reproduce the previous computation for the Octagon using now $$r=5$. This means that the points are the previous ones plus the ones associated to angle $$\pi / 4$ which are

$$ x = y = r\sqrt2/2; $$

and the appropriate change of signs according to the associated quadrant.

Solution=

length= 30.6147  absError = 0.8013   relError = 0.0255

(c) Numerical Factory 2017