Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Series Expansion

Subject: Series Expansion

From: Turgut

Date: 21 May, 2009 14:13:19

Message: 1 of 2

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*k-1)/(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???

Subject: Series Expansion

From: Roger Stafford

Date: 23 May, 2009 05:20:01

Message: 2 of 2

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*k-1)/(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 round-off error combinations and will never drop out of the while-loop. It is usual to end an iteration with some kind of tolerance check, such as

  while abs(fx-sum) > 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 round-off 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 round-off 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 while-loop 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

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us