Re: pitfall in analytic function
- To: mathgroup at smc.vnet.net
- Subject: [mg41471] Re: pitfall in analytic function
- From: "Dr. Wolfgang Hintze" <weh at snafu.de>
- Date: Wed, 21 May 2003 08:03:01 -0400 (EDT)
- References: <ba7ic1$5r8$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
Narasimham,
I have tried to give a satisfactory solution to your questions in the
notebook attached to this message. Below you find a text copy of it. You
can copy the In[] statements into a fresh notebook and study the effect.
Notice that I have used no comment marks in the text below.
Hope this helps.
Regards,
Wolfgang
-------------------- start of notebook test --------------------
Overview: The whole play consists of five acts (actions, ideas), a
prologue and an epilogue.
Prologue
In[59]:=
ClearAll["General`*"]
Act 1
First it is important to clearly define the functions in question. Let's
start with this one:
In[60]:=
f[x_, p_] := (2^x - x^2)/(x - p)
Act 2
Here I have introduced a parameter p which is equal to 2 in Narasimham's
function. But because of the possible singularity it is better to leave
the value of p open in the definition of f[].
How does f look like?
In[61]:=
Clear[x, p];
f[x, p]
Out[62]=
\!\(\(2\^x - x\^2\)\/\(\(-p\) + x\)\)
Now we plot it, setting p=2:
In[63]:=
p = 2;
Plot[f[x, p], {x, 0, 6}];
Next step: define the integral
In[65]:=
y1[x_, p_] := Integrate[f[t, p], {t, 0, x}]
Act 3
Notice that (in contrast to Narasimham's expression) I have
distinguished clearly between the variable of integration (t) and the
upper limit of integration (x).
How does it look like?
In[66]:=
Clear[x, p];
y1[x, p]
Out[67]=
\!\(1\/2\ \((\(-2\)\ p\ x - x\^2 + 2\^\(1 + p\)\ Gamma[0, p\ Log[2]] -
2\^\(1 + p\)\ Gamma[0, \((p - x)\)\ Log[2]] +
2\^p\ Log[\(-\(1\/p\)\)] - 2\^p\ Log[\(-p\)] + 2\ p\^2\
Log[\(-p\)] +
2\^\(1 + p\)\ Log[p] - 2\^\(1 + p\)\ Log[p - x] -
2\^p\ Log[1\/\(\(-p\) + x\)] + 2\^p\ Log[\(-p\) + x] -
2\ p\^2\ Log[\(-p\) + x])\)\)
Setting p=2 in this expression leads to error messages:
To see this delete the comment marks in the next cell and run it. You
probably need to abort the calculation with Strg+.
In[68]:=
(*
p = 2;
y1[t, p]
*)
Act 4
To avoid that we set p equal to 2. (i.e. we use the "real 2" or
"numerical 2" rather than the "integer 2"). This is numerically
completely equivalent but it helps Mathematica a lot. Here we go:
In[69]:=
Clear[x]; p = 2.;
y1[x, p]
Out[70]=
\!\(\((\(\(3.2472369480282737`\)\(\[InvisibleSpace]\)\) +
0.`\ \[ImaginaryI])\) - 2.`\ x - 0.5`\ x\^2 -
4.`\ Gamma[
0, \(\(1.3862943611198906`\)\(\[InvisibleSpace]\)\) - x\ Log[2]] -
4.`\ Log[\(\(2.`\)\(\[InvisibleSpace]\)\) - 1.`\ x] -
4.`\ Log[\(\(1.3862943611198906`\)\(\[InvisibleSpace]\)\) - x\
Log[2]] -
2.`\ Log[1\/\(\(-1.3862943611198906`\) + x\ Log[2]\)] +
2.`\ Log[\(-1.3862943611198906`\) + x\ Log[2]]\)
Now there are no errors.
There is still a little cloud in the sky: a neglibigle imaginary part of
the form 0.\[ImaginaryI]. We can either use Chop or ignore it. We do the
latter.
Now we can plot y1:
In[71]:=
p = 2.;
Plot[y1[x, p], {x, 0, 10}];
Resulting in a smooth plot; no problems at all at t=2!
Now similarly for the function y2:
In[73]:=
y2[x_, p_] := -2 x - x^2/2 + 4 ExpIntegralEi[-p Log[2] + x Log[2]] -
4 (ExpIntegralEi[-2 Log[2]] - Log[2]) - 4 Log[p - x]
In[74]:=
Clear[x];
p = 2.;
y2[x, p]
Out[76]=
\!\(\(-2\)\ x - x\^2\/2 +
4\ ExpIntegralEi[\(-1.3862943611198906`\) + x\ Log[2]] -
4\ \((ExpIntegralEi[\(-2\)\ Log[2]] - Log[2])\) -
4\ Log[\(\(2.`\)\(\[InvisibleSpace]\)\) - x]\)
We now get definitely imaginary parts because of the branch cuts of Log
and ExpIntegralEi for x>2. For example:
In[77]:=
x = 3.; p = 2.;
y2[x, p]
Out[78]=
-3.07211 - 12.5664 \[ImaginaryI]
Act 5
Hence let's take the real part and call it y2realPart (unfortunately,
taking the real part of an expression in Mathematica is not possible by
just writing Re[expr], but the following procedure must executed)
In[79]:=
y2realPart[x_, p_] := ComplexExpand[Re[y2[x, p]], TargetFunctions ->
{Re, Im}]
In[80]:=
Clear[x, p];
y2realPart[x, p]
Out[81]=
\!\(\(-2\)\ x - x\^2\/2 + 4\ Log[2] - 2\ Log[\((p - x)\)\^2] -
4\ Re[ExpIntegralEi[\(-2\)\ Log[2]]] +
4\ Re[ExpIntegralEi[\(-p\)\ Log[2] + x\ Log[2]]]\)
In[82]:=
Plot[y2realPart[x, 2], {x, 0, 10}];
Epilogue
The plots of the functions y1 and y2realPart look equal. That they are
can be verified by printing some values of the difference.
In[83]:=
p = 2.;
For[x = 0, x <= 5, Print["x = ", x, " -> ", y2realPart[x, p] - y1[x, p]];
x = x + 0.5];
Or, using Chop to get rid of the spurious imaginary parts:
In[85]:=
p = 2.;
For[x = 0, x <= 5,
Print["x = ", x, " -> ", Chop[y2realPart[x, p] - y1[x, p]]];
x = x + 0.5];
Narasimham G.L. wrote:
> I tried to integrate a transcendental function having factor (x-2),
> analytic on the whole line, even at x=2, but unable to get through with
> the smooth function at x=2.The following Mathematica prgram was run, and
> although at x=2 it is a smooth plot ( minimum value is around -1.204 at
> x=2.1), it proves to be a stumbling block in performing integration.
>
> Plot [ (2^x-x^2)/(x-2), { x,0,4 }]
> y1=Integrate [(2^x-x^2)/(x-2),{x,0,x}]
> Plot[y1 ,{x,0, 4} ];
> y2= -2 x - x^2/2 + 4 ExpIntegralEi[-2 Log[2] + x Log[2]] -
> 4 (ExpIntegralEi[-2 Log[2]] - Log[2]) - 4 Log[2 - x];
> Plot[y2 ,{x,2.1, 4} ];
> Plot[y2 ,{x,0, 4} ];
> All integrates terminate at x=2. I like to treat a smooth analytic
> continuous function in the normal way. Failure to do so negates
> smoothness, continuity and analyticity, it would appear. How to
> circumvent this problem?
>
> Thanks for suggestions to overcome this problem.
>