Ask Your Question
3

How to correctly plot x^(1/3)

asked 2020-04-25 13:53:08 +0100

gg gravatar image

updated 2020-04-25 16:09:55 +0100

sage: plot(x**(1/3))

This command produces a graph that looks like this:

image

I was expecting a graph like this:

image

edit retag flag offensive close merge delete

Comments

1

What do you expect to get instead?

rburing gravatar imagerburing ( 2020-04-25 14:31:05 +0100 )edit

I was expecting this

gg gravatar imagegg ( 2020-04-25 16:02:09 +0100 )edit

5 Answers

Sort by » oldest newest most voted
3

answered 2020-04-25 16:18:15 +0100

rburing gravatar image

updated 2020-04-25 16:22:20 +0100

SageMath sometimes chooses complex cube roots, which explains this behavior.

There's not much you can do about this internal choice. But you can do this:

plot(sgn(x)*abs(x)^(1/3),(x,-13,13))

cube root graph

edit flag offensive delete link more

Comments

How you arrive at this function sgn(x)*abs(x)^(1/3)?

gg gravatar imagegg ( 2020-04-25 17:08:30 +0100 )edit
rburing gravatar imagerburing ( 2020-04-25 17:57:42 +0100 )edit
1

To get the real cube root of $-x$ (where $x>0$) you just have to add a minus sign in front of the real cube root of $x$; that's what this does.

rburing gravatar imagerburing ( 2020-04-25 18:01:27 +0100 )edit
4

answered 2020-04-26 13:51:45 +0100

Masacroso gravatar image

updated 2020-04-27 17:30:52 +0100

Another way is to use the method x.nth_root(3) for real numbers x.

Using this

sage: plot(lambda x: RR(x).nth_root(3), (-1, 1))

gives the desired plot.

edit flag offensive delete link more
3

answered 2021-09-30 17:36:55 +0100

kcrisman gravatar image

updated 2021-09-30 17:37:35 +0100

After many, many years, we finally have real_nth_root as well. From the documentation:

sage: plot(real_nth_root(x, 3), (x, -1, 1))

plot of cube root for real input

See Trac 12074 for some of the story.

edit flag offensive delete link more

Comments

1

Nice !

tmonteil gravatar imagetmonteil ( 2021-09-30 17:43:58 +0100 )edit
2

answered 2020-04-25 14:57:14 +0100

slelievre gravatar image

updated 2020-04-25 19:02:31 +0100

[Edited after the question was updated, clarifying it is not "why does the graph not start at zero", but "why is the negative part missing".]

Together with the unsatisfactory plot, the command

sage: plot(x^(1/3))

gives some warnings:

verbose 0 (3797: plot.py, generate_plot_points)
WARNING: When plotting, failed to evaluate function at 100 points.
verbose 0 (3797: plot.py, generate_plot_points)
Last error message: 'can't convert complex to float'

They give a clue as to what goes wrong.

By default, plot plots on the interval $[-1, 1]$ using 200 points regularly spaced along that interval to evaluate the function being plotted.

The warning that it "failed to evaluate function at 100 points", and the plot it produces, reveal that it failed to get a point to plot for all the negative values.

Here is how Sage computes powers one-third of negative numbers, depending on whether they are rational or floating-point:

sage: a = (-1)^(1/3)
sage: a
(-1)^(1/3)
sage: b = (-1.)^(1/3)
sage: b
0.500000000000000 + 0.866025403784439*I

And here is what happens when trying to convert them to floats (plot does that):

sage: float(a)
---------------------------------------------------------------------------
Traceback (most recent call last)
...
TypeError: can't convert complex to float
During handling of the above exception, another exception occurred:
TypeError: unable to simplify to float approximation
sage: float(b)
Traceback (most recent call last)
...
TypeError: unable to convert 0.500000000000000 + 0.866025403784439*I
to float; use abs() or real_part() as desired

To plot the real-cube-root function, i.e. the composition inverse of the $(x \mapsto x^3)$ bijection, there are various options.

One is indicated in @rburing's answer, and uses the sign and the absolute value to work around this limitation in Sage. It is actually part of the SageMath FAQ, see:

One other option is to plot $x = y^3$ instead of $y = x^(1/3)$.

This can be done using a parametric plot:

sage: parametric_plot((lambda t: t^3, lambda t: t), (-1, 1))

parametric plot: cube root from minus one to one

This can also be done using an implicit plot since the desired graph is the locus where $x - y^3$ is zero:

sage: implicit_plot(lambda x, y: x - y^3, (-1, 1), (-1, 1))

implicit plot: cube root from minus one to one

By default implicit plot comes with a frame instead of axes.

One can add axes:

sage: implicit_plot(lambda x, y: x - y^3, (-1, 1), (-1, 1), axes=True)

parametric plot: cube root from minus one to one, with axes

One can additionally remove the frame:

sage: implicit_plot(lambda x, y: x - y^3, (-1, 1), (-1, 1), axes=True, frame=False)

parametric plot: cube root from minus one to one, with axes, no frame

edit flag offensive delete link more

Comments

what's the difference between plot and implicit_plot?

gg gravatar imagegg ( 2020-04-25 19:32:10 +0100 )edit

Suppose we want to visualise the set of points $(x, y)$ where $x^2 + y^2 = 1$. This cannot be put in the form $y = f(x)$. But one can think of it as the set of points where $x^2 + y^2 - 1$ is zero, and so plot it as

sage: implicit_plot(lambda x, y: x^2 + y^2 - 1, (-1, 1), (-1, 1))

It is called an implicit plot because instead of the plot of a function, it is the plot of some relation between $x$ and $y$. Some pieces of the plot could be put in the form $y = f(x)$ or $x = g(y)$, but these functions remain implicit here, as we only use some relation between $x$ and $y$ to get the plot.

slelievre gravatar imageslelievre ( 2020-04-25 20:50:41 +0100 )edit

So, its a function to plot relations rather than function. Thanks

gg gravatar imagegg ( 2020-04-26 11:53:52 +0100 )edit
0

answered 2020-04-25 15:13:23 +0100

Sébastien gravatar image

Another option is to specify the xmin, xmax, ymin, ymax or just one of them:

sage: plot(x**(1/3), ymin=0)
edit flag offensive delete link more

Comments

Specifying these arguments still doesn't plot the correct graph.

gg gravatar imagegg ( 2020-04-25 16:03:54 +0100 )edit

I thought you were unhappy with the fact that the y axis was broken. Indeed, these options do not change the range of the domain of the function which is drawn. If that was the issue, then rburing's answer is the way to go.

Sébastien gravatar imageSébastien ( 2020-04-25 16:51:49 +0100 )edit

So why does a simple graphing calculator do the correct graph but Sage can't?

cybervigilante gravatar imagecybervigilante ( 2020-10-15 03:02:25 +0100 )edit

because the default values corresponds better to what people expect

Sébastien gravatar imageSébastien ( 2020-10-15 21:33:43 +0100 )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

Stats

Asked: 2020-04-25 13:53:08 +0100

Seen: 1,770 times

Last updated: Sep 30 '21