Thread Subject: Help! Numerically handle removable singularities: a more clear

Subject: Help! Numerically handle removable singularities: a more clear

From: Luna Moon

Date: 14 Nov, 2009 21:52:56

Message: 1 of 9

Hi all,

I am helping my friend on this issue.

We have some numerical computation which involves a lot of "log(g)*g"
and we have to evaluate these for g on the interval [0, 1],
numerically... The reason that 0 is included is that because the
singularity at 0 is removable.


In Maple:

> x := 0; log(x)*x;
%;
                                      0
Error, (in ln) numeric exception: division by zero

In Matlab:


>> x=0; log(x)*x

ans =

   NaN


Our procedure is that we first derive very complicated symbolic
expressions in Maple and then translate into Matlab to get numerical
calculation.

How do handle the above "NaN" in Matlab? This is just one intermediate
step using "log(x)*x" (this expression is from Maple symbolic
results).


Of course we could take a limit at x=0 and handle that case in Matlab
separately, let say when |x| < 1e-5, we switch to this limiting
result; and when |x|>=1e-5, we still use the numerical approach.


But this creates a discontinuity at |x|=1e-5 in numerical results in
Matlab.


Could anybody shed some lights on us about how to handle such a
situation?


Thanks a lot!

Subject: Help! Numerically handle removable singularities: a more clear example...

From: A.L.

Date: 14 Nov, 2009 22:57:25

Message: 2 of 9

On Sat, 14 Nov 2009 13:52:56 -0800 (PST), Luna Moon
<lunamoonmoon@gmail.com> wrote:

>Hi all,
>
>I am helping my friend on this issue.
>
>We have some numerical computation which involves a lot of "log(g)*g"
>and we have to evaluate these for g on the interval [0, 1],
>numerically... The reason that 0 is included is that because the
>singularity at 0 is removable.
  

http://ideas.repec.org/c/boc/bocode/x031701.html

http://fmwww.bc.edu/repec/bocode/s/slog.ox

A.L.

Subject: Help! Numerically handle removable singularities: a more clear example...

From: Jon Slaughter

Date: 14 Nov, 2009 22:58:31

Message: 3 of 9

Luna Moon wrote:
> Hi all,
>
> I am helping my friend on this issue.
>
> We have some numerical computation which involves a lot of "log(g)*g"
> and we have to evaluate these for g on the interval [0, 1],
> numerically... The reason that 0 is included is that because the
> singularity at 0 is removable.
>
>
> In Maple:
>
>> x := 0; log(x)*x;
> %;
> 0
> Error, (in ln) numeric exception: division by zero
>
> In Matlab:
>
>
>>> x=0; log(x)*x
>
> ans =
>
> NaN
>
>
> Our procedure is that we first derive very complicated symbolic
> expressions in Maple and then translate into Matlab to get numerical
> calculation.
>
> How do handle the above "NaN" in Matlab? This is just one intermediate
> step using "log(x)*x" (this expression is from Maple symbolic
> results).
>
>
> Of course we could take a limit at x=0 and handle that case in Matlab
> separately, let say when |x| < 1e-5, we switch to this limiting
> result; and when |x|>=1e-5, we still use the numerical approach.
>
>
> But this creates a discontinuity at |x|=1e-5 in numerical results in
> Matlab.
>
>
> Could anybody shed some lights on us about how to handle such a
> situation?
>
>
> Thanks a lot!

log(x)*x when x = 0 is undefined because log(x) is undefined

limit(log(x)*x,x=0) = 0

Generally if you are simply doing a numerical computation you could handle
it as a side case such as

if (x == 0) \\ or |x| <= 0.00000001 or something similar
return 0;

or

// hack that is not generally a good way
if (x==0) \\ or |x| <= 0.00000001
 x = 0.00001
// do comp

If your doing some advanced numerical method then generally you will find
some numerial appoximation to log(x)*x in the first place where the
discontinuity is removed such as a taylor series


log(x)*x ~= -sum((1 - x)^k/k)*x
(not a good method)

or by using a faster converging series such as some found at

http://en.wikipedia.org/wiki/Logarithm

Subject: Help! Numerically handle removable singularities: a more clear example...

From: Gordon Sande

Date: 15 Nov, 2009 02:19:02

Message: 4 of 9

On 2009-11-14 17:52:56 -0400, Luna Moon <lunamoonmoon@gmail.com> said:

> Hi all,
>
> I am helping my friend on this issue.
>
> We have some numerical computation which involves a lot of "log(g)*g"
> and we have to evaluate these for g on the interval [0, 1],
> numerically... The reason that 0 is included is that because the
> singularity at 0 is removable.

Just as there is a more common name for sin(x)/x as sinc(x) which
is fairly common in signal processing there may be a common name
for x*log(x) which shows up in entropy calculations. There may even
be known approximations for x*log(x) as there is for sin(x)/x. Google
is your friend as I do not know of a ready source.

Try defining a new function xln(x) = x * ln(x) in Maple and let it chew
on the result. At least it will be easier to know where the simplifiction
will occur.

> In Maple:
>
>> x := 0; log(x)*x;
> %;
> 0
> Error, (in ln) numeric exception: division by zero
>
> In Matlab:
>
>
>>> x=0; log(x)*x
>
> ans =
>
> NaN
>
>
> Our procedure is that we first derive very complicated symbolic
> expressions in Maple and then translate into Matlab to get numerical
> calculation.
>
> How do handle the above "NaN" in Matlab? This is just one intermediate
> step using "log(x)*x" (this expression is from Maple symbolic
> results).
>
>
> Of course we could take a limit at x=0 and handle that case in Matlab
> separately, let say when |x| < 1e-5, we switch to this limiting
> result; and when |x|>=1e-5, we still use the numerical approach.
>
>
> But this creates a discontinuity at |x|=1e-5 in numerical results in
> Matlab.
>
>
> Could anybody shed some lights on us about how to handle such a
> situation?
>
>
> Thanks a lot!

Subject: Help! Numerically handle removable singularities: a more clear example...

From: Jon Slaughter

Date: 15 Nov, 2009 04:26:53

Message: 5 of 9

BTW, in maple, for such things, I tend to use the piecewise function

xlog := x->piecewise(x=0, 0, x*log(x));

which should return 0 when x is 0 else x*log(x).

Subject: Help! Numerically handle removable singularities: a more clear

From: Marco Ho

Date: 15 Nov, 2009 04:22:16

Message: 6 of 9

I suggest in MATLAB:
x=0+eps;
ie,using eps to avoid the situation where some is divided by zero

Subject: Help! Numerically handle removable singularities: a more clear

From: Mate

Date: 15 Nov, 2009 09:01:11

Message: 7 of 9

On Nov 15, 4:19 am, Gordon Sande <g.sa...@worldnet.att.net> wrote:
> On 2009-11-14 17:52:56 -0400, Luna Moon <lunamoonm...@gmail.com> said:
>
> > Hi all,
>
> > I am helping my friend on this issue.
>
> > We have some numerical computation which involves a lot of "log(g)*g"
> > and we have to evaluate these for g on the interval [0, 1],
> > numerically... The reason that 0 is included is that because the
> > singularity at 0 is removable.
>
> Just as there is a more common name for sin(x)/x as sinc(x) which
> is fairly common in signal processing there may be a common name
> for x*log(x) which shows up in entropy calculations. There may even
> be known approximations for x*log(x) as there is for sin(x)/x. Google
> is your friend as I do not know of a ready source.
>
> Try defining a new function xln(x) = x * ln(x) in Maple and let it chew
> on the result. At least it will be easier to know where the simplifiction
> will occur.
>


There is a big difference between sin(x)/x and x*log(x); the first is
analytic
(removable singularity at 0) but the second is not. `xln` is
continuous in
[0,oo) but is not differentiable at 0, so it will generate many
problems.

Mate

Subject: Help! Numerically handle removable singularities: a more clear

From: Sebastian Nowozin

Date: 16 Nov, 2009 09:13:07

Message: 8 of 9


Hi,

On Nov 15, 2:19 am, Gordon Sande <g.sa...@worldnet.att.net> wrote:

> Just as there is a more common name for sin(x)/x as sinc(x) which
> is fairly common in signal processing there may be a common name
> for x*log(x) which shows up in entropy calculations. There may even
> be known approximations for x*log(x) as there is for sin(x)/x. Google
> is your friend as I do not know of a ready source.

A good idea.

Another idea, not necessarily good and its applicability depends a lot
on the remaining part of your problem.
Do a variable substitution exp(y)=x, hence x*ln(x) becomes exp(y)*ln
(exp(y)) which is exp(y)*y, defined on the entire real line. The
substitution is only really useful in case you can do it on all of
your x variables.

Sebastian

Subject: Help! Numerically handle removable singularities: a more clear

From: Martin Brown

Date: 16 Nov, 2009 09:49:34

Message: 9 of 9

Gordon Sande wrote:
 > On 2009-11-14 17:52:56 -0400, Luna Moon <lunamoonmoon@gmail.com> said:
 >

 >> We have some numerical computation which involves a lot of "log(g)*g"
 >> and we have to evaluate these for g on the interval [0, 1],
 >> numerically... The reason that 0 is included is that because the
 >> singularity at 0 is removable.
 >
 > Just as there is a more common name for sin(x)/x as sinc(x) which
 > is fairly common in signal processing there may be a common name
 > for x*log(x) which shows up in entropy calculations. There may even
 > be known approximations for x*log(x) as there is for sin(x)/x. Google
 > is your friend as I do not know of a ready source.

xlog(x) has somewhat nastier behaviour at the origin than sinc(x).

QAGS in GNU SL will probably handle this integral directly. They give a
similar tricky one on their examples page. Never used their code in
anger myself.

http://www.gnu.org/software/gsl/manual/html_node/Numerical-integration-examples.html

Regards,
Martin Brown

Tags for this Thread

Add a New Tag:

Separated by commas
Ex.: root locus, bode

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.

rssFeed for this Thread

Contact us at files@mathworks.com