Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Estimating pi using quad

Subject: Estimating pi using quad

From: Jordanna De

Date: 4 Nov, 2010 03:00:06

Message: 1 of 2

I am new to Matlab and have been given the task of deriving pi (ratio of circumference to diameter) using numerical integration. I have done it using the Cartesian coordinates for a semicircle and quad to find the arc length of the unit semicircle. I was told that using parametric equations gives a value closer to the constant used in matlab so I tried that method too. However, my code does not seem to work.

I used angles in degrees to avoid introducing pi. The length formula is the integral of the square root of the sum of the partial derivatives squared.

radius = 1;
t = linspace(0,360,1000);
x = radius*cosd(t);
y = radius*sind(t);
%plot(x,y);
%xlabel('x'); ylabel('y'); title('Plot of unit circle');
Length = quad(@(y) sqrt((-radius*sind(y)).^2 + (radius*cosd(y)).^2), 0, 360);
pi_estimate = Length/(2*radius);
absolute_error = (ratio-pi)/pi;

The code returns a Length of 360 which is the angle subtended instead of the length.

Subject: Estimating pi using quad

From: Roger Stafford

Date: 4 Nov, 2010 04:40:05

Message: 2 of 2

"Jordanna De" <jordannad@gmail.com> wrote in message <iat7jm$7mi$1@fred.mathworks.com>...
> I am new to Matlab and have been given the task of deriving pi (ratio of circumference to diameter) using numerical integration. I have done it using the Cartesian coordinates for a semicircle and quad to find the arc length of the unit semicircle. I was told that using parametric equations gives a value closer to the constant used in matlab so I tried that method too. However, my code does not seem to work.
>
> I used angles in degrees to avoid introducing pi. The length formula is the integral of the square root of the sum of the partial derivatives squared.
>
> radius = 1;
> t = linspace(0,360,1000);
> x = radius*cosd(t);
> y = radius*sind(t);
> %plot(x,y);
> %xlabel('x'); ylabel('y'); title('Plot of unit circle');
> Length = quad(@(y) sqrt((-radius*sind(y)).^2 + (radius*cosd(y)).^2), 0, 360);
> pi_estimate = Length/(2*radius);
> absolute_error = (ratio-pi)/pi;
>
> The code returns a Length of 360 which is the angle subtended instead of the length.
- - - - - - - -
  Your derivatives are not correct. Remember, the derivative of say the sine of an angle with respect to that angle is only equal to the cosine if you are using radian measure.

  However to even be using radian measure already presupposes you already know the ratio between pi and 180. I would think a more convincing way of calculating pi would be some example which has no such assumption, such as the integral of 1/(1+x^2) with respect to x from x = 0 to x = 1. From that integral you can obtain the value of pi and yet there is nothing obviously angular about that function. Archimedes could have used that method if only he had known how to perform numerical integration.

Roger Stafford

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us