0

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?

Zlelik
  • 531
  • 2
  • 8

1 Answers1

3

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}]
Dr. belisarius
  • 115,881
  • 13
  • 203
  • 453
  • 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 + 2Iy*x], {y, 0, Infinity}] became something like array

    – Zlelik Jun 20 '15 at 17:41
  • 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=32 and a:=32. 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

    – Zlelik Jun 20 '15 at 17:48
  • @Zlelik read this http://mathematica.stackexchange.com/a/18487/193. And all the answers therein, BTW! – Dr. belisarius Jun 20 '15 at 20:36
  • Thank you very much. Now it is much more clear. – Zlelik Jun 20 '15 at 22:18
  • Finally it was because of DawsonF[x] function. If I just replace Integrate to DawsonF[x], it became even more faster. – Zlelik Jun 24 '15 at 20:11