%Purpose: calculate the area of a half circle with radius of 1
%then find pi
%Author: Jie Zhang
%Date:   Nov. 05, 2013
%Comments: this is for the CDS130 class

%specify the integration interval and number of iterations
format long   % use long digital format to increase the accuracy
a=-1;    % lower limit
b=+1;  % upper limit
N=10000;  % Number of iterations

dx=(b-a)/N;  %the sub-interval

%specify the initial value
x(1)=a;  
f(1)=sqrt(1-(x(1))^2);
F(1)=f(1)*dx;

%iteration to find the integration value
for i=[1:N-1]
  x(i+1)=x(i)+dx;
  f(i+1)=sqrt(1-(x(i+1))^2);
  F(i+1)=F(i)+f(i+1)*dx;
end

my_pi=2*F(N);

sprintf('My Pi value = %12.9f,  when N=%i', my_pi,N)
%print pi with length of 12, and 9 decimal points
%print N in integer format