I have implemented erf(x) using its Taylor expansion in Matlab. But even after repeated attempts to correct it, it shows wrong answer for x>1. I am not able to understand why it is so.
Any help will be greatly appreciated.
function ret = erfEval(x,b)
# b denotes how much error is to be compensated for the x value
n = 26;
c = ones(n+1,1); # Coefficient array
c(1) = 1;
for i=2:n+1
c(i)=c(i-1)*(2*i-3)/((2*i-1)*(i-1)); % Calculating the coefficients to be multiplied
end
# ret denotes the return value
ret = zeros(length(x),1);
for i = 1:13
ret = ret + (-1)^(i-1)*c(i)*(x.^(2*i-1));
end
if b>1
for i = 14:21
ret = ret + (-1)^(i-1)*c(i)*(x.^(2*i-1));
end
end
if b>3
for i = 22:n+1
ret = ret + (-1)^(i-1)*c(i)*(x.^(2*i-1));
end
end
ret = ret*2/sqrt(pi);