function F = myErf( x )
%
%  Error function calculated using Taylor polynomial,
%  up to 10^-9 accuracy (n = 11)
%
%  Math 340 * Victor Matveev * February 4, 2012
%  See solution to homework #2 for details

x2 = x*x;       % Pre-compute x^2 for efficiency
n  = 11;        % n=11 is sufficient for 10^-9 accuracy
F  = 1/(2*n+1);  

for k = n:-1:1
    F = 1 / (2*k-1) - x2 * F / k;
end

F = F * x * 2 / sqrt(pi);  

fprintf(' myErf(%g)=%g\n Exact: Erf(%g)=%g\n Difference=%g\n',...
         x, F, x, erf(x), abs(F-erf(x)));  
end

