How to code Taylor's series approximation of Error function? Is following code right? Please help

z=0:4; ert=zeros(1,length(z)); sm=zeros(1,length(z)); sm=0; for k=1:length(z) for n=0:500 sm=sm+((-1)^n*z(k)^(2*n+1))/(gamma(n-1)*(2*n+1)); end ert(k)=(2/sqrt(pi))*sm; end

Respuestas (2)

Torsten
Torsten el 8 de Mzo. de 2017
Editada: Torsten el 8 de Mzo. de 2017
Although the coding is not optimal, it looks ok.
Why don't you just compare "ert(k)" and erf(z(k)) to get an answer to your question ?
Best wishes
Torsten.

1 comentario

if true
% code
end
In the above code, I've compared answers with inbuilt 'erf' MATLAB command answers. I'm getting correct answers only for 0 to 3 after that m getting NaN.

Iniciar sesión para comentar.

Torsten
Torsten el 8 de Mzo. de 2017
Editada: Torsten el 9 de Mzo. de 2017
You must avoid to explictly calculate z^(2*n+1)/n!.
For large n and |z|>1, you'll get an infinity/infinity problem.
Use that
[(z^2)^n/n!] = [(z^2)^(n-1)/(n-1)!] * (z^2/n).
Thus, you can calculate summand (n) of the series from summand (n-1) instead of recalculating every summand anew.
Best wishes
Torsten.

Categorías

Más información sobre Mathematics en Centro de ayuda y File Exchange.

Preguntada:

el 8 de Mzo. de 2017

Editada:

el 9 de Mzo. de 2017

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by