Ask Your Question

Plot evaluation different than function evalution

asked 2014-02-20 19:33:02 +0200

Kyle gravatar image

updated 2017-01-08 12:17:41 +0200

FrédéricC gravatar image

My sage input is

sage: eta, chi, omega, omega_n = var('eta chi omega omega_n')
sage: exp1=integrate(e**(-eta**2*chi**2/2)*e**(I*(omega_n - omega)*chi),(chi,-1,1))
sage: exp2 = e**(-(omega - omega_n)**2/(2*eta**2))

It turns out that exp1 is $\newcommand{\Bold}[1]{\mathbf{#1}}-\frac{\sqrt{2} \sqrt{\pi} \text{ erf}\left(-\frac{\sqrt{2} \eta^{2} + i \sqrt{2} \omega - i \sqrt{2} \omega_{n}}{2 \eta}\right) e^{\left(-\frac{\omega^{2}}{2 \eta^{2}} + \frac{\omega \omega_{n}}{\eta^{2}} - \frac{\omega_{n}^{2}}{2 \eta^{2}}\right)}}{2 \eta} + \frac{\sqrt{2} \sqrt{\pi} \text{ erf}\left(\frac{\sqrt{2} \eta^{2} - i \sqrt{2} \omega + i \sqrt{2} \omega_{n}}{2 \eta}\right) e^{\left(-\frac{\omega^{2}}{2 \eta^{2}} + \frac{\omega \omega_{n}}{\eta^{2}} - \frac{\omega_{n}^{2}}{2 \eta^{2}}\right)}}{2 \eta}$

I wanted to check if exp2 can be used as an approximation of exp1.

Now suppose I choose to evaluate the absolute error

sage: N(abs((exp1-exp2).subs(omega=2**4,omega_n=0,eta=0.4)))

This is what it should be. However if I plot it (choosing five points for simplicity)

sage: p1 = plot(abs((exp1-exp2).subs(omega=2**4,omega_n=0)),(eta,0,1),plot_points=5)
sage: list(p1[0])

For some reason below about 0.41 the plot function evaluates it to 0, but a call like

sage: N(abs((exp1-exp2).subs(omega=2**4,omega_n=0,eta=0.0025)))

shows that this is incorrect behavior. Any idea why plot fails to evaluate this correctly for "small" values?

The larger omega is, the worse the cutoff is. That is, large omega means that the plot goes to 0 for even if eta is larger than 0.41 in general. For example choosing omega=2**5 gives a cutoff at about eta = 0.84, so that plot just puts zeros for eta < .84 in this case.

edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted

answered 2014-06-29 14:17:06 +0200

vdelecroix gravatar image


The mess comes from the conjunction of the method _plot_fast_callable (which create a fast evaluation version of your symbolic function) and sage.plot.plot.generate_plot_points (which actually evaluate the function at some points). Taking your notation

sage: fs = abs((exp1-exp2).subs(omega=2**4,omega_n=0))
sage: ffc = fs._plot_fast_callable()    # this used internally when doing plot(fs, ...)
sage: sage.plot.plot.generate_plot_points(ffc, (0,1), plot_points=5)
[(0.0025, 0.0),
 (0.2734882069985261, 0.0),
 (0.3826944564983164, 0.0),
 (0.4097567481781129, 0.0),
 (0.4368190398579094, 0.03143252012240971),
 (0.4909436232175024, 0.030322174131774005),
 (0.5991927899366885, 0.027848144066936354),
 (0.8156911233750606, 0.022234674748898026),
 (1.0, 0.017253323432106536)]

A workaround is to do yourself the fast callable step::

sage: ffc2 = fast_callable(fs, domain=CC, vars=[eta])
sage: sage.plot.plot.generate_plot_points(ffc2, (0,1), plot_points=6)
[(0.15610206187034426, 0.035375246817379545),
 (0.44301929314962857, 0.03131017699174559),
 (0.6601550660647182, 0.026335779049672842),
 (0.7019017926530642, 0.025262664135765032),
 (1.0, 0.017253323432106536)]

I will not track much further the bug but I hope it helped.


edit flag offensive delete link more


Thanks, that answers my question completely.

Kyle gravatar imageKyle ( 2014-07-15 22:23:52 +0200 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2014-02-20 19:33:02 +0200

Seen: 296 times

Last updated: Jun 29 '14