ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Mon, 23 Nov 2020 10:18:50 +0100Plot ratio of Bessel functionshttps://ask.sagemath.org/question/54361/plot-ratio-of-bessel-functions/I am new to SageMath and to this page. I'm running this version:
`SageMath version 9.2, Release Date: 2020-10-24 Using Python 3.8.5.`
I tried to plot the ratio of two functions related to Bessel functions.
First I defined `f` as the first derivative of `bessel_J(1, x)`
and `g`as `x*bessel_J(1, x)`:
f(x) = bessel_J(1, x)
g = derivative(f, x)
Then I defined `h` as their ratio:
h = g / (x*f)
Then, I tried to plot `h` with:
plot(h, (x, 0, 10))
The result is an empty plot, showing only the x, y axes, and this is not correct.
Am I doing something wrong? Is it possible to plot such a function and, if yes, how?Sun, 22 Nov 2020 23:49:14 +0100https://ask.sagemath.org/question/54361/plot-ratio-of-bessel-functions/Answer by slelievre for <p>I am new to SageMath and to this page. I'm running this version:</p>
<p><code>SageMath version 9.2, Release Date: 2020-10-24 Using Python 3.8.5.</code></p>
<p>I tried to plot the ratio of two functions related to Bessel functions.</p>
<p>First I defined <code>f</code> as the first derivative of <code>bessel_J(1, x)</code>
and <code>g</code>as <code>x*bessel_J(1, x)</code>:</p>
<pre><code>f(x) = bessel_J(1, x)
g = derivative(f, x)
</code></pre>
<p>Then I defined <code>h</code> as their ratio:</p>
<pre><code>h = g / (x*f)
</code></pre>
<p>Then, I tried to plot <code>h</code> with:</p>
<pre><code>plot(h, (x, 0, 10))
</code></pre>
<p>The result is an empty plot, showing only the x, y axes, and this is not correct.</p>
<p>Am I doing something wrong? Is it possible to plot such a function and, if yes, how?</p>
https://ask.sagemath.org/question/54361/plot-ratio-of-bessel-functions/?answer=54362#post-id-54362The plot is not empty. If you look carefully you will see
a blue line stuck along the axes.
The function `h` is a quotient of two functions,
and the denominator has zeros on the plotting interval.
This means the function `h` takes immense values.
Because of this, the scale chosen for y is immense,
which is sadly not reflected in the tick labels.
The tick labels, which look like 0.5 to 4.0,
are really 0.5e6 to 4.0e6, but for some reason
do not display properly.
Check this:
sage: ph = plot(h, (0, 10))
sage: ph.get_minmax_data()
{'xmin': 0.0005025125628140704,
'xmax': 10.0,
'ymin': -102.31864962768964,
'ymax': 3960099.7499999963}
The solution is to show an appropriate view by setting ymin and ymax.
Additionally you can use `detect_poles=True` to get rid of spurious
vertical lines when the function jumps from minus infinity to plus infinity.
Depending on the scale you choose, try something like:
sage: ph = plot(h, (0, 10), detect_poles=True)
sage: ph.show(ymin=-100, ymax=100)
Launched png viewer for Graphics object consisting of 3 graphics primitives
sage: ph.show(ymin=-10, ymax=10)
Launched png viewer for Graphics object consisting of 3 graphics primitives
sage: ph.show(ymin=-1, ymax=1)
Launched png viewer for Graphics object consisting of 3 graphics primitivesMon, 23 Nov 2020 00:52:48 +0100https://ask.sagemath.org/question/54361/plot-ratio-of-bessel-functions/?answer=54362#post-id-54362Comment by wmaddox for <p>The plot is not empty. If you look carefully you will see
a blue line stuck along the axes.</p>
<p>The function <code>h</code> is a quotient of two functions,
and the denominator has zeros on the plotting interval.</p>
<p>This means the function <code>h</code> takes immense values.</p>
<p>Because of this, the scale chosen for y is immense,
which is sadly not reflected in the tick labels.</p>
<p>The tick labels, which look like 0.5 to 4.0,
are really 0.5e6 to 4.0e6, but for some reason
do not display properly.</p>
<p>Check this:</p>
<pre><code>sage: ph = plot(h, (0, 10))
sage: ph.get_minmax_data()
{'xmin': 0.0005025125628140704,
'xmax': 10.0,
'ymin': -102.31864962768964,
'ymax': 3960099.7499999963}
</code></pre>
<p>The solution is to show an appropriate view by setting ymin and ymax.</p>
<p>Additionally you can use <code>detect_poles=True</code> to get rid of spurious
vertical lines when the function jumps from minus infinity to plus infinity.</p>
<p>Depending on the scale you choose, try something like:</p>
<pre><code>sage: ph = plot(h, (0, 10), detect_poles=True)
sage: ph.show(ymin=-100, ymax=100)
Launched png viewer for Graphics object consisting of 3 graphics primitives
sage: ph.show(ymin=-10, ymax=10)
Launched png viewer for Graphics object consisting of 3 graphics primitives
sage: ph.show(ymin=-1, ymax=1)
Launched png viewer for Graphics object consisting of 3 graphics primitives
</code></pre>
https://ask.sagemath.org/question/54361/plot-ratio-of-bessel-functions/?comment=54363#post-id-54363Thank you! What was confusing me was the scale of the output graph, with the y-axis apparently between `0.0` and `5.0` (no clues about the `e6` part in the image). Now it works correctly setting an appropriate `ymin` and `ymax` as you suggested.Mon, 23 Nov 2020 10:18:50 +0100https://ask.sagemath.org/question/54361/plot-ratio-of-bessel-functions/?comment=54363#post-id-54363