Ask Your Question
1

drawing the function y = |log x|

asked 2018-10-19 06:51:12 -0600

wisher gravatar image

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)
edit retag flag offensive close merge delete

Comments

Hint: try log(-3) etc. and see what comes out.

rburing gravatar imagerburing ( 2018-10-19 07:01:00 -0600 )edit
1

Just click here for the graphic!

kcrisman gravatar imagekcrisman ( 2018-10-19 10:36:26 -0600 )edit
1

Although 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.

dazedANDconfused gravatar imagedazedANDconfused ( 2018-10-19 10:52:09 -0600 )edit

Thank you very much for the answers. They show me that I still have to study a lot Sage.

wisher gravatar imagewisher ( 2018-10-24 05:01:52 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2018-10-21 12:23:56 -0600

slelievre gravatar image

One 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
edit flag offensive delete link more

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

Stats

Asked: 2018-10-19 06:51:12 -0600

Seen: 107 times

Last updated: Oct 21