function [y,err] = pi_approx2(n) % y = pi_approx2(n) computes an approximation y of pi using the formula % pi = 4-8*sum_k 1/(16*k^2-1) % with the summation starting from the largest indices % % [y,err] = pi_approx2(n) in addition computes the error err = |y-pi| % round the input down to the next integer (to make sure it is one) n = floor(n); x = 0; for j = n:-1:1 x = x+1/(16*j^2-1); end y = 4-8*x; err = abs(y-pi);