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.Wed, 24 Oct 2018 12:01:52 +0200drawing the function y = |log x|https://ask.sagemath.org/question/43987/drawing-the-function-y-log-x/When drawing the function y = |log x|, I know that x must be positive but I have varied x from -5 to 5 by habit. I had no error message and a graph having a part in the box x<0. I would have liked to attach the graphic but that is denied me.
def y(x):
return abs(log(x))
G = plot(y, (-5,5), color='blue')
G.show(aspect_ratio=1, axes = True, gridlines=True, ymin = -1, ymax = 6)
Fri, 19 Oct 2018 13:51:12 +0200https://ask.sagemath.org/question/43987/drawing-the-function-y-log-x/Comment by rburing for <p>When drawing the function y = |log x|, I know that x must be positive but I have varied x from -5 to 5 by habit. I had no error message and a graph having a part in the box x<0. I would have liked to attach the graphic but that is denied me.</p>
<pre><code> def y(x):
return abs(log(x))
G = plot(y, (-5,5), color='blue')
G.show(aspect_ratio=1, axes = True, gridlines=True, ymin = -1, ymax = 6)
</code></pre>
https://ask.sagemath.org/question/43987/drawing-the-function-y-log-x/?comment=43988#post-id-43988Hint: try `log(-3)` etc. and see what comes out.Fri, 19 Oct 2018 14:01:00 +0200https://ask.sagemath.org/question/43987/drawing-the-function-y-log-x/?comment=43988#post-id-43988Comment by kcrisman for <p>When drawing the function y = |log x|, I know that x must be positive but I have varied x from -5 to 5 by habit. I had no error message and a graph having a part in the box x<0. I would have liked to attach the graphic but that is denied me.</p>
<pre><code> def y(x):
return abs(log(x))
G = plot(y, (-5,5), color='blue')
G.show(aspect_ratio=1, axes = True, gridlines=True, ymin = -1, ymax = 6)
</code></pre>
https://ask.sagemath.org/question/43987/drawing-the-function-y-log-x/?comment=43994#post-id-43994Just click [here](http://sagecell.sagemath.org/?z=eJwti8EKwyAQRO-C_7C3KJhCDumh4Dk_kHsxyTYVbDasSvXva2nn9IY3s-EDqir6JgX8w5gyH-CWqALtzWkppJjAwhkoqWpA9aMZtYGVArHtlpCxa6PpEp_0Vi6euKY7u-TJDgZcwdjOM2c0sLPfgj8w2l-vL3802Q9fdKXhVX8AF-orXg==&lang=sage) for the graphic!Fri, 19 Oct 2018 17:36:26 +0200https://ask.sagemath.org/question/43987/drawing-the-function-y-log-x/?comment=43994#post-id-43994Comment by dazedANDconfused for <p>When drawing the function y = |log x|, I know that x must be positive but I have varied x from -5 to 5 by habit. I had no error message and a graph having a part in the box x<0. I would have liked to attach the graphic but that is denied me.</p>
<pre><code> def y(x):
return abs(log(x))
G = plot(y, (-5,5), color='blue')
G.show(aspect_ratio=1, axes = True, gridlines=True, ymin = -1, ymax = 6)
</code></pre>
https://ask.sagemath.org/question/43987/drawing-the-function-y-log-x/?comment=43995#post-id-43995Although most people learn the logarithm has domain positive reals and range all reals the logarithm, like other functions such as e^x, gets extended to the complex number system. Type: log? on the command line for Sage to see further down: "The log function works for negative numbers, complex numbers, and symbolic numbers too, picking the branch with angle between -pi and pi". Since the absolute value of a complex number is just its distance from the origin Sage has done everything correctly.Fri, 19 Oct 2018 17:52:09 +0200https://ask.sagemath.org/question/43987/drawing-the-function-y-log-x/?comment=43995#post-id-43995Comment by wisher for <p>When drawing the function y = |log x|, I know that x must be positive but I have varied x from -5 to 5 by habit. I had no error message and a graph having a part in the box x<0. I would have liked to attach the graphic but that is denied me.</p>
<pre><code> def y(x):
return abs(log(x))
G = plot(y, (-5,5), color='blue')
G.show(aspect_ratio=1, axes = True, gridlines=True, ymin = -1, ymax = 6)
</code></pre>
https://ask.sagemath.org/question/43987/drawing-the-function-y-log-x/?comment=44049#post-id-44049Thank you very much for the answers. They show me that I still have to study a lot Sage.Wed, 24 Oct 2018 12:01:52 +0200https://ask.sagemath.org/question/43987/drawing-the-function-y-log-x/?comment=44049#post-id-44049Answer by slelievre for <p>When drawing the function y = |log x|, I know that x must be positive but I have varied x from -5 to 5 by habit. I had no error message and a graph having a part in the box x<0. I would have liked to attach the graphic but that is denied me.</p>
<pre><code> def y(x):
return abs(log(x))
G = plot(y, (-5,5), color='blue')
G.show(aspect_ratio=1, axes = True, gridlines=True, ymin = -1, ymax = 6)
</code></pre>
https://ask.sagemath.org/question/43987/drawing-the-function-y-log-x/?answer=44015#post-id-44015One way to obtain the desired graph would be to define a function `real_log`
which would map `x` to `log(x)` if `x` is positive, and return "not a number"
if `x` is not positive.
sage: nan = float(NaN)
sage: real_log = lambda x: log(x) if x > 0 else nan
sage: y = lambda x: abs(real_log(x))
sage: G = plot(y, (-5, 5))
verbose 0 (3763: plot.py, generate_plot_points) WARNING: When plotting, failed to evaluate function at 100 points.
verbose 0 (3763: plot.py, generate_plot_points) Last error message: 'Unable to compute f(-0.047921289673)'
sage: G.show(aspect_ratio=1, axes=True, gridlines=True, ymin=-1, ymax=6)
Launched png viewer for Graphics object consisting of 1 graphics primitive
Sun, 21 Oct 2018 19:23:56 +0200https://ask.sagemath.org/question/43987/drawing-the-function-y-log-x/?answer=44015#post-id-44015