% Computes pi using the Taylor series for the arctan(x) at x=1: % pi/4 = 1 - 1/3 + 1/5 - 1/7 + ... + (-1)^N/(2N+1) % Set the value of N N = 1000; % Initialize the variable used for the partial sums. p = 0; % Here is the implementation using a loop to sum the individual % terms. for k = 0:N p = p + (-1)^k/(2*k + 1); end p = 4*p; % p contains the approximation to pi % The above code can be "vectorized" as follows: % (note: just uncomment out the following two lines.) % k = 0:N; % p = 4*sum((-1).^k./(2*k + 1));