function [meanPi,meanerror]=sphere(n)

%Circle.m-This file will take a sequence of n pairs of random numbers
%(xk,yk) and calculate Pi by asking how many of them fell within a circle
%inscribed in the unit square.

increment=5000;
x0=1/2;             %Y-center
y0=1/2;             %X-center
z0=1/2;             %Z-center

sections=n/increment;     %Places where pi will be evaluated 
trials=200;          %Number of trials for averages

Pi=zeros(trials,sections);
error=zeros(trials,sections);

for k=1: trials
x=zeros(1,n);
y=zeros(1,n);
z=zeros(1,n);
m=0;

for i=1: n
    x(i)=rand;
    y(i)=rand;
    z(i)=rand;
    
    if (x(i)-x0)^2+(y(i)-y0)^2+(z(i)-z0)^2 <= 1/4, 
        m=m+1;
    end;
    if mod(i,increment)==0,
        Pi(k,i/increment)=6*m/i;
        error(k,i/increment)=abs((pi-Pi(k,i/increment)));
    end;
end
end

meanPi=mean(Pi);
meanerror=mean(error);

%meanPi=pi(i
%Pi=4*m/n;
%error=(pi-Pi)/pi;



