clear clf np = 100; pp = zeros(np, 1); for i = 1:np pp(i) = leibnizpi(i*100); end loglog((1:np)*100, pp - pi, 'o-') hold on loglog((1:np)*100, 0.01 ./ (1:np), '--') grid on legend('experiment', 'guess') function piapprox = leibnizpi(n) % leibnizpi calculate approximation to pi by summing n terms of Leibniz series piapprox = 0; s = 1; for i = 0:n piapprox = piapprox + s/(2*i + 1.0); s = -s; end piapprox = 4*piapprox; end