0
$\begingroup$

I have a functions, which is an Integral. It is exaclty this function

Zf[x_] := 2*I*NIntegrate[Exp[-y^2.0 + 2.0*I*y*x], {y, 0, Infinity}];

Later I need to plot a graph of this function like this

Print["init time=", DateString[]]
Timing[Plot[Im[Zf[x]], {x, -2, 2}]]
Print["finish time=", DateString[]]

and it takes around 9 seconds.

but as final result I need to plot some more complicated functions, depends on this Zf like this.

f1[x_, p1_] := I*Im[Zf[x]] + p1*I*(1 + Zf[x]);
f2[x_, p1_, p2_] := 
  p1*(f1[x] + (2 + Zf[x])/ Conjugate[Zf[x]]) - 
   I*p2*(f1[x] - (2 - Zf[x])/ Conjugate[Zf[x]]);

Here p1 and p2 some parameters. and later I plot it again like this

Print["init time=", DateString[]]
Timing[Plot[Im[f1[x, 2]], {x, -2, 2}]]
Print["finish time=", DateString[]]

Print["init time=", DateString[]]
Timing[Plot[Im[f2[x, 1.35, 1.45]], {x, -2, 2}]]
Print["finish time=", DateString[]]

and plotting of f1 takes around 30 seconds, plotting of f2 takes 90 seconds.

If I replace NIntegrate to normal Integrate, it becomes much slower (I even cannot plot first graph in 10 minutes).

The problem is much bigger because finally I need dozens of graphs with comparation of all that functions and different parameters. Also I would like to change parameters quickly and compare the results.

Print["init time=", DateString[]]
Timing[Plot[{Im[Zf[x]], Im[f1[x, 2]], Im[f2[x, 1.35, 1.45]], 
   Im[f1[x, 3]], Im[f2[x, 2.35, 2.45]]}, {x, -2, 2}]]
Print["finish time=", DateString[]]

It takes 240 seconds. Here is only 2 parameters. In real life it is much more complicated. I have around 10 parameters and functions more complicated than f1 and f2. It really takes hours.

Can I do something to improve performance of this kind of calculations?

$\endgroup$
  • 2
    $\begingroup$ Evaluate the integral outside the plotting - use that result within them. Doing so on your first example case results in a ~300X speed difference in a quick test... $\endgroup$ – ciao Jun 18 '15 at 23:16
  • 1
    $\begingroup$ Zf[x] has a nice closed form in terms of the Dawson integral; why not use that instead? $\endgroup$ – J. M. will be back soon Jun 18 '15 at 23:19
  • $\begingroup$ @Guesswhoitis.: 2500X faster on last example using CF result. Post it.... $\endgroup$ – ciao Jun 18 '15 at 23:26
  • $\begingroup$ @ciao, bel already did it. :) $\endgroup$ – J. M. will be back soon Jun 18 '15 at 23:28
3
$\begingroup$

I believe this is fast enough

p[x_] = Integrate[Exp[-y^2 + 2*I*y*x], {y, 0, Infinity}]
Zf[x_] := 2 I p[x]
f1[x_, p1_] := I*Im[Zf[x]] + p1*I*(1 + Zf[x]);
f2[x_, p1_, p2_] := p1*(f1[x] + (2 + Zf[x])/Conjugate[Zf[x]]) - 
                        I*p2*(f1[x] - (2 - Zf[x])/Conjugate[Zf[x]]);

Plot[Im[f1[x, 2]], {x, -2, 2}]
Plot[Im[f2[x, 1.35, 1.45]], {x, -2, 2}]
$\endgroup$
  • $\begingroup$ It works perfect!!! Sorry, but I do not understand why it works much faster? Because p[x] will be Dawson function like this 1/2 E^-x^2 Sqrt[[Pi]] + I DawsonF[x] or because of = sign instead of := ? Or because p[x] after this line p[x_] = Integrate[Exp[-y^2 + 2*Iyx], {y, 0, Infinity}] became something like array $\endgroup$ – Zlelik Jun 20 '15 at 17:41
  • $\begingroup$ It works perfect! Sorry, but I do not understand why it works much faster? Because p[x] will be Dawson function like this 1/2 E^-x^2 Sqrt[\[Pi]] + I DawsonF[x] or because of = sign instead of := ? Also I do not understand what really means p[x_] = Integrate[Exp[-y^2 + 2*I*y*x], {y, 0, Infinity}] ? I understand what is the differens between a=3*2 and a:=3*2. It is set and delayed set. But what it means in case of function? p[x] became something like a table with all data precalculated or what. When it is better to use := and = for functions? Before I thought I must use := to plot it $\endgroup$ – Zlelik Jun 20 '15 at 17:48
  • $\begingroup$ @Zlelik read this mathematica.stackexchange.com/a/18487/193. And all the answers therein, BTW! $\endgroup$ – Dr. belisarius Jun 20 '15 at 20:36
  • $\begingroup$ Thank you very much. Now it is much more clear. $\endgroup$ – Zlelik Jun 20 '15 at 22:18
  • $\begingroup$ Finally it was because of DawsonF[x] function. If I just replace Integrate to DawsonF[x], it became even more faster. $\endgroup$ – Zlelik Jun 24 '15 at 20:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.