Turgut <deveci.turgut@gmail.com> wrote in message <18086837.3862.1242915230023.JavaMail.jakarta@nitrogen.mathforum.org>...
> I have a function which is integral of(bounds are from minus infinity to x) e^((0.5)*t^2)/sqrt(2*pi) dt.
>
> Its Taylor Series is: 0.5+[1/sqrt(2*pi)*(x+(0.5)*x^3/1!*1/3+(0.5)^2*x^5/2!*1/5+...+(0.5)^25*x^51/25!*1/51+...
>
> I need to write a function that keeps counting the series until the sum doesn't change...
>
> I have written this code:
>
> function fx=f(x)
> clear all
> clc
> format long
> x=input('Enter a value to expand around:');
> fx=(1/sqrt(2*pi))*exp(0.5*x.^2)
> k=0;
> term=x;
> sum=0;
> while (fx~=sum)
> fx=sum;
> k=k+1
> term=term*(0.5)*x^2/k*(2*k1)/(2*k+1)
> sum=sum+term
> end
> Fix=0.5+(sum/sqrt(2*pi))
> exact_solution=0.5+0.5*erf(x/sqrt(2))
> end
>
> But I cannot get accurate solution. What is the problem with my code???
You almost have it right. Your problem is that you started out with the wrong value for the 'sum' variable and that causes you to lose the first required term in the sum. What you want to get is the sum:
x  x^3/6 + x^5/40 + x^7/336  ....
but what you are getting is only
x^3/6 + x^5/40 + x^7/336  ....
with the initial x term missing. You should have initialized 'sum' as the value x, rather than zero.
In general it is not a good idea to end an iteration with a requirement of exact equality, as here between 'fx' and 'sum', when you are dealing with floating point fractions. There are cases when values like this will oscillate back and forth due to particular roundoff error combinations and will never drop out of the whileloop. It is usual to end an iteration with some kind of tolerance check, such as
while abs(fxsum) > tol
with 'tol' being a maximum permitted error.
However in your code, admittedly your method above would actually work, with iteration continuing until 'term' becomes so small its addition to 'sum' would cause no change in 'sum.
One thing you should be aware of. The fact that your cumulative distribution function is what is known as an "entire" analytic function means that its Taylor series will always converge to the true cdf value no matter how large x is in either the positive or negative directions, or indeed no matter how large a complex value is used, provided enough terms are summed. However in practical terms this has a limit based on the buildup of roundoff errors produced by overly numerous intermediate calculations. For an extreme example, in your problem if you were to assign x the value 1000, the resulting individual values of 'term' would quickly grow so large you would overflow to infinities before the k numbers could begin to reduce them back down in size. Even if you escaped that fate the roundoff errors would completely ruin the answer. That is why an effective routine for computing
something like the cumulative normal distribution must allow for greater complexity than this simple whileloop method in order to actually work properly for all possible input values. However I understand that such simple methods are a necessary part of one's learning the ins and outs of numerical analysis.
Roger Stafford
